這篇文章主要為大家詳細介紹了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()
Form1.exec_()
以上是PyQT實現多視窗切換的詳細內容。更多資訊請關注PHP中文網其他相關文章!

使用NumPy創建多維數組可以通過以下步驟實現:1)使用numpy.array()函數創建數組,例如np.array([[1,2,3],[4,5,6]])創建2D數組;2)使用np.zeros(),np.ones(),np.random.random()等函數創建特定值填充的數組;3)理解數組的shape和size屬性,確保子數組長度一致,避免錯誤;4)使用np.reshape()函數改變數組形狀;5)注意內存使用,確保代碼清晰高效。

播放innumpyisamethodtoperformoperationsonArraySofDifferentsHapesbyAutapityallate AligningThem.itSimplifififiesCode,增強可讀性,和Boostsperformance.Shere'shore'showitworks:1)較小的ArraySaraySaraysAraySaraySaraySaraySarePaddedDedWiteWithOnestOmatchDimentions.2)

forpythondataTastorage,choselistsforflexibilityWithMixedDatatypes,array.ArrayFormeMory-effficityHomogeneousnumericalData,andnumpyArraysForAdvancedNumericalComputing.listsareversareversareversareversArversatilebutlessEbutlesseftlesseftlesseftlessforefforefforefforefforefforefforefforefforefforlargenumerdataSets; arrayoffray.array.array.array.array.array.ersersamiddreddregro

Pythonlistsarebetterthanarraysformanagingdiversedatatypes.1)Listscanholdelementsofdifferenttypes,2)theyaredynamic,allowingeasyadditionsandremovals,3)theyofferintuitiveoperationslikeslicing,but4)theyarelessmemory-efficientandslowerforlargedatasets.

toAccesselementsInapyThonArray,useIndIndexing:my_array [2] accessEsthethEthErlement,returning.3.pythonosezero opitedEndexing.1)usepositiveandnegativeIndexing:my_list [0] fortefirstElment,fortefirstelement,my_list,my_list [-1] fornelast.2] forselast.2)

文章討論了由於語法歧義而導致的Python中元組理解的不可能。建議使用tuple()與發電機表達式使用tuple()有效地創建元組。 (159個字符)

本文解釋了Python中的模塊和包裝,它們的差異和用法。模塊是單個文件,而軟件包是帶有__init__.py文件的目錄,在層次上組織相關模塊。

文章討論了Python中的Docstrings,其用法和收益。主要問題:Docstrings對於代碼文檔和可訪問性的重要性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver Mac版
視覺化網頁開發工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 Linux新版
SublimeText3 Linux最新版

Atom編輯器mac版下載
最受歡迎的的開源編輯器

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具