zoukankan      html  css  js  c++  java
  • PyQt入门,一个简单的文本编辑器

    使用QtDesigner创建UI界面

    如图,给关闭按钮添加关闭操作/

    form命名为:notepad

    打开按钮命名为:button_open

    保存按钮命名为:button_save

    文件名保存为notepad.ui

    2.使用pyuic编译ui文件为py文件 :

      pyuic4 notepad.ui > notepad.py 

      编译后为打开文件notepad.py,代码为:

    # -*- coding: utf-8 -*-
    
    # Form implementation generated from reading ui file 'C:\Users\Administrator\Desktop\testqt\edytor.ui'
    #
    # Created: Fri Aug 24 15:24:55 2012
    #      by: PyQt4 UI code generator 4.9.1
    #
    # WARNING! All changes made in this file will be lost!
    
    from PyQt4 import QtCore, QtGui
    
    try:
        _fromUtf8 = QtCore.QString.fromUtf8
    except AttributeError:
        _fromUtf8 = lambda s: s
    
    class Ui_notepad(object):
        def setupUi(self, notepad):
        
            notepad.setObjectName(_fromUtf8("notepad"))
            notepad.resize(519, 372)
            
            self.button_open = QtGui.QPushButton(notepad)
            self.button_open.setGeometry(QtCore.QRect(80, 30, 111, 23))
            self.button_open.setObjectName(_fromUtf8("button_open"))
            
            self.pushButton_2 = QtGui.QPushButton(notepad)
            self.pushButton_2.setGeometry(QtCore.QRect(330, 30, 141, 23))
            self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
            
            self.editor_window = QtGui.QTextEdit(notepad)
            self.editor_window.setGeometry(QtCore.QRect(80, 80, 391, 211))
            self.editor_window.setObjectName(_fromUtf8("editor_window"))
            
            self.button_save = QtGui.QPushButton(notepad)
            self.button_save.setGeometry(QtCore.QRect(210, 30, 101, 21))
            self.button_save.setObjectName(_fromUtf8("button_save"))
    
            self.retranslateUi(notepad)
            QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL(_fromUtf8("clicked()")), notepad.close)
            QtCore.QMetaObject.connectSlotsByName(notepad)
    
        def retranslateUi(self, notepad):
            notepad.setWindowTitle(QtGui.QApplication.translate("notepad", "Form", None, QtGui.QApplication.UnicodeUTF8))
            self.button_open.setText(QtGui.QApplication.translate("notepad", "打开", None, QtGui.QApplication.UnicodeUTF8))
            self.pushButton_2.setText(QtGui.QApplication.translate("notepad", "关闭", None, QtGui.QApplication.UnicodeUTF8))
            self.button_save.setText(QtGui.QApplication.translate("notepad", "保存", None, QtGui.QApplication.UnicodeUTF8))

    3. 写调用和UI的操作,建立test.py文件

    test.py:

    #--*-- coding:utf-8--
    
    import sys
    from PyQt4 import QtCore, QtGui 
    from edytor 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) 
            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) 
    
        def file_save(self):   #保存文件
            from os.path import isfile 
            if isfile(self.filename): 
                file = open(self.filename,'w') 
                file.write(self.ui.editor_window.toPlainText()) 
                file.close() 
    
        def file_dialog(self):  #打开文件操作
            fd = QtGui.QFileDialog(self) 
            self.filename = fd.getOpenFileName() 
            from os.path import isfile 
            if isfile(self.filename): 
                text = open(self.filename).read() 
                self.ui.editor_window.setText(text) 
    
    if __name__ == "__main__": 
        app = QtGui.QApplication(sys.argv) 
        myapp = StartQt4() 
        myapp.show() 
        sys.exit(app.exec_()) 
        
        



    4. 运行test.py 文件即可使用了.

  • 相关阅读:
    SQLServer 2008(R2)如何开启数据库的远程连接
    SQLServer 中的身份验证及登录问题
    Oracle 远程链接oracle数据库服务器的配置
    nmon 及nmon analyser工具使用简介
    排错-tcpreplay回放错误:send() [218] Message too long (errno = 90)
    Linux  释放Linux 系统预留的硬盘空间
    python 全栈开发,Day126(创业故事,软件部需求,内容采集,显示内容图文列表,MongoDB数据导入导出JSON)
    python 全栈开发,Day125(HTML5+ 初识,HBuilder,夜神模拟器,Webview)
    python 全栈开发,Day124(MongoDB初识,增删改查操作,数据类型,$关键字以及$修改器,"$"的奇妙用法,Array Object 的特殊操作,选取跳过排序,客户端操作)
    python 全栈开发,Day123(图灵机器人,web录音实现自动化交互问答)
  • 原文地址:https://www.cnblogs.com/waniu/p/2654428.html
Copyright © 2011-2022 走看看