Python PyQt5中彈出子窗口解決子窗口一閃而過的問題
在主窗口添加按鈕,并把按鈕信號(hào)關(guān)聯(lián)槽,在槽函數(shù)中創(chuàng)建子窗口對(duì)象賦值到普通變量,并調(diào)用其 show 方法。
from PyQt5.QtWidgets import *import sys class Main(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('主窗口') button = QPushButton('彈出子窗', self) button.clicked.connect(self.show_child) def show_child(self): child_window = Child() child_window.show() class Child(QWidget): def __init__(self): super().__init__() self.setWindowTitle('我是子窗口啊') # 運(yùn)行主窗口if __name__ == '__main__': app = QApplication(sys.argv) window = Main() window.show() sys.exit(app.exec_())
運(yùn)行結(jié)果: 該段代碼運(yùn)行后,點(diǎn)擊主窗口中的按鈕,子窗口一閃而過。
方式二:槽函數(shù)中創(chuàng)建子窗口對(duì)象,賦值為對(duì)象屬性在主窗口添加按鈕,并把按鈕信號(hào)關(guān)聯(lián)槽,在槽函數(shù)中創(chuàng)建子窗口對(duì)象并賦值為對(duì)象屬性,并調(diào)用其 show 方法。
from PyQt5.QtWidgets import *import sys class Main(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('主窗口') button = QPushButton('彈出子窗', self) button.clicked.connect(self.show_child) def show_child(self): self.child_window = Child() self.child_window.show() class Child(QWidget): def __init__(self): super().__init__() self.setWindowTitle('我是子窗口啊') # 運(yùn)行主窗口if __name__ == '__main__': app = QApplication(sys.argv) window = Main() window.show() sys.exit(app.exec_())
運(yùn)行結(jié)果: 該段代碼運(yùn)行后,點(diǎn)擊主窗口中的按鈕,子窗口正常打開,重復(fù)點(diǎn)擊按鈕,子窗口重復(fù)彈出。
方式三:在主窗口__init__方法中創(chuàng)建子窗在主窗口__init__方法中創(chuàng)建子窗口對(duì)象并賦值為對(duì)象屬性,添加按鈕,并把按鈕信號(hào)關(guān)聯(lián)槽,在槽函數(shù)中調(diào)用子窗口對(duì)象的 show 方法。
from PyQt5.QtWidgets import *import sys class Main(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('主窗口') button = QPushButton('彈出子窗', self) button.clicked.connect(self.show_child) self.child_window = Child() def show_child(self): self.child_window.show() class Child(QWidget): def __init__(self): super().__init__() self.setWindowTitle('我是子窗口啊') # 運(yùn)行主窗口if __name__ == '__main__': app = QApplication(sys.argv) window = Main() window.show() sys.exit(app.exec_())
運(yùn)行結(jié)果: 重復(fù)點(diǎn)擊按鈕,子窗口不重復(fù)彈出。
方式四:exec()方法把例1的show()方法改為exec()方法
from PyQt5.QtWidgets import *import sys class Main(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('主窗口') button = QPushButton('彈出子窗', self) button.clicked.connect(self.show_child) def show_child(self): child_window = Child() child_window.exec() class Child(QWidget): def __init__(self): super().__init__() self.setWindowTitle('我是子窗口啊') # 運(yùn)行主窗口if __name__ == '__main__': app = QApplication(sys.argv) window = Main() window.show() sys.exit(app.exec_())
運(yùn)行結(jié)果:子窗口順利彈出,且不能重新選擇父窗口
結(jié)論:這里涉及到一個(gè)概念 模式對(duì)話框 與 非模式對(duì)話框 (modeless dialog | modal dialog)
模式對(duì)話框,就是在彈出窗口的時(shí)候,整個(gè)程序就被鎖定了,處于等待狀態(tài),直到對(duì)話框被關(guān)閉。這時(shí)往往是需要對(duì)話框的返回值進(jìn)行下面的操作。如:確認(rèn)窗口(選擇“是”或“否”)。非模式對(duì)話框,在調(diào)用彈出窗口之后,調(diào)用即刻返回,繼續(xù)下面的操作。這里只是一個(gè)調(diào)用指令的發(fā)出,不等待也不做任何處理。如:查找框。
show() ------ modeless dialog
exec() ------- modal dialog
方式一中 子窗口 通過 show() 方法顯示,為非模態(tài)窗口,它的實(shí)例為父窗口show_child()方法中的局部變量,當(dāng)窗口顯示后,父窗口的show_child()方法繼續(xù)執(zhí)行,當(dāng)方法運(yùn)行完后,python的回收機(jī)制就把局部變量銷毀了,相當(dāng)于子窗口實(shí)例被銷毀,故子窗口一閃而過; 方式二中 子窗口實(shí)例為 主窗口類的變量,當(dāng)show_child()方法運(yùn)行完后,主窗口對(duì)象依舊存在,子窗口實(shí)例也存在,故子窗口正常顯示,但是每一次運(yùn)行槽函數(shù)都會(huì)重新創(chuàng)建子窗口對(duì)象; 方式三中 子窗口實(shí)例為 主窗口類的變量,當(dāng)show_child()方法運(yùn)行完后,主窗口對(duì)象依舊存在,子窗口實(shí)例也存在,故子窗口正常顯示,每一次show_child()函數(shù),重新調(diào)用子窗口對(duì)象show_child()方法,不會(huì)創(chuàng)建新窗口,且可隨意在父,子窗口間切換; 方式四中 子窗口 通過 exec() 方法顯示,為模態(tài)窗口,雖然他為父窗口show_child()方法中的局部變量,由于阻塞的機(jī)制,父窗口show_child()并沒有繼續(xù)執(zhí)行,故其不會(huì)像 例1 中 一閃而過,且不能在父,子窗口間切換;到此這篇關(guān)于Python PyQt5中彈出子窗口解決子窗口一閃而過的問題的文章就介紹到這了,更多相關(guān)Python PyQt5彈出子窗口內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
