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


    记事本程序:

    # 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):
    
  • 相关阅读:
    Linux 第一个脚本程序
    Linux 8开启FTP功能服务
    PPT 倒计时时钟,用 GIF 动画实现,可直接使用 -- 附 Python 实现代码
    python flask 虚拟环境迁移
    GOLANG学习之路之二
    Golang学习之路之一
    vscode 调试flask项目 解决(socket.gaierror: [Errno 11001] getaddrinfo failed)
    windows下部署 flask (win10+flask+nginx)
    git入门
    配置maven的国内镜像
  • 原文地址:https://www.cnblogs.com/lypy/p/6351387.html
Copyright © 2020-2023  润新知