How to implement a simple button in PyQt

前端 未结 1 1373
萌比男神i
萌比男神i 2020-12-11 02:23

I want to implement a simple button in PyQt which prints \"Hello world\" when clicked. How can I do that?

I am a real newbie in PyQt.

相关标签:
1条回答
  • 2020-12-11 03:17

    If you're new to PyQt4, there are some useful tutorials on the PyQt Wiki to get you started.

    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_())
    
    0 讨论(0)
提交回复
热议问题