-
Notifications
You must be signed in to change notification settings - Fork 6
/
97_line_edit_regexp_validator.py
executable file
·105 lines (78 loc) · 3.07 KB
/
97_line_edit_regexp_validator.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python
import sys
# import "jádra" frameworku Qt i modulu pro GUI
from PySide import QtCore, QtGui
# nový widget bude odvozen od obecného widgetu
class MainWindowContent(QtGui.QWidget):
def __init__(self):
# zavoláme konstruktor předka
super(MainWindowContent, self).__init__()
# konfigurace GUI + přidání widgetu do okna
self.prepareGUI()
def prepareGUI(self):
quitButton = self.prepareQuitButton()
showTextButton = self.prepareShowTextButton()
self.lineEdit = self.prepareLineEdit()
# vytvoření prvního správce geometrie
topLayout = QtGui.QVBoxLayout()
# vytvoření druhého správce geometrie
subLayout = QtGui.QHBoxLayout()
# umístění widgetů do okna
topLayout.addWidget(self.lineEdit)
topLayout.addLayout(subLayout)
# tlačítka vložíme do druhého správce geometrie
subLayout.addWidget(showTextButton)
subLayout.addWidget(quitButton)
# nastavení správce geometrie a vložení všech komponent do okna
self.setLayout(topLayout)
def prepareShowTextButton(self):
# druhé tlačítko
showTextButton = QtGui.QPushButton("Show text", self)
showTextButton.resize(showTextButton.sizeHint())
# navázání akce na signál
showTextButton.clicked.connect(self.showTextDialog)
return showTextButton
def prepareQuitButton(self):
# tlačítko
quitButton = QtGui.QPushButton("Quit", self)
quitButton.resize(quitButton.sizeHint())
# navázání akce na signál
quitButton.clicked.connect(QtCore.QCoreApplication.instance().quit)
return quitButton
def prepareLineEdit(self):
# jednořádkové vstupní textové pole
lineEdit = QtGui.QLineEdit(self)
lineEdit.setPlaceholderText(u"login name")
lineEdit.setMaxLength(8)
regexp = QtCore.QRegExp("[a-z]{6}[0-9]{2}")
lineEdit.setValidator(QtGui.QRegExpValidator(regexp, self))
return lineEdit
def showTextDialog(self):
msgBox = QtGui.QMessageBox()
text = self.lineEdit.text()
msgBox.setText(u"Text: {t}".format(t=text))
msgBox.setIcon(QtGui.QMessageBox.Information)
msgBox.exec_()
# nový widget bude odvozen od obecného hlavního okna
class MainWindow(QtGui.QMainWindow):
def __init__(self):
# zavoláme konstruktor předka
super(MainWindow, self).__init__()
# konfigurace GUI + přidání widgetu do okna
self.prepareGUI()
def prepareGUI(self):
# velikost není potřeba specifikovat
# self.resize(320, 240)
self.setWindowTitle("QMainWindow + QListWidget")
# vložení komponenty do okna
self.setCentralWidget(MainWindowContent())
def run(self, app):
# zobrazení okna na obrazovce
self.show()
# vstup do smyčky událostí (event loop)
app.exec_()
def main():
app = QtGui.QApplication(sys.argv)
MainWindow().run(app)
if __name__ == "__main__":
main()