1

I've got the following code, and after sieving through the answers on StackOverflow, I can't manage to adapt them to my (very simple) code. This creates a window with two drop downs (one selecting a month, and the other a year) and a button to commence the rest of the script.

I need to store the "selection" of the Combo Box in a global variable for use in the rest of the script.

I'm not sure if this is the most elegantly written, or even the best way to do this.

I'm not sure if I need to encapsulate this in a class of some sort, but I've had no luck so far. The code below currently just returns the starting text, rather than user-selected text in the dropdown.

def runapp():
    def on_button_clicked():
        startprocessing()

    app = QApplication([])
    app.setStyle('Fusion')
    window = QWidget()
    layout = QVBoxLayout()
    combobox_month = QComboBox()
    combobox_year = QComboBox()
    progress = QLabel('Test')
    layout.addWidget(progress)
    layout.addWidget(combobox_month)
    layout.addWidget(combobox_year)
    combobox_month.addItems(calendar.month_name)
    combobox_year.addItems(['2017', '2018', '2019'])
    processbutton = QPushButton('Process')
    layout.addWidget(processbutton)
    global month
    month = str(combobox_month.currentText())
    global year
    year = str(combobox_year.currentText())
    processbutton.clicked.connect(on_button_clicked)
    window.setLayout(layout)
    window.show()
    app.exec_()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
jbentley
  • 163
  • 4
  • 13

1 Answers1

1

Analyze if you need a class or is not difficult to analyze what you provide, I also recommend reading Why are global variables evil? because you may be abusing the global variables. Going to the problem, you have to update the value of the variable by connecting the slot to the currentTextChanged signal:

from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QComboBox, QLabel, QPushButton
from PyQt5.QtCore import pyqtSlot

month = ""
year = ""

def runapp():
    def on_button_clicked():
        # startprocessing()
        print("process")

    app = QApplication([])
    app.setStyle('Fusion')
    window = QWidget()
    layout = QVBoxLayout()
    combobox_month = QComboBox()
    combobox_year = QComboBox()
    progress = QLabel('Test')
    layout.addWidget(progress)
    layout.addWidget(combobox_month)
    layout.addWidget(combobox_year)
    combobox_month.addItems(calendar.month_name)
    combobox_year.addItems(['2017', '2018', '2019'])
    processbutton = QPushButton('Process')
    layout.addWidget(processbutton)
    @pyqtSlot(str)
    def on_combobox_month_changed(text):
        global month
        month = text

    @pyqtSlot(str)
    def on_combobox_year_changed(text):
        global year
        year = text
    combobox_month.currentTextChanged.connect(on_combobox_month_changed)
    combobox_year.currentTextChanged.connect(on_combobox_year_changed)
    processbutton.clicked.connect(on_button_clicked)
    window.setLayout(layout)
    window.show()
    app.exec_()

if __name__ == '__main__':
    runapp()
    print(month, year)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thanks, works perfectly! I'll work around getting rid of the global variables after reading your link - perhaps by returning them from the function and pulling them in as parameters into my other functions elsewhere in the script? – jbentley May 01 '19 at 13:57