make 2 updates to label from 1 slot
-
Hello, am making an application, with a QPushButton, when the button is pressed its should set a label's text to 'a' and then after 3 seconds it should set it to 'b'
I have implemented this in the slot connected to the pushbutton' s click. However whenever I click the button it just sets to 'b' i don't see it setting to a'a' before going to 'b'. -
def greeting(): """Slot function.""" btn.setEnabled(False) msg.setText('a.........................................') sleep(3) msg.setText('b') btn.setEnabled(True) app = QApplication(sys.argv) window = QWidget() window.setWindowTitle('Signals and slots') layout = QVBoxLayout() btn = QPushButton('Greet') msg = QLabel('') btn.clicked.connect(functools.partial(greeting)) # Connect clicked to greeting layout.addWidget(btn) layout.addWidget(msg) window.setLayout(layout) window.show() sys.exit(app.exec_())
i dont think its a problem with the sleep()
even without the sleep.... any the last modifications i make in the slot take action
i cant make multiple changes to a widget from a slot. only the last change is effected.
even the button is not disabled.@gblessed your sleep() call blocks the event loop. That means for 3 seconds your UI will NOT be updated! You never do such things in event driven applications! You really have to learn this.
The explanation why you don't see button disabled without sleep() is very simple: you disable the button and immediately enable it again.To solve your issue do what @JonB already suggested: use QTimer. Remove the sleep() and both lines bellow it and use a QTimer with 3 seconds timeout. In the timeout slot you put the last two lines from greeting().
-
Hi,
How did you implement the three seconds delay ?
-
def greeting(): """Slot function.""" btn.setEnabled(False) msg.setText('a.........................................') sleep(3) msg.setText('b') btn.setEnabled(True) app = QApplication(sys.argv) window = QWidget() window.setWindowTitle('Signals and slots') layout = QVBoxLayout() btn = QPushButton('Greet') msg = QLabel('') btn.clicked.connect(functools.partial(greeting)) # Connect clicked to greeting layout.addWidget(btn) layout.addWidget(msg) window.setLayout(layout) window.show() sys.exit(app.exec_())
i dont think its a problem with the sleep()
even without the sleep.... any the last modifications i make in the slot take action
i cant make multiple changes to a widget from a slot. only the last change is effected.
even the button is not disabled. -
def greeting(): """Slot function.""" btn.setEnabled(False) msg.setText('a.........................................') sleep(3) msg.setText('b') btn.setEnabled(True) app = QApplication(sys.argv) window = QWidget() window.setWindowTitle('Signals and slots') layout = QVBoxLayout() btn = QPushButton('Greet') msg = QLabel('') btn.clicked.connect(functools.partial(greeting)) # Connect clicked to greeting layout.addWidget(btn) layout.addWidget(msg) window.setLayout(layout) window.show() sys.exit(app.exec_())
i dont think its a problem with the sleep()
even without the sleep.... any the last modifications i make in the slot take action
i cant make multiple changes to a widget from a slot. only the last change is effected.
even the button is not disabled.@gblessed said in make 2 updates to label from 1 slot:
i dont think its a problem with the sleep()
And you would be wrong about that.
-
def greeting(): """Slot function.""" btn.setEnabled(False) msg.setText('a.........................................') sleep(3) msg.setText('b') btn.setEnabled(True) app = QApplication(sys.argv) window = QWidget() window.setWindowTitle('Signals and slots') layout = QVBoxLayout() btn = QPushButton('Greet') msg = QLabel('') btn.clicked.connect(functools.partial(greeting)) # Connect clicked to greeting layout.addWidget(btn) layout.addWidget(msg) window.setLayout(layout) window.show() sys.exit(app.exec_())
i dont think its a problem with the sleep()
even without the sleep.... any the last modifications i make in the slot take action
i cant make multiple changes to a widget from a slot. only the last change is effected.
even the button is not disabled.@gblessed your sleep() call blocks the event loop. That means for 3 seconds your UI will NOT be updated! You never do such things in event driven applications! You really have to learn this.
The explanation why you don't see button disabled without sleep() is very simple: you disable the button and immediately enable it again.To solve your issue do what @JonB already suggested: use QTimer. Remove the sleep() and both lines bellow it and use a QTimer with 3 seconds timeout. In the timeout slot you put the last two lines from greeting().