I'm trying to toggle on/off a button based on the value of control variables (choice1_is_selected
& choice2_is_selected
), which are defined as global.
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()
What is tripping me is that the code seems to check the conditional statement at the start but not later so it never enables the download option.
CodePudding user response:
It doesn't work because your if statement no longer works after execution. You should call that statement again (using signals, calling the function, etc.).
Here is a possible solution that worked for me:
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()