代码改变世界

python2.7+pyqt4实现记事本基本功能

2017-01-26 11:15  一方书斋  阅读(1415)  评论(0编辑  收藏  举报

记事本程序:

# coding:utf-8
import sys
from PyQt4.QtGui import QMainWindow
from PyQt4.QtGui import QApplication, QAction, QIcon, QFileDialog, QTextEdit
from PyQt4.QtCore import SIGNAL, SLOT
import codecs
import os.path
import sys
reload(sys)
sys.setdefaultencoding("utf-8")


class MyWindow(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        self.setWindowTitle("PyQt")
        self.resize(300, 200)
        self.create_menu_bar()
        self.create_menu_action()
        self.add_menu_action()
        self.create_tool_bar()
        self.add_tool_action()
        self.maintext = QTextEdit()
        self.setCentralWidget(self.maintext)
    
    def create_menu_bar(self):
        menu = self.menuBar()
        self.FileMenu = menu.addMenu(u'文件')
        self.EditMenu = menu.addMenu(u'编辑')
        self.SearchMenu = menu.addMenu(u'搜索')

    def create_menu_action(self):
        self.OpenMenu = QAction(QIcon('images/open.png'),u'打开', self)
        self.OpenMenu.setShortcut('Ctrl+O')
        self.OpenMenu.setStatusTip(u'打开文件')
        self.OpenMenu.connect(self.OpenMenu, SIGNAL('triggered()'), self.open_file)

        self.SaveMenu = QAction(QIcon('images/save.png'), u'保存', self)
        self.SaveMenu.setShortcut('Ctrl+S')
        self.SaveMenu.setStatusTip(u'保存文件')
        self.connect(self.SaveMenu, SIGNAL('triggered()'), self.save_file)

        self.SaveAsMenu = QAction(QIcon('images/saveas.png'), u'另存为', self)
        self.SaveAsMenu.setShortcut('Ctrl+Alt+S')
        self.SaveAsMenu.setStatusTip(u'文件另存为')
        self.connect(self.SaveAsMenu, SIGNAL('triggered()'), self.save_as_file)

        self.ExitMenu = QAction(QIcon('images/exit.png'), u'退出', self)
        self.ExitMenu.setShortcut('Ctrl+Q')
        self.ExitMenu.setStatusTip(u'退出程序')
        self.connect(self.ExitMenu, SIGNAL('triggered()'), SLOT('close()'))

    def add_menu_action(self):
        self.FileMenu.addAction(self.OpenMenu)
        self.FileMenu.addAction(self.SaveMenu)
        self.FileMenu.addAction(self.SaveAsMenu)
        self.FileMenu.addAction(self.ExitMenu)

    def create_tool_bar(self):
        self.Open_tool = self.addToolBar(u'打开')
        self.Save_tool = self.addToolBar(u'保存')
        self.SaveAs_tool = self.addToolBar(u'另存为')
        self.Exit_tool = self.addToolBar(u'退出')

    def add_tool_action(self):
        self.Open_tool.addAction(self.OpenMenu)
        self.Save_tool.addAction(self.SaveMenu)
        self.SaveAs_tool.addAction(self.SaveAsMenu)
        self.Exit_tool.addAction(self.ExitMenu)
       
    def open_file(self):
        self.filename = QFileDialog.getOpenFileName()
        if os.path.isfile(self.filename):
            with codecs.open(self.filename, 'r','utf-8') as f:
                s = f.read()
                self.maintext.setText(s)

    def save_file(self):
        if 'filename' in dir(self):
            with codecs.open(self.filename, 'w','utf-8') as f:
                f.write(self.maintext.toPlainText())
        else:
            self.save_as_file()

    def save_as_file(self):
        self.filename = QFileDialog.getSaveFileName()
        with codecs.open(self.filename, 'w','utf-8') as f:
            f.write(self.maintext.toPlainText())


app = QApplication(sys.argv)
mywindow = MyWindow()
mywindow.show()
app.exec_()

说明:

1. 下面代码可以保证读取文件中文内容不出现乱码

import codecs

with codecs.open(self.filename, 'w','utf-8') as f

2. 下面代码可以保证中文内容写入不出现乱码

import sys

reload(sys)

sys.setdefaultencoding("utf-8")

3. 下面判断保证新文件保存时候采取另存为的方式

if 'filename' in dir(self):