【问题标题】:PyQt - Link background-color to a stringPyQt - 将背景颜色链接到字符串
【发布时间】:2020-12-09 16:41:26
【问题描述】:

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

# 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 调用它们,但它不起作用,可能是因为周围的撇号?有解决办法吗?

谢谢!

【问题讨论】:

  • 我不是 PyQt 专家,但你为什么不用f"background-color: {c}" 呢?
  • 嘿,我试过这种方式d = 'red' self.button.setStyleSheet("background-color: {d}"),但它似乎也没有获取相应的数据..

标签: string loops colors background


【解决方案1】:

我得到了它的工作 :) %s 但第二个获取需要在撇号之外的数据..

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 2015-07-07
    • 2015-04-29
    • 2013-06-24
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    相关资源
    最近更新 更多