基于全局控制变量的切换按钮



我正试图根据定义为全局的控制变量(choice1_is_selected&choice2_is_selected(的值来打开/关闭按钮。

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QGridLayout, QHBoxLayout, QLabel, QComboBox, QPushButton
class duo(QWidget):
def __init__(self,text,choice):
super().__init__()
layoutC = QHBoxLayout()
layoutC.addWidget(QLabel(text))
options=QComboBox()
options.addItems(choice)
options.currentTextChanged.connect(self.text_changed)
layoutC.addWidget(options)
self.setLayout(layoutC)
def text_changed(self, s): 
global choice1_is_selected,choice2_is_selected
choice1_is_selected = True
class duoB(QWidget):
def __init__(self,text,choice):
super().__init__()
layoutB = QHBoxLayout()
layoutB.addWidget(QLabel(text))
options=QComboBox()
options.addItems(choice)
options.currentTextChanged.connect(self.text_changed)
layoutB.addWidget(options)
self.setLayout(layoutB)
def text_changed(self, s): 
global choice1_is_selected,choice2_is_selected
choice2_is_selected = True
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle("Reproducible")
layout = QGridLayout()
layout.addWidget(QLabel("Block 1"),0,0)
text2 = 'Block 2'
select2 = ['Not selected','first','second']
widgetcomposite1 = duo(text2,select2)
layout.addWidget(widgetcomposite1,1,0)
text3 = 'Block 3'
select3 = ['Unselected','I','II']
widgetcomposite2 = duoB(text3,select3)
layout.addWidget(widgetcomposite2,2,0)
self.btn_download = QPushButton('Download')
if (choice1_is_selected == True) and (choice2_is_selected == True):
self.btn_download.setDisabled(False)
else:
self.btn_download.setDisabled(True)
layout.addWidget(self.btn_download,3,0)
self.setLayout(layout)

app = QApplication(sys.argv)
choice1_is_selected = False
choice2_is_selected = False
window = MainWindow()
window.show()
app.exec()

让我感到困惑的是,代码似乎在开始时检查了条件语句,但后来没有检查,所以它从未启用下载选项。

它不起作用,因为if语句在执行后不再起作用。您应该再次调用该语句(使用信号、调用函数等(

以下是一个对我有效的可能解决方案:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QGridLayout, QHBoxLayout, QLabel, QComboBox, QPushButton
class duo(QWidget):
def __init__(self,parent, text,choice):
super().__init__(parent)
layoutC = QHBoxLayout()
layoutC.addWidget(QLabel(text))
options=QComboBox()
options.addItems(choice)
options.currentTextChanged.connect(self.text_changed)
layoutC.addWidget(options)
self.setLayout(layoutC)
def text_changed(self):
global choice1_is_selected,choice2_is_selected
choice1_is_selected = True
self.parent().button_is_selected()
class duoB(QWidget):
def __init__(self,parent,text,choice):
super().__init__(parent)
layoutB = QHBoxLayout()
layoutB.addWidget(QLabel(text))
options=QComboBox()
options.addItems(choice)
options.currentTextChanged.connect(self.text_changed)
layoutB.addWidget(options)
self.setLayout(layoutB)
def text_changed(self):
global choice1_is_selected,choice2_is_selected
choice2_is_selected = True
self.parent().button_is_selected()
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle("Reproducible")
layout = QGridLayout()
layout.addWidget(QLabel("Block 1"),0,0)
text2 = 'Block 2'
select2 = ['Not selected','first','second']
widgetcomposite1 = duo(self, text2,select2)
layout.addWidget(widgetcomposite1,1,0)
text3 = 'Block 3'
select3 = ['Unselected','I','II']
widgetcomposite2 = duoB(self, text3,select3)
layout.addWidget(widgetcomposite2,2,0)
self.btn_download = QPushButton('Download')
layout.addWidget(self.btn_download,3,0)
self.setLayout(layout)
self.button_is_selected()
def button_is_selected(self):
if (choice1_is_selected == True) and (choice2_is_selected == True):
self.btn_download.setDisabled(False)
else:
self.btn_download.setDisabled(True)

app = QApplication(sys.argv)
choice1_is_selected = False
choice2_is_selected = False
window = MainWindow()
window.show()
app.exec()

最新更新