In this PyQt5 article i want to show you How To Create QPushButton In PyQt5. The QPushButton widget provides a command button.The push button, or command button, is perhaps the most commonly used widget in any graphical user interface. Push (click) a button to command the computer to perform some action, or to answer a question. Typical buttons are OK, Apply, Cancel, Close, Yes, No and Help.
OK in the previous article we have created our Window in PyQt5, the process is the same. also you can check my article about How To Create Window In PyQt5 (Python GUI Development)
Join PyQt5 Full Course for Free
PyQt5 Course
Also you can read more Python GUI articles in the below links
- Kivy GUI Development Tutorials
- TKinter GUI Development Tutorials
- Pyside2 GUI Development
- wxPython GUI Development Tutorials
- PyQt5 GUI Development Tutorials
First let me show you the complete code for How To Create QPushButton In PyQt5 after that iam going to describe the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton import sys from PyQt5 import QtGui from PyQt5.QtCore import QRect from PyQt5 import QtCore class Window(QMainWindow): def __init__(self): super().__init__() title = "PyQt5 PushButton" left = 500 top = 200 width = 300 height = 250 iconName = "icon.png" self.setWindowTitle(title) self.setWindowIcon(QtGui.QIcon(iconName)) self.setGeometry(left, top, width, height) self.UiComponents() self.show() def UiComponents(self): button = QPushButton("Close Application", self) #button.move(50,50) button.setGeometry(QRect(100,100,150,40)) button.setIcon(QtGui.QIcon("icon.png")) button.setIconSize(QtCore.QSize(40,40)) button.setToolTip("<h1>This Is Click Button<h1>") button.clicked.connect(self.ButtonAction) def ButtonAction(self): print("Hello World") sys.exit() if __name__ == "__main__": App = QApplication(sys.argv) window = Window() sys.exit(App.exec_()) |
In the above code we have imported our required classes after that we have created our Window class with the requirements of the window and in the UiComponents() method we create our QPushButton object , we set the geometry for the button, also we have created QIcon and setIconSize for the QPushButton. in the ButtonAction() method or slot we create just an Hello World.
Run the complete code and this will be the result
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email