鱼C论坛

 找回密码
 立即注册
查看: 2353|回复: 1

[技术交流] PyQt4 designer 设计界面,python调用(原创)

[复制链接]
发表于 2016-11-12 15:53:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 wuyaqi45 于 2016-11-12 15:55 编辑

        我看了不少书,没有涉及到designer设计的ui文件,如何在自己的python中调用窗口或按钮,这个是调用按钮的。

   
在设计里面选择widget,,再拖出按钮,
1.PNG
2.PNG 3.PNG

  1. # -*- coding: utf-8 -*-

  2. # Form implementation generated from reading ui file 'untitled.ui'
  3. #
  4. # Created by: PyQt4 UI code generator 4.11.4
  5. #
  6. # WARNING! All changes made in this file will be lost!

  7. from PyQt4 import QtCore, QtGui

  8. try:
  9.     _fromUtf8 = QtCore.QString.fromUtf8
  10. except AttributeError:
  11.     def _fromUtf8(s):
  12.         return s

  13. try:
  14.     _encoding = QtGui.QApplication.UnicodeUTF8
  15.     def _translate(context, text, disambig):
  16.         return QtGui.QApplication.translate(context, text, disambig, _encoding)
  17. except AttributeError:
  18.     def _translate(context, text, disambig):
  19.         return QtGui.QApplication.translate(context, text, disambig)

  20. class Ui_Form(object):
  21.     def setupUi(self, Form):
  22.         Form.setObjectName(_fromUtf8("Form"))
  23.         Form.resize(400, 300)
  24.         self.pushButton = QtGui.QPushButton(Form)
  25.         self.pushButton.setGeometry(QtCore.QRect(160, 210, 75, 23))
  26.         self.pushButton.setObjectName(_fromUtf8("pushButton"))

  27.         self.retranslateUi(Form)
  28.         QtCore.QMetaObject.connectSlotsByName(Form)

  29.     def retranslateUi(self, Form):
  30.         Form.setWindowTitle(_translate("Form", "Form", None))
  31.         self.pushButton.setText(_translate("Form", "PushButton", None))

复制代码


生成代码如上,self.pushButton = QtGui.QPushButton(Form),这个就是我们后面需要调用的按钮。、

python调用按钮如下。
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sat Nov 12 15:18:31 2016

  4. @author: Administrator
  5. """

  6. from PyQt4.QtGui import *
  7. from PyQt4.QtCore import *

  8. import sys
  9. import untitled

  10. class TestDialog(QDialog,untitled.Ui_Form):
  11.         def __init__(self,parent=None):
  12.                 super(TestDialog,self).__init__(parent)
  13.                 self.setupUi(self)
  14.                
  15.                            
  16.                 self.pushButton.clicked.connect(self.slotChild)
  17.         def slotChild(self):
  18.                 print ('hello')
  19.        
  20. app = QApplication(sys.argv)
  21. dialog=TestDialog()
  22. dialog.show()

  23. app.exec_()
复制代码

import untitled

self.pushButton.clicked.connect(self.slotChild)
        def slotChild(self):
                print ('hello')

这行是自己添加的。其他的可以复制使用,运行正常,有图有真相 4.PNG
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-11-12 18:08:49 | 显示全部楼层
PyQt4 designer checkBox使用

1.PNG
2.PNG

第一图,先拖入checkBox,和标签

第二图,给checkBox添加触发信号。生成程序里面就是这个“QtCore.QObject.connect(self.checkBox, QtCore.SIGNAL(_fromUtf8("clicked()")), Form.slotcheck)”

生成的代码如下
  1. # -*- coding: utf-8 -*-

  2. # Form implementation generated from reading ui file 'untitled.ui'
  3. #
  4. # Created by: PyQt4 UI code generator 4.11.4
  5. #
  6. # WARNING! All changes made in this file will be lost!

  7. from PyQt4 import QtCore, QtGui

  8. try:
  9.     _fromUtf8 = QtCore.QString.fromUtf8
  10. except AttributeError:
  11.     def _fromUtf8(s):
  12.         return s

  13. try:
  14.     _encoding = QtGui.QApplication.UnicodeUTF8
  15.     def _translate(context, text, disambig):
  16.         return QtGui.QApplication.translate(context, text, disambig, _encoding)
  17. except AttributeError:
  18.     def _translate(context, text, disambig):
  19.         return QtGui.QApplication.translate(context, text, disambig)

  20. class Ui_Form(object):
  21.     def setupUi(self, Form):
  22.         Form.setObjectName(_fromUtf8("Form"))
  23.         Form.resize(400, 300)
  24.         self.checkBox = QtGui.QCheckBox(Form)
  25.         self.checkBox.setGeometry(QtCore.QRect(190, 170, 71, 20))
  26.         self.checkBox.setObjectName(_fromUtf8("checkBox"))
  27.         self.label = QtGui.QLabel(Form)
  28.         self.label.setGeometry(QtCore.QRect(200, 130, 54, 12))
  29.         self.label.setObjectName(_fromUtf8("label"))

  30.         self.retranslateUi(Form)
  31.         QtCore.QObject.connect(self.checkBox, QtCore.SIGNAL(_fromUtf8("clicked()")), Form.slotcheck)
  32.         QtCore.QMetaObject.connectSlotsByName(Form)

  33.     def retranslateUi(self, Form):
  34.         Form.setWindowTitle(_translate("Form", "Form", None))
  35.         self.checkBox.setText(_translate("Form", "CheckBox", None))
  36.         self.label.setText(_translate("Form", "True", None))

复制代码

  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sat Nov 12 15:18:31 2016

  4. @author: Administrator
  5. """

  6. from PyQt4.QtGui import *
  7. from PyQt4.QtCore import *

  8. import sys
  9. import untitled

  10. class TestDialog(QDialog,untitled.Ui_Form):
  11.         def __init__(self,parent=None):
  12.                 super(TestDialog,self).__init__(parent)
  13.                 self.setupUi(self)
  14.                
  15.         def slotcheck(self):
  16.                 if self.checkBox.isChecked():
  17.                         self.label.setText("hello")
  18.                 else:
  19.                         self.label.setText("world")

  20.   
  21.   
  22.        
  23.        
  24. app = QApplication(sys.argv)
  25. dialog=TestDialog()
  26. dialog.show()

  27. app.exec_()
复制代码


调用的代码如上,这个就是对应的槽函数,里面就是一些信号响应后的操作。
  1. def slotcheck(self):
  2.                 if self.checkBox.isChecked():
  3.                         self.label.setText("hello")
  4.                 else:
  5.                         self.label.setText("world")
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 03:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表