PySide2 basic chapter: QPushButton application

PySide2 basic part (3) - QPushButton application

preface:
What can I learn from reading this article?
                         .

——If you think this is a good blog, I hope you can give a little praise, thank you for your support.

1 create a button

The    button is one of the most commonly used controls in dialog interaction. PySide2 provides us with a class PushButton for generating buttons. Let's try to instantiate it to create a button control.
Example code:

from PySide2.QtWidgets import QApplication, QPushButton

app = QApplication([])
PushButton = QPushButton("Initial text")

PushButton.show()
app.exec_()

   operation result:

   the string "initial text" passed in to the constructor will be displayed on the button. If you do not want to display any text, leave the parameter blank. If you want to modify the text content displayed by the button later, you can call the setText() function to modify the text content.
Example code:

from PySide2.QtWidgets import QApplication, QPushButton

app = QApplication([])
PushButton = QPushButton("Initial text")
PushButton.setText("Modify text")             #Modify text content

PushButton.show()
app.exec_()

   operation result:

2 add click event to button

                      . In PySide2, there is the concept of Slot, which is actually a decorator (if you don't know about decorators, please refer to my Python 3 advanced tutorial, which has a detailed introduction to decorators).
Example code:

from PySide2.QtWidgets import QApplication, QPushButton
from PySide2.QtCore import Slot

@Slot()
def PressButton():
    print("Hello World!")

app = QApplication([])
PushButton = QPushButton("Initial text")
PushButton.clicked.connect(PressButton)

PushButton.show()
app.exec_()

   operation result:

                       .

3 Personalized button

                  .

3.1 add Icon to button

                     .
Example code:

from PySide2.QtWidgets import QApplication, QPushButton
from PySide2.QtCore import Slot
from PySide2.QtGui import QPixmap

@Slot()
def PressButton():
    print("Hello World!")

app = QApplication([])
PixMap = QPixmap(".\png\PushButton.png")
PushButton = QPushButton(PixMap, "Initial text")
PushButton.clicked.connect(PressButton)

PushButton.show()
app.exec_()

   operation result:

   you can see that both the pixel image and the text content are displayed. Let's try adding icons again.
Example code:

from PySide2.QtWidgets import QApplication, QPushButton
from PySide2.QtCore import Slot
from PySide2.QtGui import QIcon

@Slot()
def PressButton():
    print("Hello World!")

app = QApplication([])
Icon = QIcon(".\png\PushButton.ico")
PushButton = QPushButton(Icon, "Initial text")
PushButton.clicked.connect(PressButton)

PushButton.show()
app.exec_()

   operation result:

3.2 set the background color for the button

   many controls can call setStyleSheet method to set the appearance, here we try to change the button color to green through it.

from PySide2.QtWidgets import QApplication, QPushButton
from PySide2.QtCore import Slot
from PySide2.QtGui import QPixmap

@Slot()
def PressButton():
    print("Hello World!")

app = QApplication([])
PushButton = QPushButton("Initial text")
PushButton.clicked.connect(PressButton)
PushButton.setStyleSheet("background-color:green")

PushButton.show()
app.exec_()

   operation result:

4 other function settings of the button

4.1 menu button

                     .
Example code:

from PySide2.QtWidgets import QApplication, QPushButton
from PySide2.QtWidgets import QMenu

app = QApplication([])
PushButton = QPushButton("Initial text")
Menu = QMenu()
Menu.addAction("1")
Menu.addAction("2")
Menu.addAction("3")
PushButton.setMenu(Menu)

PushButton.show()
app.exec_()

   operation result:

4.2 disable button (gray)

   sometimes there is some logic between buttons. When you click a button, other buttons need to be disabled. In this case, you need to temporarily "gray" the button.
Example code:

from PySide2.QtWidgets import QApplication, QPushButton

app = QApplication([])
PushButton = QPushButton("Initial text")
PushButton.setEnabled(False)

PushButton.show()
app.exec_()

   operation result:

Tags: Python

Posted on Thu, 04 Jun 2020 23:34:31 -0700 by Michael_zz