pyqt4原始例程--调用QtDesigner生成的子函数

参考http://www.rkblog.rk.edu.pl/w/p/introduction-pyqt4/

1.PyQt4的设置与使用

2.qt designer生成的代码

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'button_one.ui'
#
# Created: Wed Apr 26 10:59:32 2017
#      by: PyQt4 UI code generator 4.10
#
# WARNING! All changes made in this file will be lost!

#------------------------------------------开头默认添加---------------------------------
from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)
#=========================================END========================================
    
class Ui_notepad(object):#窗口notepad内的所有内容
    
    def setupUi(self, notepad):#添加控件
        notepad.setObjectName(_fromUtf8("notepad"))#notepad是窗口的对象名objectname(默认对象名为Form)
        notepad.resize(447, 371)#窗口notepad的面积大小
        
        self.button_open = QtGui.QPushButton(notepad)#声明按键在窗口里面
        self.button_open.setGeometry(QtCore.QRect(80, 60, 111, 21))#前两位是坐标,后两位是大小
        self.button_open.setObjectName(_fromUtf8("button_open"))#button_open是窗口的对象名objectname(默认对象名为pushButton)
        self.button_close = QtGui.QPushButton(notepad)
        self.button_close.setGeometry(QtCore.QRect(230, 60, 101, 21))#前两位是坐标,后两位是大小
        self.button_close.setObjectName(_fromUtf8("button_close"))#button_open是窗口的对象名objectname(默认对象名为pushButton)

        self.editor_window = QtGui.QTextEdit(notepad)#声明按键在窗口里面
        self.editor_window.setGeometry(QtCore.QRect(80, 120, 251, 111))#前两位是坐标,后两位是大小
        self.editor_window.setObjectName(_fromUtf8("editor_window"))#editor_window是窗口的对象名objectname(默认对象名为textEdit)

        #控件间的关联操作
        self.retranslateUi(notepad)
        QtCore.QObject.connect(self.button_close, QtCore.SIGNAL(_fromUtf8("clicked()")), notepad.close)#按钮button_close点击(clicked),窗口notepad关闭
        QtCore.QObject.connect(self.button_open, QtCore.SIGNAL(_fromUtf8("clicked()")), self.editor_window.close)#按钮button_open点击(clicked),窗口notepad显示
        QtCore.QMetaObject.connectSlotsByName(notepad)

    def retranslateUi(self, notepad):#控件的text取名模块
        notepad.setWindowTitle(_translate("notepad", "波形发生器", None))#窗口显示的文本text为"波形发生器"
        self.button_open.setText(_translate("notepad", "打开", None))#按键显示的文本text为"打开"
        self.button_close.setText(_translate("notepad", "关闭", None))#按键显示的文本text为"关闭"


3.创建主文件,调用Qt Designer生成的子代码


# -*- coding: utf-8 -*-
#button_one.py文件的main文件

import sys
from PyQt4 import QtCore, QtGui
from button_one import Ui_notepad #引用Qt design生成的代码
#Ui_Form是用pyuic4工具从"Form"窗口生成的对应python类的名字。
#你可以在QtDesigner自己喜欢的名字一个类的名字(下一节我们会讲到)。

class My_Form(QtGui.QMainWindow):#My_Form可以修改,需要把下面的一同修改
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_notepad()#此处修改
        self.ui.setupUi(self)
        
if __name__ == "__main__":#主程序
    app = QtGui.QApplication(sys.argv)
    
    myapp = My_Form()#My_Form可以修改
    myapp.show()
    sys.exit(app.exec_())


4.运行结果。点击打开,文本框关闭;点击关闭,窗口关闭


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值