【问题标题】:How to store text from QComboBox in a global variable如何将 QComboBox 中的文本存储在全局变量中
【发布时间】:2019-09-19 23:54:49
【问题描述】:

我有以下代码,在通过 StackOverflow 上的答案筛选后,我无法将它们调整为我的(非常简单的)代码。 这将创建一个带有两个下拉菜单的窗口(一个选择月份,另一个选择年份)和一个按钮来开始脚本的其余部分。

我需要将组合框的“选择”存储在一个全局变量中,以便在脚本的其余部分中使用。

我不确定这是不是写得最优雅的,甚至是最好的方法。

我不确定是否需要将它封装在某种类中,但到目前为止我还没有运气。下面的代码目前只返回起始文本,而不是下拉列表中用户选择的文本。

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_()

【问题讨论】:

    标签: python pyqt pyqt5 qcombobox


    【解决方案1】:

    分析你是否需要一个类或者不难分析你提供的内容,我也推荐阅读Why are global variables evil?,因为你可能在滥用全局变量。进入问题,您必须通过将插槽连接到 currentTextChanged 信号来更新变量的值:

    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)
    

    【讨论】:

    • 谢谢,完美运行!阅读您的链接后,我将努力摆脱全局变量 - 也许通过从函数中返回它们并将它们作为参数拉入脚本中其他地方的其他函数?
    猜你喜欢
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    • 2016-01-24
    相关资源
    最近更新 更多