記事本程序:app
# 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_()
說明:ui
1. 下面代碼能夠保證讀取文件中文內容不出現亂碼spa
import codecscode
with codecs.open(self.filename, 'w','utf-8') as fblog
2. 下面代碼能夠保證中文內容寫入不出現亂碼ip
import sysutf-8
reload(sys)get
sys.setdefaultencoding("utf-8")it
3. 下面判斷保證新文件保存時候採起另存爲的方式io
if 'filename' in dir(self):