侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

PyQT实现多窗口切换

Python  /  管理员 发布于 5年前   610

最近做个软件,用PyQT写的,在实现菜单栏点击弹出新窗口的时候严重被卡壳,发现用WxPython的思想和方式来做完全无法实现。PyQT的中文资料实在是太少了。看了点英文资料和QT的资料,逆推PyQT的实现方法,总算搞定。下面是一个小demo。

主界面的代码如下所示:

# -*- coding: utf-8 -*-  from PyQt4 import QtCore, QtGui from dialog1 import Dialog1 from dialog2 import Dialog2 import sys  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)    class MainWindow(QtGui.QWidget):       dialog1_signal = QtCore.pyqtSignal()     #定义一个无参数的信号,串口设定与子站初始化信号   dialog2_signal = QtCore.pyqtSignal()     #定义一个无参数的信号,串口设定与子站初始化信号   exit_signal = QtCore.pyqtSignal()     #定义一个无参数的信号,串口设定与子站初始化信号      def __init__(self):     super(MainWindow,self).__init__()        def setupUi(self, Form):     Form.setObjectName(_fromUtf8("Form"))     Form.resize(400, 300)     self.form = Form     self.pushButton = QtGui.QPushButton(Form)     self.pushButton.setGeometry(QtCore.QRect(70, 90, 75, 23))     self.pushButton.setObjectName(_fromUtf8("pushButton"))     self.pushButton_2 = QtGui.QPushButton(Form)     self.pushButton_2.setGeometry(QtCore.QRect(240, 90, 75, 23))     self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))     self.pushButton_3 = QtGui.QPushButton(Form)     self.pushButton_3.setGeometry(QtCore.QRect(150, 160, 75, 23))     self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))     self.label = QtGui.QLabel(Form)     self.label.setGeometry(QtCore.QRect(170, 40, 54, 16))     self.label.setObjectName(_fromUtf8("label"))      self.retranslateUi(Form)     QtCore.QMetaObject.connectSlotsByName(Form)      #信号连接到指定槽     self.pushButton.clicked.connect(self.on_pushButton_clicked)     self.pushButton_2.clicked.connect(self.on_pushButton_2_clicked)     self.pushButton_3.clicked.connect(self.on_pushButton_3_clicked) def retranslateUi(self, Form):     Form.setWindowTitle(_translate("Form", "Form", None))     self.pushButton.setText(_translate("Form", "进入dialog1", None))     self.pushButton_2.setText(_translate("Form", "进入dialog2", None))     self.pushButton_3.setText(_translate("Form", "退出", None))     self.label.setText(_translate("Form", "主窗体", None))        def on_pushButton_clicked(self):     self.form.hide()     Form1 = QtGui.QDialog()     ui = Dialog1()     ui.setupUi(Form1)     Form1.show()     Form1.exec_()     self.form.show()    def on_pushButton_3_clicked(self, Form):     #QtCore.QObject.connect( self.pushButton_3, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT(quit()))     #也可以这样     self.form.close()        def on_pushButton_2_clicked(self):     self.form.close()     Form1 = QtGui.QDialog()     ui = Dialog2()     ui.setupUi(Form1)     Form1.show()     Form1.exec_()     self.form.show()  if __name__ == '__main__':   app = QtGui.QApplication(sys.argv)   Form = QtGui.QWidget()   window = MainWindow()    window.setupUi(Form)   Form.show()     sys.exit(app.exec_())       pass 

Dialog1界面的代码如下:

# -*- coding: utf-8 -*-  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)   class Dialog1(QtGui.QWidget):   def setupUi(self, Dialog):     Dialog.setObjectName(_fromUtf8("Dialog"))     Dialog.resize(400, 300)     self.form = Dialog     self.label = QtGui.QLabel(Dialog)     self.label.setGeometry(QtCore.QRect(180, 50, 54, 12))     self.label.setObjectName(_fromUtf8("label"))     self.dialog1_pushButton = QtGui.QPushButton(Dialog)     self.dialog1_pushButton.setGeometry(QtCore.QRect(160, 130, 75, 23))     self.dialog1_pushButton.setObjectName(_fromUtf8("pushButton"))      self.retranslateUi(Dialog)     QtCore.QMetaObject.connectSlotsByName(Dialog)      #信号连接到指定槽     self.dialog1_pushButton.clicked.connect(self.on_dialog1_pushButton_clicked)        def retranslateUi(self, Dialog):     Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))     self.label.setText(_translate("Dialog", "dialog1", None))     self.dialog1_pushButton.setText(_translate("Dialog", "返回主窗体", None))    def on_dialog1_pushButton_clicked(self):     self.form.close()  if __name__ == "__main__":   import sys   app = QtGui.QApplication(sys.argv)   Dialog = QtGui.QDialog()   ui = Dialog1()   ui.setupUi(Dialog)   Dialog.show()   sys.exit(app.exec_())    Dialog2界面的代码如下:[python] view plain copy# -*- coding: utf-8 -*-  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)   class Dialog2(object):   def setupUi(self, Dialog):     Dialog.setObjectName(_fromUtf8("Dialog"))     Dialog.resize(400, 300)     self.form = Dialog     self.label = QtGui.QLabel(Dialog)     self.label.setGeometry(QtCore.QRect(180, 60, 54, 12))     self.label.setObjectName(_fromUtf8("label"))     self.pushButton = QtGui.QPushButton(Dialog)     self.pushButton.setGeometry(QtCore.QRect(160, 140, 75, 23))     self.pushButton.setObjectName(_fromUtf8("pushButton"))      self.retranslateUi(Dialog)     QtCore.QMetaObject.connectSlotsByName(Dialog)          #信号连接到指定槽     self.pushButton.clicked.connect(self.on_pushButton_clicked)        def retranslateUi(self, Dialog):     Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))     self.label.setText(_translate("Dialog", "dialog2", None))     self.pushButton.setText(_translate("Dialog", "返回主窗体", None))        def on_pushButton_clicked(self):     self.form .close()  if __name__ == "__main__":   import sys   app = QtGui.QApplication(sys.argv)   Dialog = QtGui.QDialog()   ui = Dialog2()   ui.setupUi(Dialog)   Dialog.show()   sys.exit(app.exec_()) 

按钮绑定到新弹出界面的处理函数,使用的槽连接方式为:

self.pushButton.clicked.connect(self.on_pushButton_clicked) 

如果是Menu项绑定到新弹出界面的处理函数,则应使用的槽连接方式为:

QtCore.QObject.connect(self.set_value_menu, QtCore.SIGNAL(_fromUtf8("triggered()")), self.open_set_value_form) 

二者使用的槽处理函数基本一致。
若不显示原界面,只需将原界面hide()即可,如:

self.form.hide() 

若需在弹出新窗口时同时原窗口保持可见,则不需这一步。且在这种情况下,若要原窗口可选为顶层窗体,则在显示新窗口时应使用show():

Form1.show() 

若新窗口为固定的顶层窗体,原窗体被遮盖,则应使用exec_():

Form1.exec_() 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    PyQt5每天必学之事件与信号
    下一条:
    PyQt5每天必学之切换按钮
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客