PyQt - Link 背景色到字符串

PyQt - Link background-color to a string

您好,我正在通过循环创建一个按钮网格,并尝试对它们应用不同的颜色。 我的代码是这样的:

# Set Colors
colornumber = 24
color = ["red", "blue", "purple", "yellow", "red", "red", "red", "blue", "purple", "yellow", "red", "white", "red", "blue", "purple", "yellow", "red", "red", "red", "blue", "purple", "yellow", "red", "red"]

# Set Lists
z = []
w = []
self.button = {}

# Set Loops
for i in range(4):
    for j in range (4):
        z.append(i)
        w.append(j)

for (a, b, c) in izip_longest (z, w, color):


                # Keep Buttons Reference
                self.button[(a, b)] = QtWidgets.QPushButton() 
                self.button[(a, b)].setStyleSheet("background-color: (%s %(c))")
                
                # Add to the Layout
                self.grid.addWidget(self.button[(a, b)], a, b)

因此,a 和 b 打印位置值,c 正确地迭代我的颜色列表。

现在我正在尝试使用 setStyleSheet 为按钮设置颜色,但无法正常工作。 正如您在代码中看到的那样,我尝试通过 %s 调用它们,但它不起作用,可能是因为周围的撇号 ?有解决办法吗?

谢谢!

我成功了 :) %s 但是第二个获取数据的数据需要在撇号之外..

                self.button[(a, b)].setStyleSheet("background-color: %s ;" %c)