• 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 文件即可使用了.

  • 相关阅读:
    Spring视频学习笔记(一)
    枚举(enum)与类的静态成员(static)
    Java的继承模式
    HTML锁定Table中某一列
    Winform 中的KeyDown
    我的WebService入门
    获取键盘或鼠标多久没有对屏幕进行操作了
    从DB输出值到DataTable时,字段值为NULL时报错相关信息;
    Data层相关问题 & JS循环取值
    字符串String的trim()方法
  • 原文地址:https://www.cnblogs.com/waniu/p/2654428.html
Copyright © 2020-2023  润新知