Created
September 4, 2015 12:17
-
-
Save saleph/2079fe1e9f6c97e1557c to your computer and use it in GitHub Desktop.
[qt5] file dialog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from PyQt5.QtWidgets import (QMainWindow, QTextEdit, | |
QAction, QFileDialog, QApplication) | |
from PyQt5.QtGui import QIcon | |
class Example(QMainWindow): | |
def __init__(self): | |
super().__init__() | |
self.init_ui() | |
def init_ui(self): | |
self.text_edit = QTextEdit() | |
self.setCentralWidget(self.text_edit) | |
self.statusBar() | |
open_file = QAction(QIcon('open.png'), '&Open', self) | |
open_file.setShortcut('Ctrl+O') | |
open_file.setStatusTip('Open new file') | |
open_file.triggered.connect(self.show_dialog) | |
menu_bar = self.menuBar() | |
file_menu = menu_bar.addMenu('&File') | |
file_menu.addAction(open_file) | |
self.setGeometry(300, 300, 350, 300) | |
self.setWindowTitle('File dialog') | |
self.show() | |
def show_dialog(self): | |
file_name = QFileDialog.getOpenFileName(self, 'Open file', '/home')[0] | |
f = open(str(file_name), 'r') | |
with f: | |
data = f.read() | |
self.text_edit.setText(data) | |
if __name__ == "__main__": | |
app = QApplication(sys.argv) | |
ex = Example() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment