PyQT实现多窗口切换

 更新时间:2018年04月20日 10:05:59   作者:sollor525  
这篇文章主要为大家详细介绍了PyQT实现多窗口切换的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

最近做个软件,用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 elasticsearch环境搭建详解

    python elasticsearch环境搭建详解

    在本篇文章里小编给大家整理的是关于python elasticsearch环境搭建的相关知识点内容,有需要的朋友们可以参考下。
    2019-09-09
  • 使用pandas实现筛选出指定列值所对应的行

    使用pandas实现筛选出指定列值所对应的行

    这篇文章主要介绍了使用pandas实现筛选出指定列值所对应的行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • Python内建函数Built_in Funtions用法示例详解

    Python内建函数Built_in Funtions用法示例详解

    这篇文章主要为大家介绍了Python内建函数Built_in Funtions用法示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • 对Python 除法负数取商的取整方式详解

    对Python 除法负数取商的取整方式详解

    今天小编就为大家分享一篇对Python 除法负数取商的取整方式详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-12-12
  • python 去除txt文本中的空格、数字、特定字母等方法

    python 去除txt文本中的空格、数字、特定字母等方法

    今天小编就为大家分享一篇python 去除txt文本中的空格、数字、特定字母等方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • Python OpenCV处理图像之图像像素点操作

    Python OpenCV处理图像之图像像素点操作

    这篇文章主要为大家详细介绍了Python OpenCV处理图像之图像像素点操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • python使用opencv读取图片的实例

    python使用opencv读取图片的实例

    下面小编就为大家带来一篇python使用opencv读取图片的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • Python装饰器重载内置的使用

    Python装饰器重载内置的使用

    本文主要介绍了Python装饰器重载内置的使用,详细介绍如何创建装饰器,如何使用装饰器来重载内置函数,具有一定的参考价值,感兴趣的可以了解一下
    2024-01-01
  • Python图像处理之使用OpenCV检测对象颜色

    Python图像处理之使用OpenCV检测对象颜色

    OpenCV颜色检测只是一个起点,最终目标是最终使用Python 3代码在视频流帧中定位彩色元素位置,下面这篇文章主要给大家介绍了关于Python图像处理之使用OpenCV检测对象颜色的相关资料,需要的朋友可以参考下
    2022-12-12
  • 将labelme格式数据转化为标准的coco数据集格式方式

    将labelme格式数据转化为标准的coco数据集格式方式

    今天小编就为大家分享一篇将labelme格式数据转化为标准的coco数据集格式方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02

最新评论