如何将 QComboBox 中的文本存储在全局变量中
How to store text from QComboBox in a global variable
我有以下代码,在 Whosebug 上筛选答案后,我无法设法使它们适应我的(非常简单的)代码。
这将创建一个 window,其中包含两个下拉菜单(一个选择月份,另一个选择年份)和一个用于开始脚本其余部分的按钮。
我需要将组合框的 "selection" 存储在全局变量中以供脚本的其余部分使用。
我不确定这是否是最优雅的写法,甚至是最好的方法。
我不确定是否需要将其封装在某种 class 中,但到目前为止我还没有成功。下面的代码目前只是 returns 起始文本,而不是用户在下拉列表中选择的文本。
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_()
分析一下如果你需要一个class或者不难分析你提供的东西,我也推荐阅读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)
我有以下代码,在 Whosebug 上筛选答案后,我无法设法使它们适应我的(非常简单的)代码。 这将创建一个 window,其中包含两个下拉菜单(一个选择月份,另一个选择年份)和一个用于开始脚本其余部分的按钮。
我需要将组合框的 "selection" 存储在全局变量中以供脚本的其余部分使用。
我不确定这是否是最优雅的写法,甚至是最好的方法。
我不确定是否需要将其封装在某种 class 中,但到目前为止我还没有成功。下面的代码目前只是 returns 起始文本,而不是用户在下拉列表中选择的文本。
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_()
分析一下如果你需要一个class或者不难分析你提供的东西,我也推荐阅读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)