1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
import sys from PyQt4 import QtGui from PyQt4.QtGui import * from PyQt4.QtCore import *
class window(QWidget): def __init__(self, parent=None): super(window, self).__init__(parent) self.setGeometry(100,100,400,300) self.setWindowTitle('test')
edit1 = QLineEdit() edit1.setValidator(QIntValidator()) edit1.setMaxLength(4) edit1.setAlignment(Qt.AlignCenter) edit1.setFont(QFont("Arial",20))
edit2 = QLineEdit() edit2.setValidator(QDoubleValidator(0.99,99.99,2))
edit3 = QLineEdit() edit3.setInputMask("+99_9999_999999")
edit4 = QLineEdit() edit4.textChanged.connect(self.textchanged)
edit5 = QLineEdit() edit5.setEchoMode(QLineEdit.Password) edit5.editingFinished.connect(self.enterPress)
edit6 = QLineEdit("Hello World") edit6.setReadOnly(True)
self.edit7 = QTextEdit() self.edit7.textChanged.connect(self.text7changed)
flo = QFormLayout() flo.addRow(u"只能输入整数", edit1) flo.addRow(u"只能输入两位小数", edit2) flo.addRow(u"设定输入的格式", edit3) flo.addRow(u"文本修改事件", edit4) flo.addRow(u"设定显示模式为密码", edit5) flo.addRow(u"默认文本且只读", edit6) flo.addRow(unicode("QTextEdit使用示例", 'utf8'), self.edit7)
self.setLayout(flo)
def textchanged(self,text): print "contents of text box: " + text
def text7changed(self): data = str(self.edit7.toPlainText().toUtf8()) print "contents of text box: \n" + data
def enterPress(self): print "edited"
if __name__ == '__main__': app = QtGui.QApplication(sys.argv) w = window() w.show() sys.exit(app.exec_())
|