In my program I have a button that is connected to a function that takes about 15 seconds to run. I want to ignore all user inputs during this run time. When btn
is pressed the following function is called:
def btn_call(self) :
self.btn.setEnable(False) # disables the button and shows it greyed out
fn() # some function that takes ~15 seconds to run
self.btn.setEnable(True) # re-enables the button
The hope was to keep the program from responding to btn
presses while fn()
is running. Currently, if btn
is pressed while fn()
is running, fn()
will run for each time btn
was pressed.
Is there a way to clear all user inputs that occurred while fn()
runs?
Edit:
Added a MWE. If you click Run Function.
the function will begin. If you click Run Function.
while it is still running, the function will run a second time. This is the behaviour that I would like to stop.
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import sys
from time import sleep
def fn(num) :
for i in range(num) :
sleep(0.5)
yield i
class MainWindow(QWidget) :
def __init__(self) :
super().__init__()
self.layout = QVBoxLayout(self)
self.btn_fn = QPushButton("Run function.")
self.btn_fn.clicked.connect(self.run_fn)
self.layout.addWidget(self.btn_fn)
self.prog_bar = QProgressBar()
self.layout.addWidget(self.prog_bar)
self.show()
def run_fn(self) :
self.btn_fn.setEnabled(False)
num = 20
self.prog_bar.setValue( 0 )
for i in fn(num) :
self.prog_bar.setValue( 100*(i+1)/num )
self.btn_fn.setEnabled(True)
if __name__ == '__main__' :
app = QApplication(sys.argv)
window = MainWindow()
sys.exit( app.exec_() )