从QTimer对象捕获最终信号

时间:2018-10-09 12:41:26

标签: python-3.x pyqt5

我有一个简单的PyQt脚本。当我单击一个按钮时,它将启动QTimer对象并增加进度条。我想要的是在进度条达到100%时更改文本标签。它曾经对我有用,但我再也无法使用了。我在做什么错了?

这是我代码的主要部分。

df = pd.DataFrame({
    'Rk': [10, 540,541,1135,1136,1713,1714,2000],

})

labs = ['2017/2018','2016/2017','2015/2016', '0']
df['Year'] = pd.cut(df['Rk'], bins=[-np.inf,540, 1135, 1713, np.inf], labels=labs)
print (df)
     Rk       Year
0    10  2017/2018
1   540  2017/2018
2   541  2016/2017
3  1135  2016/2017
4  1136  2015/2016
5  1713  2015/2016
6  1714          0
7  2000          0

2 个答案:

答案 0 :(得分:1)

这不起作用,因为您正在连接计时器destoryed信号,但是计时器并未被破坏。要按原样使用此代码,请在停止计时器后调用self.timerObject.deleteLater()

答案 1 :(得分:1)

仅当您删除对象时才会发出

destroyed,QTimer对等点并不意味着它已从内存中删除,因此它不会发出该信号,一种可能的解决方案是为{ {1}},当该值取最大值时,如下所示:

QProgressBar

另一个最佳选择是使用import sys from PyQt5 import QtCore, QtWidgets class ProgressBar(QtWidgets.QProgressBar): finished = QtCore.pyqtSignal() def __init__(self, *args, **kwargs): super(ProgressBar, self).__init__(*args, *kwargs) self.valueChanged.connect(self.on_valueChanged) @QtCore.pyqtSlot(int) def on_valueChanged(self, val): if val == self.maximum(): self.finished.emit() class MainWindow(QtWidgets.QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle('QProgressBar demo') self.timerButton = QtWidgets.QPushButton("Start", self) self.timerButton.clicked.connect(self.timerStart) self.timerObject = QtCore.QTimer(self) self.progressBar = ProgressBar(self) self.progressBar.finished.connect(lambda: print("Called" )) self.increment = 0 lay = QtWidgets.QVBoxLayout(self) lay.addWidget(self.progressBar) lay.addWidget(self.timerButton) self.resize(300, 300) @QtCore.pyqtSlot() def headsUp(self): if self.increment >= 100: self.timerObject.stop() else: self.increment += 1 self.progressBar.setValue(self.increment) @QtCore.pyqtSlot() def timerStart(self): if self.timerObject.isActive(): self.timerObject.stop() self.timerButton.setText("Resume") else: self.timerObject.timeout.connect(self.headsUp) self.timerButton.setText("Pause") self.timerObject.start(100) if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) w = MainWindow() w.show() sys.exit(app.exec_()) 及其QTimeLine信号:

finished