Cheap VPS host selection
Provide server host evaluation information

Pyqt5 Set background picture (with detailed code)

For the control in QPushButton, you can use setStyleSheet to set its background picture. There are two ways to set the background image

self.button.setStyleSheet(“QPushButton{background-image: url(img/1.png)}”)

However, for this method, the background image cannot be adaptive to the border, and the following method can be used

self.button.setStyleSheet(“QPushButton{border-image: url(img/1.png)}”)

The frame can be adaptive.

Code 1

import sys from PyQt5.QtWidgets import QApplication, QWidget, QPushButton class Example(QWidget): def __init__(self): super().__ Init__ () self. initUI () # Give the interface drawing to InitUi method def initUI (self): # Set the position and size of the window self. setGeometry (300, 300, 300, 220) # Set the window title self. setwindowTitle ('QPushButton ') # Define and set the control QPushButton self. button=QPushButton (self) self. button. setStyleSheet ("QPushButton {border image: url (img/1. png)}" "QPushButton: over {border image: url (img/1_1. png)}" "QPushButton: pressed {border image: url (img/1_1. png)}") # Set the position and size of the control QPushButton self. button. setGeometry (100, 100, 50, 50) if __name__=='__main__ ': # Create applications and objects app=QApplication (sys. argv) ex=Example() ex.show() sys. exit (app. exec_ ())

The background picture of the button, the background switch when the mouse moves over the button, and the background switch when the button is pressed are realized.

However, when I press the button, I need to switch images directly without returning to the original background. Please refer to my code 2.

import sys from PyQt5.QtWidgets import QApplication, QWidget, QPushButton class Example(QWidget): def __init__(self): super().__ Init__ () self. initUI() # Give the interface drawing to InitUi method self. slot_init() def initUI (self): # Set the position and size of the window self. setGeometry (300, 300, 300, 220) # Set the title of the window self. setwindowTitle ('QPushButton ') # Define and set the control QPushButton self. button=QPushButton (self) Self. button. setStyleSheet ("QPushButton {border image: url (img/1. png)}" "QPushButton: over {border image: url (img/1_1. png)}") # Set the position and size of the control QPushButton self. button. setGeometry (100, 100, 50, 50) def slot_init (self): self. button. clicked. connect (self. button_change) def button_change (self) : # The switch icon lights up self. button. setStyleSheet ('QPushButton {border image: url (img/1_1. png)} ') if __name__=='__main__': # Create applications and objects app=QApplication (sys. argv) ex=Example() ex.show() sys. exit (app. exec_ ())

If you need to switch back and forth, you can define a counter to solve this problem.

This is how pyqt5 implements the button to add a background image and switch between background images.

Do not reprint without permission: Cheap VPS evaluation » Pyqt5 Set background picture (with detailed code)