1import sys
2from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, \
3 QPushButton, QVBoxLayout, QFileDialog
4
5# you can copy and run this code
6
7class MainWindow(QMainWindow):
8 def __init__(self, parent=None):#--------------
9 super(MainWindow, self).__init__(parent)# |
10 self.setWindowTitle("open file dialog")# |
11# |
12 btn = QPushButton("Open File")# |---- Just initialization
13 layout = QVBoxLayout()# |
14 layout.addWidget(btn)# |
15 widget = QWidget()# |
16 widget.setLayout(layout)# |
17 self.setCentralWidget(widget)#-------------
18
19 btn.clicked.connect(self.open) # connect clicked to self.open()
20 self.show()
21
22 def open(self):
23 path = QFileDialog.getOpenFileName(self, 'Open a file', '',
24 'All Files (*.*)')
25 if path != ('', ''):
26 print("File path : "+ path[0])
27
28if __name__ == "__main__":
29 app = QApplication(sys.argv)
30 window = MainWindow()
31 sys.exit(app.exec_())
1def open_file(self):
2 def open(self):
3 path = QFileDialog.getOpenFileName(self, 'Open a file', '',
4 'All Files (*.*)')
5 if path != ('', ''):
6 print(path[0])
1def open(self):
2 path = QFileDialog.getOpenFileName(self, 'Open a file', '',
3 'All Files (*.*)')
4 if path != ('', ''):
5 print(path[0])