[英]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.