SoFunction
Updated on 2024-10-30

python GUI library graphical interface development PyQt5 switch button control QPushButton detailed use and examples

Introduction to PyQt5 Toggle Button Control QPushButton

QAbstractButton class is an abstract class and cannot be instantiated, it must be inherited by other button classes to realize different functions and expressions, common buttons QPushButton, QToolButton, QRadioButton and QCheckBox are inherited from the QAbstractButton class, according to their respective scenarios of use through the graphical display of the

The states provided by QAbstractButton are listed below.

state of affairs hidden meaning
isDown() Prompts if the button has been pressed
isChecked() Whether the prompt button has been marked
isEnable() Whether the prompt button can be clicked by the user
isCheckAble() Whether the prompt button is markable
setAutoRepeat() Set whether the button can be automatically repeated when the user presses it for a long time.

The signals provided by QAbstractButton are listed in the following table

code hidden meaning
Pressed This signal is triggered when the mouse pointer is on a button and the left button is pressed.
Released Triggered when the left mouse button is released.
Clicked Triggered when the left mouse button is pressed and released, or when a shortcut key is released.
Toggled Triggered when the marking state of the button changes

Common methods in the QPUshButton class

methodologies descriptive
setCheckable() Sets whether the button has been selected, if set to True, it means the button will remain clicked and released.
toggle() Switching between button states
setIcon() Setting the icon on the button
setEnabled() Sets whether the button is available or not, when set to False, the button becomes unavailable and clicking it will not emit a signal.
isChecked() Returns the state of the button, with a return value of True or False.
setDefault() Setting the default state of a button
setText() Setting the display text of a button
text() Return to the display text of the button

Setting shortcut keys for QPushButton

You can set a shortcut key for QPushButton by the button name, for example, if the name of the button is '&Download', its shortcut key is 'Alt+D'. The rule is; want to realize the shortcut key is "Alt+D", then the button name has the letter D, and in front of the D add "&", the letter D is generally the first letter of the button name, and in the button display. "&" will not be displayed, if you want to display, then you need to escape, the core code is as follows

self.btn4=QPushButton('&Download')
self.(True)

Example of using the QPushButton button

import sys
from  import *
from  import *
from  import *

class Form(QDialog):
  def __init__(self,parent=None):
    super(Form, self).__init__(parent)

    #Vertical Layout
    layout=QVBoxLayout()

    #Create button 1
    self.btn1=QPushButton('Button1')
    #setCheckable(): set whether the button has been checked, if True, it means the button will remain clicked and released.
    self.(True)
    #toggle(): toggle between button states
    self.()
    The #click signal is connected to the slot function, which is implemented in this step: the clicked button is output on the console.
    self.(lambda :(self.btn1))
    The #click signal is connected to the slot function to realize the purpose: input the current state of the annyu, pressed or released
    self.()

    # Add controls to the layout
    (self.btn1)

    #Create button 2
    self.btn2=QPushButton('image')
    # Add icon for button 2
    self.(QIcon(QPixmap('E:\pyqt5 Rapid Development and Practice\Chapter 4\images\')))
    #The #click signal is connected to the slot function, which is implemented in this step: the clicked button is output on the console.
    self.(lambda :(self.btn2))

    (self.btn2)

    self.btn3=QPushButton('Disabled')
    #setEnabled() sets whether the button can be used or not, when set to False, the button becomes unavailable and clicking it will not emit a signal.
    self.(False)

    (self.btn3)

    # Create buttons and add shortcuts
    self.btn4=QPushButton('&Download')
    #setDefault(): set the default state of the button
    self.(True)
    #The #click signal is connected to the slot function, which is implemented in this step: the clicked button is output on the console.
    self.(lambda :(self.btn4))

    (self.btn4)

    ("Button demo")
    (layout)

  def btnstate(self):
    #isChecked(): determine the status of the button, the return value is True or False.
    if self.():
      print('button pressed')
    else:
      print('button released')

  def whichbtn(self,btn):
    # Output the button that was clicked
    print('clicked button is '+())
if __name__ == '__main__':
  app=QApplication()
  btnDemo=Form()
  ()
  (app.exec_())

The rendering is as follows

QPushButton code analysis.

In this example, four buttons are created, and these four QPushButton objects are defined as instance variables of the class, each of which sends the clicked signal to the specified slot function in response to the button click event

The first button, btn1,** toggles the state of the button via the toggle() function, the core code of which is

self.btn1=QPushButton('Button1')
self.(True)
self.()

When this button is clicked, the clicked signal is sent to the slot function btnstate() to get the status of whether the button is clicked or released, the core code of which is

self.()

An additional parameter, btn1, can also be passed through a lambda expression that sends the clicked signal to the slot function whichbtn(), with its core code

self.(lambda :(self.btn1))

The second button, btn2, which displays an icon, uses the setIcon() method to accept an image file of a QPixmap object as an input parameter, the core code of which is

self.(QIcon(QPixmap('E:\pyqt5 Rapid Development and Practice\Chapter 4\images\')))

For the third button, btn3, use the setEnabled() method to disable the btn3 button.

self.(False)

The fourth button, btn4, uses the setDefault() method - to set the default state of the button. The shortcut is '&+Text' (&Download), and the slot function is called via the 'Alt+D' shortcut

self.btn4=QPushButton('&Download')

This article mainly explains the PyQt5 switch button control QPushButton detailed use of methods and examples, more about PyQt5 control tutorials, please see the following related links