self.button.setStyleSheet("QPushButton{background-image: url(img/1.png)}")
然而对于这种方法背景图片无法进行边框的自适应,可以使用下面的方法
self.button.setStyleSheet("QPushButton{border-image: url(img/1.png)}")
可以做到自适应边框。
代码里面有两个图片需要使用,我放在下面了
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.png)}"
"QPushButton:hover{border-image: url(img/1_1.png)}"
"QPushButton:pressed{border-image: url(img/1_1.png)}")
#设置控件QPushButton的位置和大小
self.button.setGeometry(100, 100, 50, 50)
#控件QPushButton的定义和设置
self.button = QPushButton(self)
self.button.setStyleSheet("QPushButton{border-image: url(img/1.png)}"
"QPushButton:hover{border-image: url(img/1_1.png)}")
# 设置控件QPushButton的位置和大小
self.button.setGeometry(100, 100, 50, 50)
def slot_init(self):
self.button.clicked.connect(self.button_change)
def button_change(self):
# 切换图标变亮
self.button.setStyleSheet('QPushButton{border-image:url(img/1_1.png)}')
if __name__ == '__main__':
# 创建应用程序和对象
app = QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
如果需要来回的切换,可以定义一个计数器来解决这个问题
在QT中,QPushButton是我们经常用的控件,但是系统默认控件的样式不好看,因此我们一般都会给QPushButton设置
背景图片
,并且一般想达到的效果是,当鼠标放上去时是一张图片,当鼠标点击时是一张图片,当鼠标离开时,又是另外一张图片,怎么
实现
这个效果了。
1、setStyleSheet
setStyleSheet("QPushButton{background-image: u
具体如下:
self.pushButton.setStyleSheet("QPushButton{border-image: url(a.png)}") # 设置
背景图片
,设置后一直存在
self.pushButton.setStyleSheet("QPushButton:hover{border-image: url(a.png...
本例程使用
PyQt
5
库,在QMainWindow中设置
背景图片
。重要的是两步:
mainwindow.setObjectName("MainWindow")#设置对象名称
mainwindow.setStyleSheet("#MainWindow{border-image:url(./python.jpg);}") #设置窗口
背景图片
以下为示例代码:
#-*-cod...
abc__121314:
yolo v3 pytorch版本的预训练模型(百度云盘)
2301_77934603:
虚拟机Centos系统下hadoop集群中静态IP配置
2401_87221216:
PaddleOCR服务器端部署C++ cpu或者gpu进行预测
L C H:
PaddleOCR服务器端部署C++ cpu或者gpu进行预测
L C H: