欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PyQT實(shí)現(xiàn)多窗口切換

 更新時(shí)間:2018年04月20日 10:05:59   作者:sollor525  
這篇文章主要為大家詳細(xì)介紹了PyQT實(shí)現(xiàn)多窗口切換的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

最近做個(gè)軟件,用PyQT寫的,在實(shí)現(xiàn)菜單欄點(diǎn)擊彈出新窗口的時(shí)候嚴(yán)重被卡殼,發(fā)現(xiàn)用WxPython的思想和方式來做完全無法實(shí)現(xiàn)。PyQT的中文資料實(shí)在是太少了??戳它c(diǎn)英文資料和QT的資料,逆推PyQT的實(shí)現(xiàn)方法,總算搞定。下面是一個(gè)小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()     #定義一個(gè)無參數(shù)的信號(hào),串口設(shè)定與子站初始化信號(hào) 
  dialog2_signal = QtCore.pyqtSignal()     #定義一個(gè)無參數(shù)的信號(hào),串口設(shè)定與子站初始化信號(hào) 
  exit_signal = QtCore.pyqtSignal()     #定義一個(gè)無參數(shù)的信號(hào),串口設(shè)定與子站初始化信號(hào) 
   
  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) 
 
    #信號(hào)連接到指定槽 
    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", "進(jìn)入dialog1", None)) 
    self.pushButton_2.setText(_translate("Form", "進(jìn)入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) 
 
    #信號(hào)連接到指定槽 
    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) 
     
    #信號(hào)連接到指定槽 
    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_()) 

按鈕綁定到新彈出界面的處理函數(shù),使用的槽連接方式為:

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

如果是Menu項(xiàng)綁定到新彈出界面的處理函數(shù),則應(yīng)使用的槽連接方式為:

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

二者使用的槽處理函數(shù)基本一致。
若不顯示原界面,只需將原界面hide()即可,如:

self.form.hide() 

若需在彈出新窗口時(shí)同時(shí)原窗口保持可見,則不需這一步。且在這種情況下,若要原窗口可選為頂層窗體,則在顯示新窗口時(shí)應(yīng)使用show():

Form1.show() 

若新窗口為固定的頂層窗體,原窗體被遮蓋,則應(yīng)使用exec_():

Form1.exec_() 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python的Django框架安裝全攻略

    Python的Django框架安裝全攻略

    這篇文章主要介紹了Python的Django框架安裝全攻略,其中包括Trunk版本的安裝方法,是上手Django的超給力教程!需要的朋友可以參考下
    2015-07-07
  • python中try的使用方法詳解

    python中try的使用方法詳解

    這篇文章主要給大家介紹了關(guān)于python中try使用的相關(guān)資料,try語句用于包含一段可能引發(fā)異常的代碼塊,并允許我們定義如何處理這些異常,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-10-10
  • Python實(shí)現(xiàn)的堆排序算法原理與用法實(shí)例分析

    Python實(shí)現(xiàn)的堆排序算法原理與用法實(shí)例分析

    這篇文章主要介紹了Python實(shí)現(xiàn)的堆排序算法,簡(jiǎn)單描述了堆排序的原理,并結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)堆排序的相關(guān)操作技巧,代碼中備有較為詳細(xì)的注釋便于理解,需要的朋友可以參考下
    2017-11-11
  • Python和Pycharm 環(huán)境部署詳細(xì)步驟

    Python和Pycharm 環(huán)境部署詳細(xì)步驟

    Python環(huán)境搭建過程很多朋友都操作過,本次我們將向大家介紹Python和Pycharm 環(huán)境部署的流程,文章通過圖文的形式給大家展示一目了然一看就懂,需要的朋友參考下吧
    2021-06-06
  • Python讓列表逆序排列的3種方式小結(jié)

    Python讓列表逆序排列的3種方式小結(jié)

    這篇文章主要介紹了Python讓列表逆序排列的3種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Jetson?NX?配置?pytorch的問題及解決方法

    Jetson?NX?配置?pytorch的問題及解決方法

    這篇文章主要介紹了Jetson?NX?配置?pytorch的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03
  • Python實(shí)現(xiàn)的簡(jiǎn)單讀寫csv文件操作示例

    Python實(shí)現(xiàn)的簡(jiǎn)單讀寫csv文件操作示例

    這篇文章主要介紹了Python實(shí)現(xiàn)的簡(jiǎn)單讀寫csv文件操作,結(jié)合實(shí)例形式分析了Python使用csv模塊針對(duì)csv文件進(jìn)行讀寫操作的相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下
    2018-07-07
  • 如何在Django配置文件里配置session鏈接

    如何在Django配置文件里配置session鏈接

    這篇文章主要介紹了如何在Django配置文件里配置session鏈接,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • pyqt5 禁止窗口最大化和禁止窗口拉伸的方法

    pyqt5 禁止窗口最大化和禁止窗口拉伸的方法

    今天小編就為大家分享一篇pyqt5 禁止窗口最大化和禁止窗口拉伸的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • Python+Selenium實(shí)現(xiàn)自動(dòng)填寫問卷

    Python+Selenium實(shí)現(xiàn)自動(dòng)填寫問卷

    本文主要介紹了Python+Selenium實(shí)現(xiàn)自動(dòng)填寫問卷,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07

最新評(píng)論