PyQt4 模擬記事本基本功能(儲存,開啟檔案)

阿里瓜瓜發表於2017-05-05

image

完成功能:

1. 預設【儲存】按鈕enable

2. 修改文字的內容後,【enable】

3. 解決字元亂碼問題:utf-8

4. 提示:如果修改了檔案沒有儲存的時候,又嘗試開啟新的檔案,給出相關的提示資訊

操作步驟:

1. 在Designer中設計,生產**.ui檔案
2. 切換到.ui檔案所在目錄,執行如下命令,生成.py檔案
pyuic4 **.ui > **.py

# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui
from edytor import Ui_notepad
from os.path import isfile
import codecs

class StartQt4(QtGui.QMainWindow):
    def __init__(self, parent=None, flags=0):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_notepad()
        self.ui.setupUi(self)
        ''' click點選 button_open 執行file_dialog'''
        QtCore.QObject.connect(self.ui.button_open, QtCore.SIGNAL("clicked()"), self.file_dialog)
        ''' click點選 button_save 執行file_save儲存'''
        QtCore.QObject.connect(self.ui.button_save, QtCore.SIGNAL("clicked()"), self.file_save)
        ''' editor_window的內容傳送改變時,執行enable_save函式,enable = true'''
        QtCore.QObject.connect(self.ui.editor_window, QtCore.SIGNAL("textChanged()"),self.enable_save )
        
    def file_dialog(self):       
        response = False
        # buttton texts
        SAVE = 'Save'
        DISCARD = 'Discard'
        CANCEL =  'Cancel'
        
        # if we have changes then ask about them
        if self.ui.button_save.isEnabled() and self.filename:
            message = QtGui.QMessageBox(self)
            message.setText('What to do about unsaved changes ?')
            message.setWindowTitle('Notpad')
            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()
            
        # save file
        if response == SAVE:
            self.file_save()
            self.ui.button_save.setEnabled(False)
            
        # descard changes 
        elif response == DISCARD:
            self.ui.button_save.setEnabled(False)
            
        # if we didn't cancelled show the file dialogue
        
        if response != CANCEL:    
            ''' 使用QFileDialog來選擇檔案'''
            fd = QtGui.QFileDialog(self) 
            ''' 使用getOpenFileName()彈出一個檔案選擇框
            fd.getOpenFileName()用於返回我們選擇檔案的名字。如果沒有選擇檔案的話,會得打一個空的檔名
            '''
            self.filename = fd.getOpenFileName()
            if isfile(self.filename):
                text = codecs.open(self.filename, 'r', 'utf-8').read()
                self.ui.editor_window.setPlainText(text)    
                self.ui.button_save.setEnabled(False)
            '''文字沒有被儲存的時候,顯示提示訊息 —— 通過save是否被禁用得到文字是否被儲存'''
     
                  
    def file_save(self):
        if isfile(self.filename):
            file = codecs.open(self.filename, 'w', 'utf-8')
            file.write(unicode(self.ui.editor_window.toPlainText()))
            file.close()  
            self.ui.button_save.setEnabled(False)
           
    def enable_save(self):
        self.ui.button_save.setEnabled(True)
        
           
if __name__ == "__main__":
     app = QtGui.QApplication(sys.argv)
     myapp = StartQt4()
     myapp.show()
     sys.exit(app.exec_())

相關文章