按下另一个按钮时,如何更改按钮的颜色(pyqt5,qt)

发布于 2025-02-06 06:28:15 字数 811 浏览 1 评论 0原文

您好的应用程序,我正在尝试在按下不同按钮时更改一个按钮的颜色。

我正在通过创建一个名为Changeymbol的全局布尔变量来做到这一点,当更改符号返回true时,其中一个按钮将更改。

这是我的代码: [由于复制和粘贴困难(也是我的新手堆栈溢出),编辑我会发布屏幕截图]

”在此处输入图像说明”

Hello for my application, I am trying to change the color of one button when different button is pressed.

I am doing this by creating a global boolean variable called changeSymbol, and one of the buttons will change when the changeSymbol returns true.

Here is my code:
[EDIT Due to difficulties in copy and pasting (also I am new to stack overflow) I will post screenshots instead]
enter image description here
enter image description here
enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一口甜 2025-02-13 06:28:15

我不知道我是否理解问题,因为我看不到完整的代码。

也许您应该在一个函数中完成所有操作

def Change_pressed(self):
if self.Change.isChecked():
self.Change.setText("

I don't know if I understand problem because I can't see full code.

Maybe you should do all in one function

def Change_pressed(self):
    if self.Change.isChecked():
        self.Change.setText("????")
        self.Clear.setStyleSheet("QPushButton {\n"
                                 "  background-color: RGB (107, 128, 104) ;\n"
                                 "  border: 1px solid gray;\n"
                                 "}\n"
                                 "QPushButton:pressed {\n"
                                 "    background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
                                 "                                      stop: 0 #BEBEBE, stop: 1 #D7D7D7);\n"
                                 "}")

    else:
        self.Change.setText("????")
        self.Clear.setStyleSheet("QPushButton {\n"
                                 "  background-color: rgb(215, 215, 215);\n"
                                 "  border: 1px solid gray;\n"
                                 "}\n"
                                 "QPushButton:pressed {\n"
                                 "    background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
                                 "                                      stop: 0 #BEBEBE, stop: 1 #D7D7D7);\n"
                                 "}")

And if you have to keep code in separated functions then you could use self.changeMode instead of global variable. And also run Clear_changed() in `Change_pressed()

def Change_pressed(self):
    self.changeSymbol()
    self.Clear_changed()
    
def changeSymbol(self):
    if self.Change.isChecked():
        self.Change.setText("????")
        self.changeMode = True
    else:
        self.Change.setText("????")
        self.changeMode = False

def Clear_changed(self):
    if self.changeMode == True:
        self.Clear.setStyleSheet("QPushButton {\n"
                                 "  background-color: RGB (107, 128, 104) ;\n"
                                 "  border: 1px solid gray;\n"
                                 "}\n"
                                 "QPushButton:pressed {\n"
                                 "    background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
                                 "                                      stop: 0 #BEBEBE, stop: 1 #D7D7D7);\n"
                                 "}")
    else:
        self.Clear.setStyleSheet("QPushButton {\n"
                                 "  background-color: rgb(215, 215, 215);\n"
                                 "  border: 1px solid gray;\n"
                                 "}\n"
                                 "QPushButton:pressed {\n"
                                 "    background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\n"
                                 "                                      stop: 0 #BEBEBE, stop: 1 #D7D7D7);\n"
                                 "}")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文