PyQT实现多窗口切换


Posted in Python onApril 20, 2018

最近做个软件,用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_()

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

Python 相关文章推荐
在Django中创建动态视图的教程
Jul 15 Python
Python 中开发pattern的string模板(template) 实例详解
Apr 01 Python
利用python模拟sql语句对员工表格进行增删改查
Jul 05 Python
Python模块结构与布局操作方法实例分析
Jul 24 Python
python直接获取API传递回来的参数方法
Dec 17 Python
基于python实现KNN分类算法
Apr 23 Python
对Python的交互模式和直接运行.py文件的区别详解
Jun 29 Python
jupyter lab的目录调整及设置默认浏览器为chrome的方法
Apr 10 Python
python安装cx_Oracle和wxPython的方法
Sep 14 Python
Django mysqlclient安装和使用详解
Sep 17 Python
如何在向量化NumPy数组上进行移动窗口
May 18 Python
python 标准库原理与用法详解之os.path篇
Oct 24 Python
使用实现pandas读取csv文件指定的前几行
Apr 20 #Python
浅析python打包工具distutils、setuptools
Apr 20 #Python
PyQt5每天必学之切换按钮
Aug 20 #Python
PyQt5每天必学之滑块控件QSlider
Apr 20 #Python
PyQt4实现下拉菜单可供选择并打印出来
Apr 20 #Python
PyQt5每天必学之组合框
Apr 20 #Python
python 时间信息“2018-02-04 18:23:35“ 解析成字典形式的结果代码详解
Apr 19 #Python
You might like
星际玩家的三大定律
2020/03/04 星际争霸
LotusPhp笔记之:基于ObjectUtil组件的使用分析
2013/05/06 PHP
利用PHP脚本在Linux下用md5函数加密字符串的方法
2015/06/29 PHP
PHP按指定键值对二维数组进行排序的方法
2015/12/22 PHP
Windows平台实现PHP连接SQL Server2008的方法
2017/07/26 PHP
PHP设计模式之PHP迭代器模式讲解
2019/03/22 PHP
iframe 自适应高度[在IE6 IE7 FF下测试通过]
2009/04/13 Javascript
简单js代码实现selece二级联动(推荐)
2014/02/18 Javascript
Javascript递归打印Document层次关系实例分析
2015/05/15 Javascript
jquery中键盘事件小结
2016/02/24 Javascript
AngularJs bootstrap详解及示例代码
2016/09/01 Javascript
在vue项目中使用Nprogress.js进度条的方法
2018/01/31 Javascript
vue+element的表格实现批量删除功能示例代码
2018/08/17 Javascript
详解关于Vue单元测试的几个坑
2020/04/26 Javascript
[02:33]DOTA2英雄基础教程 司夜刺客
2013/12/04 DOTA
[54:41]2018DOTA2亚洲邀请赛3月30日 小组赛B组 VGJ.T VS paiN
2018/03/31 DOTA
[01:09:23]KG vs TNC 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
使用Pyrex来扩展和加速Python程序的教程
2015/04/13 Python
Python中encode()方法的使用简介
2015/05/18 Python
python如何为被装饰的函数保留元数据
2018/03/21 Python
《与孩子一起学编程》python自测题
2018/05/27 Python
解决pycharm安装后代码区不能编辑的问题
2018/10/28 Python
只需7行Python代码玩转微信自动聊天
2019/01/27 Python
Python实现生成密码字典的方法示例
2019/09/02 Python
线程安全及Python中的GIL原理分析
2019/10/29 Python
Spring http服务远程调用实现过程解析
2020/06/11 Python
详解HTML5常用的语义化标签
2019/09/27 HTML / CSS
世界上最大的折扣香水店:FragranceNet.com
2016/10/26 全球购物
欧舒丹美国官网:L’Occitane美国
2018/02/23 全球购物
Tea Collection官网:一家位于旧金山的童装公司
2020/08/07 全球购物
北京银河万佳Java面试题
2012/03/21 面试题
外语系毕业生求职自荐信
2014/04/12 职场文书
机房搬迁方案
2014/05/01 职场文书
幼儿园亲子活动通知
2015/04/24 职场文书
2015年办公室文秘工作总结
2015/04/30 职场文书
创业计划书之密室逃脱
2019/11/08 职场文书