반응형
/*********************************************************************************************************
-- Title : [PyQt4] Notepad 따라하기
-- Reference : thecodeinn.blogspot.kr opentutorials.org
-- Key word : python pyqt pyqt4 qt qt4 파이썬 프로그래밍 메모장 notepad
*********************************************************************************************************/
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | # -*- coding: utf-8 -*- import sys from PyQt4 import QtGui, QtCore class Window(QtGui.QMainWindow): def __init__(self, parent=None): QtGui.QMainWindow.__init__(self, parent) self.setGeometry(100,100,640,480) # staring points and size of window self.setWindowTitle("Python") self.setWindowIcon(QtGui.QIcon("../img/pythonlogo.png")) # set window icon self.statusBar() self.vSaved = True self.vWindowname = "Python" self.vFilename = "" self.on_init_objects() def on_init_objects(self): # -- Create menubar and set menu. tMainBar = self.menuBar() tFileMenu = tMainBar.addMenu("&File") self.tNewAction = QtGui.QAction(QtGui.QIcon("../img/new.png"), "&New", self) self.tNewAction.triggered.connect(self.on_new_file) self.tNewAction.setStatusTip("Create new file.") self.tFileAction = QtGui.QAction(QtGui.QIcon("../img/open.png"), "&Open", self) self.tFileAction.triggered.connect(self.on_open_file) self.tFileAction.setStatusTip("Open the file.") self.tSaveAction = QtGui.QAction(QtGui.QIcon("../img/save.png"), "&Save", self) self.tSaveAction.triggered.connect(self.on_save_file) self.tSaveAction.setStatusTip("Save the file.") self.tSaveAction.setEnabled(False) self.tQuitAction = QtGui.QAction(QtGui.QIcon("../img/exit.png"), "&Exit", self) self.tQuitAction.triggered.connect(self.on_quit_file) self.tQuitAction.setStatusTip("Quit The Application.") tFileMenu.addAction(self.tNewAction) tFileMenu.addAction(self.tFileAction) tFileMenu.addAction(self.tSaveAction) tFileMenu.addSeparator() tFileMenu.addAction(self.tQuitAction) # -- Create Textedit self.tTextedit = QtGui.QTextEdit(self) self.setCentralWidget(self.tTextedit) self.tTextedit.textChanged.connect(self.on_text_changed) def on_text_changed(self): self.vSaved = False self.tSaveAction.setEnabled(True) def on_new_file(self): if self.vSaved is not True: vAnswer = QtGui.QMessageBox.information(self, "Save", "Save the file that you write?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.Cancel, QtGui.QMessageBox.Yes ) if vAnswer == QtGui.QMessageBox.Yes: self.on_save_file() elif vAnswer == QtGui.QMessageBox.No: return self.tTextedit.clear() self.setWindowTitle(self.vWindowname) self.tSaveAction.setEnabled(False) def on_save_file(self): vSavefilename = QtGui.QFileDialog.getSaveFileName(self, "Save File", filter="*.txt") if vSavefilename == "": return f = open(vSavefilename, "w") vTextstring = self.tTextedit.toPlainText() f.write(vTextstring) f.close() self.vFilename = vSavefilename self.tSaveAction.setEnabled(False) self.setWindowTitle(self.vFilename + ' - ' + self.vWindowname) self.vSaved = True def on_open_file(self): if self.vSaved is not True: vAnswer = QtGui.QMessageBox.information(self, "Save", "Save the file that you write?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.Cancel, QtGui.QMessageBox.Yes ) if vAnswer == QtGui.QMessageBox.Yes: self.on_save_file() return vOpenfilename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', filter="*.txt") if vOpenfilename == "": return f = open(vOpenfilename, "r") vTextstring = f.read() self.tTextedit.setText(vTextstring) f.close() self.vFilename = vOpenfilename self.tSaveAction.setEnabled(False) self.setWindowTitle(self.vFilename + ' - ' + self.vWindowname) self.vSaved = False def on_quit_file(self): self.close() if __name__ == "__main__": app = QtGui.QApplication(sys.argv) gui = Window() gui.show() sys.exit(app.exec_()) | cs |
반응형