PyQt 窗口不显示

编程入门 行业动态 更新时间:2024-10-21 06:36:29
本文介绍了PyQt 窗口不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在 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 窗口不显示

本文发布于:2023-07-18 12:52:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1144961.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗口   PyQt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!