简体   繁体   English

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

[英]How do I change the color of a PushButton when another button is pressed(PyQT5, QT)

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.我通过创建一个名为 changeSymbol 的全局布尔变量来做到这一点,并且当 changeSymbol 返回 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]这是我的代码:[编辑由于复制和粘贴困难(我也是堆栈溢出的新手),我将发布屏幕截图] 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

在此处输入图像描述

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.如果您必须将代码保存在单独的函数中,那么您可以使用self.changeMode而不是全局变量。 And also run Clear_changed() in `Change_pressed()并在 `Change_pressed() 中运行Clear_changed() ()

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"
                                 "}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 PyQt5 中制作按钮图标以根据按钮是否被按下来改变它的颜色 - How to make pushbutton's icon in PyQt5 to change it's color dependent on if the button is pressed PyQt5 QPushButton setSyleSheet 按下时不改变按钮颜色 - PyQt5 QPushButton setSyleSheet does not change button color when pressed Pyqt5检查按钮颜色并更改按钮颜色 - Pyqt5 Check pushbutton color and change pushbutton color 在 PyQt5 中,如何从 QStackedWidget 上的 label 单击按钮时移动到另一个 label? - In PyQt5, how do I move to another label when clicking a button from a label on a QStackedWidget? 在 PyQt5 中按下时如何切换按钮文本 - How to toggle button text when pressed in PyQt5 在pyqt5中按下按钮时如何创建新标签 - how to create a new label when a button is pressed in pyqt5 如何单击 PyQt5 代码上的按钮并允许它执行/运行另一个 .py 文件? - How can I click a pushButton on my PyQt5 code and allow it to execute/run another .py file? 当在 PyQt5 中的不同 class 中按下按钮时,如何更新图形? - How can I update a figure when button is pressed in a different class in PyQt5? 按下按钮时获取PyQt4打开另一个Qt窗口 - Get PyQt4 to open another Qt window when a button is pressed PyQt5 - 如何识别按下了哪个按钮 - PyQt5 - How to recognize which button is pressed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM