Python ÇÁ·Î±×·¡¹Ö > PyQt5 - QtWidgets - QFileDialog (ÆÄÀÏ ´ÙÀ̾ó·Î±×)
µî·ÏÀÏ : 2018-07-11 15:56
Á¶È¸¼ö : 52,670
À̹ø ¿¹Á¦´Â ¸ÞÀθ޴º¿¡¼ '¿±â' ¸Þ´º¸¦ ³Ö¾úÀ»¶§ ÆÄÀÏ ´ÙÀ̾ó·Î±×¸¦ ¶ç¿ö¼ ÆÄÀϼ±Åà ÇÑÈÄ ³»¿ëÀ» textedit ¿¡ Ç¥½ÃÇÏ´Â ¿¹Á¦¸¦ º¸µµ·Ï ÇÏ°Ú½À´Ï´Ù.
# -*- coding: utf-8 -*-
import sys
import codecs
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QMenuBar, QAction, QTextEdit
from PyQt5.QtGui import QIcon
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
fileAction = QAction(QIcon("refresh.png"), "&Open", self)
fileAction.setShortcut('Ctrl+O')
fileAction.triggered.connect(self.showDialog)
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(fileAction)
self.textedit = QTextEdit(self)
self.setCentralWidget(self.textedit)
self.statusBar()
self.setGeometry(300,300,300,300)
self.setWindowTitle('File Window')
self.show()
def showDialog(self, e):
filepath = QFileDialog.getOpenFileName(self, 'Open File', 'd:')
if filepath[0]:
# ÀÏ¹Ý open À¸·Î ¿¾úÀ» °æ¿ì ¾Æ·¡¿Í °°Àº ÀÎÄÚµù ¿¡·¯°¡ ³ª¿Â´Ù
# UnicodeDecodeError: 'cp949' codec can't decode bytes in position
fp = codecs.open(filepath[0], 'r', 'utf-8')
with fp:
buf = fp.read()
self.textedit.setText(buf)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())