运行时进度状态(如状态栏)我正在尝试为我的Pyqt5 Python代码创建简单的进度状态标签,并在函数执行一堆东西的循环的每次迭代后更新它。我想要更新的标签是“status_label_counter”。下面的代码只显示了创建标签的部分以及我想要使用我提到的功能的确切位置。python
#initialisation code, etc...
self.status_label_counter = QLabel()
self.status_label_from = QLabel(' from: ')
self.status_label_total = QLabel()
status_hbox = QHBoxLayout()
status_hbox.addStretch()
status_hbox.addWidget(self.status_label_counter)
status_hbox.addWidget(self.status_label_from)
status_hbox.addWidget(self.status_label_total)
status_hbox.addStretch()
#bunch of other code...
def create_ics(self):
counter = 0
self.status_label_total.setText(str(len(self.groups)))
for group in self.groups:
#does a bunch of stuff inside
group_manager.create_calendar_for(self.rows, group, self.term)
counter += 1
#for console output
print('iteration: ', counter)
#trying to update status counter
self.status_label_counter.setText(str(counter))
问题是,当循环完成嵌套函数时,我只看到两个标签的更新。当我点击一个按钮,要求“create_ics”功能窗口变为非活动状态约5秒时,我会看到控制台上的日志与迭代次数,但没有任何反应。
尝试QApplication.processEvents()循环 – eyllanesc
里面我也建议使用的QThread – eyllanesc