[英]How I reopen a window using pyqt?
我使用 qtgenerator 創建了幾個 windows 並使用這種插槽來隱藏當前的 window 並顯示另一個:
SearchResearchScreen.py
class Ui_ResearchMenu(object):
def setupUi(self, ResearchMenu):
...
def retranslateUi(self, ResearchMenu):
...
def edit_clicked1(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_EditNewResearch()
self.ui.setupUi(self.window)
self.window.show()
ResearchMenu.hide()
self.timer = QtCore.QTimer()
self.timer.setInterval(100)
self.timer.timeout.connect(self.checkVar)
self.timer.start()
EditresearchScreen.py -
class Ui_EditNewResearch(object):
def setupUi(self, EditNewResearch):
...
def retranslateUi(self, EditNewResearch):
...
def btn_Cancel_clicked(self):
global status
status = False
self.window = QtWidgets.QMainWindow()
self.ui = Ui_ResearchMenu()
self.ui.setupUi(self.window)
self.window.show()
EditNewResearch.hide()
單擊按鈕時效果很好,之后,在第二個 window 中,我使用相同的代碼返回到第一個 window 但應用程序崩潰。 除非我評論.hide()
但我不能返回到相同的 window 而是在舊的仍然存在於后台時打開一個新的。
如何在程序不崩潰的情況下通過不同的 windows?
我通過使用 QTimer 解決了這個問題。 您應該在第一個 window 中使用 QTimer,它將不斷檢查您將在這兩種情況下更新的任何全局變量。
像這樣創建 QTimer:-
def edit_clicked1(self):
global status
status = True # This will be your global variable which will check if the window is closed or not
self.window = QtWidgets.QMainWindow()
self.ui = Ui_EditNewResearch()
self.ui.setupUi(self.window)
self.window.show()
ResearchMenu.hide()
self.timer = QTimer()
self.timer.setInterval(100)
self.timer.timeout.connect(self.checkVar)
self.timer.start()
並檢查這樣的變量
def checkVar(self):
global status
if status == False:
# Show your hided window here
self.show()
請記住,在關閉第二個 window 時,您需要將此status
變量設置為 False。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.