我想知道如何使用PyQt5在Python应用程序中设置背景颜色和背景图像。我不知道如何同时设置它们。我试着做
self.window.setStyleSheet("* {color: qlineargradient(spread:pad, x1:0 y1:0, x2:1 y2:0, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));"
"background: qlineargradient( x1:0 y1:0, x2:1 y2:0, stop:0 #ffc982, stop:1 #ff9982);}; background-image: url(image.png); background-repeat: no-repeat")
但这是行不通的。我得到了“无法解析样式表”错误。显然,图像与代码的方向相同。
另外,当我只设置背景图像时,它会显示“阴影”:
你知道如何解决这些问题吗?
发布于 2021-01-25 00:26:41
有两个问题:
;}
,这使得样式表无效;使用"*
“通用选择器(与使用QWidget
几乎相同)的H 29G 210
因此,除了修复错误之外,您还应该只为您感兴趣的部件应用背景。一个好的解决方案可以是设置中央小部件的对象名称(如果还没有设置,例如在使用设计器文件时),并在样式表中使用适当的选择器。我还建议您在样式表上使用更好的格式和缩进,因为它将使样式表更具可读性,使您能够更容易地查找语法错误。
self.window.centralWidget().setObjectName('centralWidget')
self.window.setStyleSheet('''
QWidget#centralWidget {
color: qlineargradient(spread:pad, x1:0 y1:0, x2:1 y2:0,
stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));
background: qlineargradient(x1:0 y1:0, x2:1 y2:0,
stop:0 #ffc982, stop:1 #ff9982);
background-image: url(image.png);
background-repeat: no-repeat;
}''')
https://stackoverflow.com/questions/65877269
复制相似问题