【问题标题】:creating widgets in user defined slots在用户定义的插槽中创建小部件
【发布时间】:2013-01-12 14:32:06
【问题描述】:

我正在构建一个简单的应用程序,其中有一个按钮,单击该按钮时会打印 hello。

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QMainWindow):
    def __init__(self):
        super(Example, self).__init__();
        self.initUI()


    def initUI(self):
        self.button = QtGui.QPushButton("print hello",self)
        self.button.clicked.connect(self.print_hello)

    def print_hello(self):
        self.button.deleteLater()
        self.label = QtGui.QLabel("hello",self)



def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

if __name__=='__main__':
    main()

现在,插槽 print_hello() 不会输出标签“hello”
为什么会这样?

【问题讨论】:

    标签: python qt pyqt signals signals-slots


    【解决方案1】:

    标签没有显示,因为虽然您已经创建了它,但您还没有告诉 GUI 显示它。例如,您可能希望在决定显示标签之前在后台对标签执行一些其他操作。

    self.label.show() 添加到 print_hello() 将使其可见。

    【讨论】:

      猜你喜欢
      • 2013-01-11
      • 2013-05-31
      • 2020-02-15
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      相关资源
      最近更新 更多