如何在 PyQt 中实现一个简单的按钮 [英] How to implement a simple button in PyQt
本文介绍了如何在 PyQt 中实现一个简单的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 PyQt 中实现一个简单的按钮,单击时会打印Hello world".我该怎么做?
I want to implement a simple button in PyQt which prints "Hello world" when clicked. How can I do that?
我是 PyQt 的真正新手.
I am a real newbie in PyQt.
推荐答案
如果您是 PyQt4 的新手,PyQt Wiki 帮助您入门.
If you're new to PyQt4, there are some useful tutorials on the PyQt Wiki to get you started.
但与此同时,这是您的Hello World"示例:
But in the meantime, here's your "Hello World" example:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.button = QtGui.QPushButton('Test', self)
self.button.clicked.connect(self.handleButton)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.button)
def handleButton(self):
print ('Hello World')
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
这篇关于如何在 PyQt 中实现一个简单的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文