简介
对与控件QPushButton中的可以使用setStyleSheet设置它背景图片。具体设置背景图片的方法有两种
self.button.setStyleSheet("QPushButton{background-image: url(img/1.png)}")
然而对于这种方法背景图片无法进行边框的自适应,可以使用下面的方法
self.button.setStyleSheet("QPushButton{border-image: url(img/1.png)}")
可以做到自适应边框。
代码
代码里面有两个图片需要使用,我放在下面了
代码1
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI() # 界面绘制交给InitUi方法
def initUI(self):
# 设置窗口的位置和大小
self.setGeometry(300, 300, 300, 220)
# 设置窗口的标题
self.setWindowTitle('QPushButton')
#控件QPushButton的定义和设置
self.button = QPushButton(self)
self.button.setStyleSheet("QPushButton{border-image: url(img/1.