【发布时间】:2014-07-07 05:39:01
【问题描述】:
我正在尝试用三个按钮制作一个简单的小部件,单击每个按钮时都会显示相应的其他小部件。
以下是我尝试运行但无法弄清楚为什么新小部件未显示的代码。
from PyQt4 import QtGui, QtCore
from functools import partial
import sys
class MainWidget(QtGui.QWidget):
def __init__(self):
super(MainWidget, self).__init__()
self.main_widget()
def main_widget(self):
another = Another()
simple = Simple()
grid = QtGui.QGridLayout()
show_another_button = QtGui.QPushButton("Show Another")
show_another_button.clicked.connect(another.show_another)
grid.addWidget(show_another_button, 0, 0)
show_simple_button = QtGui.QPushButton("Show Simple")
show_simple_button.clicked.connect(simple.show_simple)
grid.addWidget(show_simple_button, 0, 1)
print_button = QtGui.QPushButton("Print Hello")
print_button.clicked.connect(partial(print, "Hello"))
grid.addWidget(another_button, 0, 2)
self.setLayout(grid)
self.show()
class Another(QtGui.QWidget):
def __init__(self):
print("another initialized")
super(Another, self).__init__()
def show_another(self):
print("another called")
grid = QtGui.QGridLayout()
self.setLayout(grid)
self.show()
class Simple(QtGui.QDialog):
def __init__(self):
print("simple initialized")
super(Simple, self).__init__()
def show_simple(self):
print("simple called")
self.show()
def main():
app = QtGui.QApplication(sys.argv)
main_widget = MainWidget()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
请帮忙!
【问题讨论】:
-
他们可能在函数结束时收集了垃圾。尝试使它们成为实例属性。将
simple更改为self.simple,另一个也一样。还有另一个没有函数show_another和ex = Example()可能是错字。 -
是的,实际上有点想通了,是的,示例是一个错字,也已修复。感谢您的帮助。
标签: python user-interface widget pyqt pyqt4