-
Notifications
You must be signed in to change notification settings - Fork 6
/
99_text_edit_as_html_viewer.py
executable file
·90 lines (66 loc) · 2.49 KB
/
99_text_edit_as_html_viewer.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
#!/usr/bin/env python
import io
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()
self.textEdit = self.prepareTextEdit()
# vytvoření správce geometrie
topLayout = QtGui.QVBoxLayout()
# umístění widgetů do okna
topLayout.addWidget(self.textEdit)
topLayout.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 prepareTextEdit(self):
# jednořádkové vstupní textové pole
textEdit = QtGui.QTextEdit(self)
textEdit.setAcceptRichText(True)
textEdit.setReadOnly(True)
with io.open("test.html", encoding="utf-8") as fin:
textEdit.setHtml(fin.read())
return textEdit
# 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):
self.resize(640, 480)
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()