QPushButton
PyQt5 : QPushButton widget .
As the name suggests the QPushButton is used to add a button on our PyQt5 GUI Window .
Button is used to call any function to perform some specific action .
A button is rectangular and usually displays a text label that describes its actions. Shortcuts can be specified for buttons.
In this blog you will learn how to create button , call a function using it and lots more .
So lets code :
# PyQt5 : Dynamic Coding # creating button // QPushButton from PyQt5.QtWidgets import QPushButton,QApplication,QMainWindow from PyQt5.QtGui import * import sys class Example(QMainWindow): def __init__(self): super().__init__() self.setGeometry(200,300,300,200) self.setWindowTitle('Dynamic coding') button = QPushButton('Button 1',self) button.resize(100,50) button.move(110,50) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_())
QPushButton creates the widget, the first argument is text on the button and the second is where to place it .
The move and resize method is used for changing the position and size of Button or any widget .
OUTPUT :
Adding icon on button :
# PyQt5 : Dynamic Coding # adding icon on button from PyQt5.QtWidgets import QPushButton,QApplication,QMainWindow from PyQt5.QtGui import * import sys class Example(QMainWindow): def __init__(self): super().__init__() self.setGeometry(200,300,300,200) self.setWindowTitle('Dynamic coding') button = QPushButton('Button 1',self) button.setIcon(QIcon('sublime.jpg')) # gives a perfect size of any PyQt5 widget // sizeHint button.resize(button.sizeHint()) button.move(110,50) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_())
For adding icon on QPushButton we use a method setIcon and QIcon method of QtGui Class .
if you see the code we also use a szeHint method :
The sizeHint ( ) method returns a perfect size of any PyQt5 widget according to the content present on it .
OUTPUT :
Calling function through button :
# PyQt5 : Dynamic Coding # calling function through button from PyQt5.QtWidgets import QPushButton,QApplication,QMainWindow from PyQt5.QtGui import * import sys class Example(QMainWindow): def __init__(self): super().__init__() self.setGeometry(200,300,300,200) self.setWindowTitle('Dynamic coding') self.button = QPushButton('Button 1',self) self.button.resize(self.button.sizeHint()) self.button.move(110,50) # ... self.button.clicked.connect(self.function) def function(self): self.button.setText('Button is pressed') self.button.resize(self.button.sizeHint()) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_())
When a button is activated by a mouse, space bar, or keyboard shortcut, the button sends a clicked() signal.
Connect to this signal to perform the action of the button. Buttons also provide less-used signals, such as pressed()
and released().
Note : you can also disable button by using command : setEnabled( False )
OUTPUT :
When the button is pressed the button text is changed to Button is pressed .
Setting tooltip :
The method setToolTip shows the message when the user points the mouse on the button.
# PyQt5 : Dynamic Coding # Adding tooltip from PyQt5.QtWidgets import QPushButton,QApplication,QMainWindow from PyQt5.QtGui import * import sys class Example(QMainWindow): def __init__(self): super().__init__() self.setGeometry(200,300,300,200) self.setWindowTitle('Dynamic coding') self.button = QPushButton('Button 1',self) # .. setting tooltip self.button.setToolTip('press on button') self.button.resize(self.button.sizeHint()) self.button.move(110,50) # ... self.button.clicked.connect(self.function) def function(self): self.button.setText('Button is pressed') self.button.resize(self.button.sizeHint()) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_())
OUTPUT :
Applying stylesheet :
# PyQt5 : Dynamic Coding # changing color of button using stylesheet from PyQt5.QtWidgets import QPushButton,QApplication,QMainWindow from PyQt5.QtGui import * import sys class Example(QMainWindow): def __init__(self): super().__init__() self.setGeometry(200,300,300,200) self.setWindowTitle('Dynamic coding') self.button = QPushButton('Button 1',self) # .. setting tooltip self.button.setToolTip('press on button') # .. adding styles self.button.setStyleSheet("color:black;background-color:cyan;") self.button.resize(self.button.sizeHint()) self.button.move(110,50) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_())
OUTPUT :
No comments