我创建了一个带按钮的GUI。单击按钮时,将打开QDialog框并提示您打开或关闭。当用户选择其中一个选项时,我需要在QWidget中定义的按钮来改变颜色。我知道如何在初始点击时更改颜色,但我需要根据QDialog框选择它。
这是按钮:
self.brkr1_A = QtWidgets.QPushButton(self.centralwidget)
self.brkr1_A.setGeometry(QtCore.QRect(120, 70, 40, 40))
font = QtGui.QFont()
font.setFamily("Times New Roman")
self.brkr1_A.setFont(font)
self.brkr1_A.setStyleSheet("background-color: red;")
self.brkr1_A.setObjectName("brkr1_A")
############ Button Event ##############
self.brkr1_A.clicked.connect(self.clickA1)
########################################
这是按钮事件调用的内容,位于MainWindow:
def clickA1(self):
if not hasattr(self, 'dialogA1'):
self.dialogA1= WindowA1(self)
self.dialogA1.show()
print("A1 Clicked")
这是提示用户打开或关闭的QDialog:
class WindowA1(QDialog):
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
self.label = QLabel("Ace 1", self)
self.label.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.button = QPushButton("Open", self)
self.button2 = QPushButton("Close", self)
self.layout = QGridLayout()
self.layout.addWidget(self.label, 0, 0)
self.layout.addWidget(self.button, 0, 1)
self.layout.addWidget(self.button2, 1, 1)
self.setWindowModality(QtCore.Qt.ApplicationModal)
self.setWindowTitle("Power Lab 4.0")
self.setLayout(self.layout)
self.show()
################# Button Events #################
self.button.clicked.connect(self.OpenBreakerA1)
self.button2.clicked.connect(self.CloseBreakerA1)
######################################################
# OpenBreaker is where you will communicate with arduino
def OpenBreakerA1(self):
print('Opening A1')
def CloseBreakerA1(self):
print('Closing A1')
这就是GUI的样子,如果我附上所有代码,你们会讨厌我: brkr1_A is breaker 1 on Ace