Pyqt5 List QtPushbutton 添加边框图像
Pyqt5 List QtPushbutton add border-image
我正在使用 Python 3 ( Pycharm ) 和 Pyqt5 (Windows 10)
我需要使用一个QPushbutton列表。
我设法改变了按钮的颜色,没关系。
尽管如此,我无法将边框图像(相同QT)的图像更改为我添加按钮的功能(Add_Buttons_KA_IU)
我的曲目结构:
My_Project :
|-> Vision_Room.py
|-> Ressource -> LightOff.png
import sys
from PyQt5.QtWidgets import QMainWindow,QWidget,QLabel,QLineEdit,QPushButton,QApplication,QVBoxLayout
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindows(QMainWindow):
buttons_KA, keydata_KA = {}, {}
Positions_Button_KA = [(330, 70, 10, 20),(270, 110, 10, 20),(300, 110, 10, 20),(360, 110, 10, 20),(330, 150, 10, 20),
(180, 190, 10, 20),(240, 190, 10, 20),(300, 190, 10, 20),(360, 190, 10, 20),(210, 230, 10, 20),
(270, 230, 10, 20),(330, 230, 10, 20),(180, 270, 10, 20),(240, 270, 10, 20),(270, 270, 10, 20),
(300, 270, 10, 20),(360, 270, 10, 20),(210, 310, 10, 20),(330, 310, 10, 20),(180, 350, 10, 20),
(240, 350, 10, 20),(300, 350, 10, 20),(360, 350, 10, 20),(210, 390, 10, 20),(270, 390, 10, 20),
(330, 390, 10, 20),(180, 430, 10, 20),(240, 430, 10, 20),(300, 430, 10, 20),(360, 430, 10, 20)]
def __init__(self):
super(Ui_MainWindows,self).__init__()
#self.layout = QVBoxLayout()
self.resize(1280,960)
self.centralWidget = QWidget(self)
self.setCentralWidget(self.centralWidget)
self.setWindowTitle("Vision Room")
#self.setStyleSheet("background-color: rgb(0, 0, 0);")
self.Add_Buttons_KA_IU()
def Add_Buttons_KA_IU(self):
Name_Button = "Button_KA"
for i in range(0, 30):
Full_Name_Button = Name_Button + str(i)
print(Full_Name_Button)
b = self.buttons_KA[i] = QtWidgets.QPushButton(self.centralWidget)
b.setGeometry(QtCore.QRect(self.Positions_Button_KA[i][0],self.Positions_Button_KA[i][1],self.Positions_Button_KA[i][2],
self.Positions_Button_KA[i][3]))
#str_Style_Sheet = "#" + Full_Name_Button + "\n" "{\n""background-color: rgb(0, 0, 255);\n" "}"
str_Style_Sheet = "#" + Full_Name_Button + "\n" "{\n""border-image: url(:/Ressource/LightOff.png);\n" "}"
print(str_Style_Sheet)
b.setObjectName(Full_Name_Button)
b.setStyleSheet(str_Style_Sheet)
def main():
app = QApplication(sys.argv)
MainWindow = Ui_MainWindows()
MainWindow.show()
rc = app.exec_()
sys.exit(rc)
if __name__ == "__main__":
main()
首先如果你要给每个按钮添加QSS是没有必要使用objectName的,另一方面问题是因为你不明白“:”是什么意思,“:”表示您正在使用一个 qresource (.qrc),它是一个允许在 Qt 中普遍存储文件的文件,并且是一个虚拟路径,您不应该使用那些“:”,因为您没有使用它。另一方面,拥有文件的完整路径总是更好,以避免出现问题,但它也必须易于管理,因此解决方案是获取 .py 的路径并将其与资源的相对路径连接起来关于 .py,所以你可以移动你的项目,即使那样它也会工作。
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
dir_path = os.path.dirname(os.path.realpath(__file__))
positions_button_KA = [(330, 70, 10, 20),(270, 110, 10, 20),(300, 110, 10, 20),(360, 110, 10, 20),(330, 150, 10, 20),
(180, 190, 10, 20),(240, 190, 10, 20),(300, 190, 10, 20),(360, 190, 10, 20),(210, 230, 10, 20),
(270, 230, 10, 20),(330, 230, 10, 20),(180, 270, 10, 20),(240, 270, 10, 20),(270, 270, 10, 20),
(300, 270, 10, 20),(360, 270, 10, 20),(210, 310, 10, 20),(330, 310, 10, 20),(180, 350, 10, 20),
(240, 350, 10, 20),(300, 350, 10, 20),(360, 350, 10, 20),(210, 390, 10, 20),(270, 390, 10, 20),
(330, 390, 10, 20),(180, 430, 10, 20),(240, 430, 10, 20),(300, 430, 10, 20),(360, 430, 10, 20)]
class Ui_MainWindows(QtWidgets.QMainWindow):
def __init__(self):
super(Ui_MainWindows,self).__init__()
self.central_widget = QtWidgets.QWidget()
self.setCentralWidget(self.central_widget)
self.setWindowTitle("Vision Room")
self.buttons_KA = {}
self.add_buttons_KA_IU()
self.resize(1280, 960)
def add_buttons_KA_IU(self):
name_group = "button_KA"
for i, geom in enumerate(positions_button_KA):
b = QtWidgets.QPushButton(self.central_widget)
b.setGeometry(*geom)
path_image = os.path.join(dir_path, "Ressource/LightOff.png").replace("\", "/")
qss = 'border-image: url({})'.format(path_image)
b.setStyleSheet(qss)
self.buttons_KA[i] = b
def main():
app = QtWidgets.QApplication(sys.argv)
MainWindow = Ui_MainWindows()
MainWindow.show()
rc = app.exec_()
sys.exit(rc)
if __name__ == "__main__":
main()
Linux:
Windows:
我正在使用 Python 3 ( Pycharm ) 和 Pyqt5 (Windows 10)
我需要使用一个QPushbutton列表。 我设法改变了按钮的颜色,没关系。 尽管如此,我无法将边框图像(相同QT)的图像更改为我添加按钮的功能(Add_Buttons_KA_IU) 我的曲目结构:
My_Project :
|-> Vision_Room.py
|-> Ressource -> LightOff.png
import sys
from PyQt5.QtWidgets import QMainWindow,QWidget,QLabel,QLineEdit,QPushButton,QApplication,QVBoxLayout
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindows(QMainWindow):
buttons_KA, keydata_KA = {}, {}
Positions_Button_KA = [(330, 70, 10, 20),(270, 110, 10, 20),(300, 110, 10, 20),(360, 110, 10, 20),(330, 150, 10, 20),
(180, 190, 10, 20),(240, 190, 10, 20),(300, 190, 10, 20),(360, 190, 10, 20),(210, 230, 10, 20),
(270, 230, 10, 20),(330, 230, 10, 20),(180, 270, 10, 20),(240, 270, 10, 20),(270, 270, 10, 20),
(300, 270, 10, 20),(360, 270, 10, 20),(210, 310, 10, 20),(330, 310, 10, 20),(180, 350, 10, 20),
(240, 350, 10, 20),(300, 350, 10, 20),(360, 350, 10, 20),(210, 390, 10, 20),(270, 390, 10, 20),
(330, 390, 10, 20),(180, 430, 10, 20),(240, 430, 10, 20),(300, 430, 10, 20),(360, 430, 10, 20)]
def __init__(self):
super(Ui_MainWindows,self).__init__()
#self.layout = QVBoxLayout()
self.resize(1280,960)
self.centralWidget = QWidget(self)
self.setCentralWidget(self.centralWidget)
self.setWindowTitle("Vision Room")
#self.setStyleSheet("background-color: rgb(0, 0, 0);")
self.Add_Buttons_KA_IU()
def Add_Buttons_KA_IU(self):
Name_Button = "Button_KA"
for i in range(0, 30):
Full_Name_Button = Name_Button + str(i)
print(Full_Name_Button)
b = self.buttons_KA[i] = QtWidgets.QPushButton(self.centralWidget)
b.setGeometry(QtCore.QRect(self.Positions_Button_KA[i][0],self.Positions_Button_KA[i][1],self.Positions_Button_KA[i][2],
self.Positions_Button_KA[i][3]))
#str_Style_Sheet = "#" + Full_Name_Button + "\n" "{\n""background-color: rgb(0, 0, 255);\n" "}"
str_Style_Sheet = "#" + Full_Name_Button + "\n" "{\n""border-image: url(:/Ressource/LightOff.png);\n" "}"
print(str_Style_Sheet)
b.setObjectName(Full_Name_Button)
b.setStyleSheet(str_Style_Sheet)
def main():
app = QApplication(sys.argv)
MainWindow = Ui_MainWindows()
MainWindow.show()
rc = app.exec_()
sys.exit(rc)
if __name__ == "__main__":
main()
首先如果你要给每个按钮添加QSS是没有必要使用objectName的,另一方面问题是因为你不明白“:”是什么意思,“:”表示您正在使用一个 qresource (.qrc),它是一个允许在 Qt 中普遍存储文件的文件,并且是一个虚拟路径,您不应该使用那些“:”,因为您没有使用它。另一方面,拥有文件的完整路径总是更好,以避免出现问题,但它也必须易于管理,因此解决方案是获取 .py 的路径并将其与资源的相对路径连接起来关于 .py,所以你可以移动你的项目,即使那样它也会工作。
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
dir_path = os.path.dirname(os.path.realpath(__file__))
positions_button_KA = [(330, 70, 10, 20),(270, 110, 10, 20),(300, 110, 10, 20),(360, 110, 10, 20),(330, 150, 10, 20),
(180, 190, 10, 20),(240, 190, 10, 20),(300, 190, 10, 20),(360, 190, 10, 20),(210, 230, 10, 20),
(270, 230, 10, 20),(330, 230, 10, 20),(180, 270, 10, 20),(240, 270, 10, 20),(270, 270, 10, 20),
(300, 270, 10, 20),(360, 270, 10, 20),(210, 310, 10, 20),(330, 310, 10, 20),(180, 350, 10, 20),
(240, 350, 10, 20),(300, 350, 10, 20),(360, 350, 10, 20),(210, 390, 10, 20),(270, 390, 10, 20),
(330, 390, 10, 20),(180, 430, 10, 20),(240, 430, 10, 20),(300, 430, 10, 20),(360, 430, 10, 20)]
class Ui_MainWindows(QtWidgets.QMainWindow):
def __init__(self):
super(Ui_MainWindows,self).__init__()
self.central_widget = QtWidgets.QWidget()
self.setCentralWidget(self.central_widget)
self.setWindowTitle("Vision Room")
self.buttons_KA = {}
self.add_buttons_KA_IU()
self.resize(1280, 960)
def add_buttons_KA_IU(self):
name_group = "button_KA"
for i, geom in enumerate(positions_button_KA):
b = QtWidgets.QPushButton(self.central_widget)
b.setGeometry(*geom)
path_image = os.path.join(dir_path, "Ressource/LightOff.png").replace("\", "/")
qss = 'border-image: url({})'.format(path_image)
b.setStyleSheet(qss)
self.buttons_KA[i] = b
def main():
app = QtWidgets.QApplication(sys.argv)
MainWindow = Ui_MainWindows()
MainWindow.show()
rc = app.exec_()
sys.exit(rc)
if __name__ == "__main__":
main()
Linux:
Windows: