Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a simple button in PyQt

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.

like image 467
Abid Rahman K Avatar asked Jan 06 '12 18:01

Abid Rahman K


People also ask

What is Tool button in PyQt?

Detailed Description. A tool button is a special button that provides quick-access to specific commands or options. As opposed to a normal command button, a tool button usually doesn't show a text label, but shows an icon instead.


1 Answers

If you're new to PyQt, there are some useful tutorials on the PyQt Wiki to get you started. But in the meantime, here's your "Hello World" example:

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_())
like image 81
ekhumoro Avatar answered Oct 16 '22 04:10

ekhumoro