問題描述
我的代碼用一個按鈕調用一個窗口.單擊按鈕時,調用另一個窗口.但是第二個窗口立即關閉
My code calls one window with a button. When the button is clicked, call another window. But the second window closes immediately
基本"和windows_two"是 pyuic5 從 .ui 文件中生成的 .py 庫
"basic" and "windows_two" are .py libraries genereted by pyuic5 from .ui files
import basic, windows_two
from PyQt5 import QtCore, QtGui, QtWidgets
if __name__ == "__main__":
#Declarations
import sys
app = QtWidgets.QApplication(sys.argv)
def Call_Second_Window():
#Second Screen
Form = QtWidgets.QWidget()
ui = windows_two.Ui_Form()
ui.setupUi(Form)
Form.show()
def Call_Main_Window():
#MainWindow
MainWindow = QtWidgets.QMainWindow()
ui = basic.Ui_MainWindow()
ui.setupUi(MainWindow)
ui.printButton.clicked.connect(Call_Second_Window) #click event to second window
MainWindow.show()
sys.exit(app.exec_())
Call_Main_Window()
怎么了?
謝謝
推薦答案
只要變量是本地,它就會被收集垃圾".一旦函數返回;這意味著變量可能引用的所有內容也將(可能)被刪除.
Whenever a variable is local it gets "garbage collected" as soon as the function returns; this means that everything the variable might reference to will also be (possibly) deleted too.
在您的情況下發生的情況是,在正確創建窗口時,當 Call_Second_Window
返回時(就在 Form.show 之后),它將立即被刪除(由于垃圾收集)()
).
What is happening in your case is that while the windows is correctly created, it will be immediately deleted (due to the garbage collection) when the Call_Second_Window
returns (just after Form.show()
).
為了避免只有一個解決方案:使對對象的引用持久化.有多種方法可以實現這一目標,具體取決于具體情況.
To avoid that there is only one solution: make the reference to the object persistent. There are various approaches to achieve that, depending on the situation.
不幸的是,您的代碼有點不正統(尤其是從 PyQt 的角度來看),所以我正在重構".它是為了使它更標準化、更好地面向對象,而且更重要的是,更易于閱讀.
Unfortunately your code is a bit unorthodox (especially from a PyQt perspective), so I'm "refactoring" it in order to make it more standardized, better object oriented and, also importantly, easily readable.
import basic, windows_two
from PyQt5 import QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.ui = basic.Ui_MainWindow()
self.ui.setupUi(self)
self.ui.printButton.clicked.connect(self.call_Second_Window)
self.secondWindow = None
def call_Second_Window(self):
if not self.secondWindow:
self.secondWindow = SecondWindow()
self.secondWindow.show()
class SecondWindow(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.ui = windows_two.Ui_Form()
self.ui.setupUi(self)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
注意:如您所見,我將 call_Second_Window
的名稱更改為小寫的c",這是因為大寫名稱只能用于類和常量,而函數名稱應始終以小寫字母開頭.這又是為了可讀性,這在編程中非常重要,也是 python 的核心原則之一.在官方 Python 代碼樣式指南.
Note: As you can see, I changed the name of call_Second_Window
with a lower "c", and that's because capitalized names should only be used for classes and constants, while function names should always start with a lower case. This is again for readability, which is very important in programming and one of the core principles of python. Read more about this and other important topics on the official Style Guide for Python Code.
這篇關于運行后立即關閉窗口的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!