기능 구현이 너무 쉽고 간단하다.

전에 만들었던 Simple Text Editor에 기능 몇가지를 추가했다.


- File Close 기능

- Save Button Disable 기능, textBrowser에 변화가 있을 경우 활성화됨

- 파일 변경 후 Save 없이 Close할 경우 메시지박스로 저장할 것인지 확인함




 [ Source ]


- Main.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -*- coding: utf-8 -*-
 
# Form implementation generated from reading ui file 'Main.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
 
from PyQt4 import QtCore, QtGui
 
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s
 
try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)
 
class Ui_notepad(object):
    def setupUi(self, notepad):
        notepad.setObjectName(_fromUtf8("notepad"))
        notepad.resize(802625)
        self.centralwidget = QtGui.QWidget(notepad)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.textBrowser = QtGui.QTextBrowser(self.centralwidget)
        self.textBrowser.setGeometry(QtCore.QRect(1040781541))
        self.textBrowser.setTextInteractionFlags(QtCore.Qt.LinksAccessibleByKeyboard|QtCore.Qt.LinksAccessibleByMouse|QtCore.Qt.TextBrowserInteraction|QtCore.Qt.TextEditable|QtCore.Qt.TextEditorInteraction|QtCore.Qt.TextSelectableByKeyboard|QtCore.Qt.TextSelectableByMouse)
        self.textBrowser.setObjectName(_fromUtf8("textBrowser"))
        self.button_open = QtGui.QPushButton(self.centralwidget)
        self.button_open.setGeometry(QtCore.QRect(10107523))
        self.button_open.setObjectName(_fromUtf8("button_open"))
        self.button_save = QtGui.QPushButton(self.centralwidget)
        self.button_save.setGeometry(QtCore.QRect(90107523))
        self.button_save.setCheckable(False)
        self.button_save.setAutoDefault(False)
        self.button_save.setDefault(False)
        self.button_save.setFlat(False)
        self.button_save.setObjectName(_fromUtf8("button_save"))
        self.button_close = QtGui.QPushButton(self.centralwidget)
        self.button_close.setGeometry(QtCore.QRect(710107523))
        self.button_close.setObjectName(_fromUtf8("button_close"))
        notepad.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(notepad)
        self.menubar.setGeometry(QtCore.QRect(0080221))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        notepad.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(notepad)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        notepad.setStatusBar(self.statusbar)
 
        self.retranslateUi(notepad)
        QtCore.QMetaObject.connectSlotsByName(notepad)
 
    def retranslateUi(self, notepad):
        notepad.setWindowTitle(_translate("notepad""Extended Text Editor", None))
        self.button_open.setText(_translate("notepad""Open", None))
        self.button_save.setText(_translate("notepad""Save", None))
        self.button_close.setText(_translate("notepad""Close", None))
 
 
cs


- Start.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import sys
import codecs
from os.path import isfile
from PyQt4 import QtCore, QtGui
from Main import Ui_notepad
 
class StartQT4(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_notepad()
        self.ui.setupUi(self)
        self.filename = ""
 
        QtCore.QObject.connect(self.ui.button_open, QtCore.SIGNAL("clicked()"), self.file_dialog)
        QtCore.QObject.connect(self.ui.button_save, QtCore.SIGNAL("clicked()"), self.file_save)
        QtCore.QObject.connect(self.ui.textBrowser, QtCore.SIGNAL("textChanged()"), self.enable_save)
        QtCore.QObject.connect(self.ui.button_close, QtCore.SIGNAL("clicked()"), self.file_close)
        self.initalize()
 
    def initalize(self):
        self.ui.textBrowser.setPlainText("")
        self.ui.button_save.setEnabled(False)
        self.setWindowTitle("Extended Text Editor")
 
    def file_dialog(self):
        fd = QtGui.QFileDialog(self)
        self.filename = fd.getOpenFileName()
        if isfile(self.filename):
            s = codecs.open(self.filename, 'r''utf-8').read()
            self.ui.textBrowser.setPlainText(s)
            self.setWindowTitle(self.filename)
            self.ui.button_save.setEnabled(False)
 
    def enable_save(self):
        self.ui.button_save.setEnabled(True)
 
    def file_save(self):
        if isfile(self.filename):
            file = codecs.open(self.filename, 'w''utf-8')
            file.write(unicode(self.ui.textBrowser.toPlainText()))
            file.close()
            self.ui.button_save.setEnabled(False)
 
    def file_close(self):
        if isfile(self.filename):
            # not saved
            if self.ui.button_save.isEnabled():
                message = QtGui.QMessageBox(self)
                message.setText('Would you like to save the file before closing?')
                message.setWindowTitle("Warning")
                message.setIcon(QtGui.QMessageBox.Question)
                message.addButton('Save', QtGui.QMessageBox.AcceptRole)
                message.addButton('Discard', QtGui.QMessageBox.DestructiveRole)
                message.addButton('Cancel', QtGui.QMessageBox.RejectRole)
                message.setDetailedText('Unsaved changes in file: ' + str(self.filename))
                message.exec_()
                response = message.clickedButton().text()
                if response == 'Save':
                    self.file_save()
                    self.ui.button_save.setEnabled(False)
                elif response == 'Discard':
                    self.initalize()
                else:
                    pass
            else:
                self.initalize()
                    
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = StartQT4()
    myapp.show()
    sys.exit(app.exec_())
cs


github : https://github.com/skyclad0x7b7/StudyPyQt4/tree/master/0x02.%20Extended%20Text%20Editor

'Programming' 카테고리의 다른 글

[PyQt4] 0x04. QYolk II  (0) 2016.11.16
[PyQt4] 0x03. QYolk I  (6) 2016.11.15
[PyQt4] 0x02. Extended Text Editor  (0) 2016.11.15
[PyQt4] 0x01. PyQt4 설치, Simple text editor  (0) 2016.11.15
[Go] Defer, Panic, Recovery  (0) 2016.10.09
[DLL Injection] DLL Injector  (0) 2016.10.05
블로그 이미지

__미니__

E-mail : skyclad0x7b7@gmail.com 나와 계약해서 슈퍼 하-카가 되어 주지 않을래?

댓글을 달아 주세요