-
Notifications
You must be signed in to change notification settings - Fork 0
/
uifile2.py
51 lines (40 loc) · 1.34 KB
/
uifile2.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
import sys
from PyQt5.QtWidgets import *
class filedialogdemo(QWidget):
def __init__(self, parent = None):
super(filedialogdemo, self).__init__(parent)
layout = QVBoxLayout()
self.btn = QPushButton("QFileDialog static method demo")
self.btn.clicked.connect(self.getfile)
layout.addWidget(self.btn)
self.le = QLabel("Hello")
layout.addWidget(self.le)
self.btn1 = QPushButton("QFileDialog object")
self.btn1.clicked.connect(self.getfiles)
layout.addWidget(self.btn1)
self.contents = QTextEdit()
layout.addWidget(self.contents)
self.setLayout(layout)
self.setWindowTitle("File Dialog demo")
def getfile(self):
fname = QFileDialog.getOpenFileName(self, 'Open file',
'c:\\',"Image files (*.jpg *.gif)")
self.le.setPixmap(QPixmap(fname))
def getfiles(self):
dlg = QFileDialog()
dlg.setFileMode(QFileDialog.AnyFile)
dlg.setFilter("Text files (*.txt)")
filenames = QStringList()
if dlg.exec_():
filenames = dlg.selectedFiles()
f = open(filenames[0], 'r')
with f:
data = f.read()
self.contents.setText(data)
def main():
app = QApplication(sys.argv)
ex = filedialogdemo()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()