【问题标题】:How to implement a simple button in PyQt如何在 PyQt 中实现一个简单的按钮
【发布时间】:2012-02-04 11:30:45
【问题描述】:

我想在 PyQt 中实现一个简单的按钮,单击它会打印“Hello world”。我该怎么做?

我是 PyQt 的真正新手。

【问题讨论】:

标签: python user-interface button pyqt signals-slots


【解决方案1】:

如果您是 PyQt 的新手,PyQt Wiki 上有一些有用的教程可以帮助您入门。但与此同时,这是您的“Hello World”示例:

from PyQt5 import QtWidgets, QtCore

class Window(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.button = QtWidgets.QPushButton('Test')
        self.button.clicked.connect(self.handleButton)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.button)

    def handleButton(self):
        print('Hello World')

if __name__ == '__main__':

    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

【讨论】:

  • 当我尝试这个时,我得到:Failed to load platform plugin "xcb". Available platforms are "linuxfb" and "minimal". Do you know why? I am running from with the latest stable Python, IPython, Qt5 and snapshot from PyQt4
  • @user815423426。听起来你的安装坏了。您在哪个平台上,您是如何安装组件的?
猜你喜欢
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
  • 1970-01-01
  • 2012-09-19
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 2012-03-09
相关资源
最近更新 更多