PyQt 窗口不显示 [英] PyQt Window No Show
问题描述
在 simple 上调用 show 方法时,简单窗口不显示.为什么我的简单窗口不显示.:(
Upon calling the show method on simple the simple window does not show. Why doesn't my Simple window show. :(
import sys
from PyQt4 import QtGui
class Widget(QtGui.QWidget):
def __init__(self):
super(Widget, self).__init__()
simple = Simple()
button = QtGui.QPushButton("Button", self)
button.clicked.connect(simple.show)
self.show()
class Simple(QtGui.QWidget):
def __init__(self):
super(Simple, self).__init__()
self.setGeometry(300, 250, 250, 150)
self.setWindowTitle("Simple Widget")
if __name__ =="__main__":
app = QtGui.QApplication(sys.argv)
widget = Widget()
sys.exit(app.exec_())
请帮忙!
推荐答案
你的代码的问题在于,__init__
类的Widget
方法中的
simple
/code> 是一个局部变量,所以一旦 __init__
方法执行完毕,simple
对象就会被 python Garbage Collector 销毁,因此该窗口不会出现,因为该对象不存在于内存中.要解决您的问题,只需在 simple
变量的开头添加 self
使其成为成员变量.
The problem with your code is that, simple
in __init__
method of class Widget
is a local variable, so as soon as the __init__
method finishes execution, the simple
object is destroyed by the python Garbage Collector, thus the window does not appear because the object does not exist in the memory. To solve your problem, just add self
at the starting of the simple
variable to make it member variable.
...
self.simple = Simple()
button = QtGui.QPushButton("Button", self)
button.clicked.connect(self.simple.show)
...
这篇关于PyQt 窗口不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!