Reputation: 185
I am working on a python application with a Gtk3 interface and have encountered an issue that I would like some help with. A button click signal runs an 'on_clicked' function which in turn calls a 'filechooser' function. The filechooser function displays a Gtk.FileChooserDialog to select a file and returns the selected filepath. Then the 'on_clicked' function performs an action which takes some time to complete.
My issue is that the FileChooserDialog remains open and inactive until the 'on_clicked' function completes the time-consuming action.
Sample code:
def on_clicked(self, widget):
print("selecting file") #This prints out okay
filepath = self.filechooser()
print(filepath) #The filepath is printed even though the dialog remains open.
# Do something that takes several seconds to complete
time.sleep(10) # FileChooserDialog remains open for 10 seconds
# At this point the dialog closes.
print("Wake Up Charlie!")
What I would like is for the FileChooserDialog to close when the filechooser function calls dialog.destroy(), rather than after the on_clicked function that calls it has completed its activity. I have tried calling the time-consuming activity in a different thread but the FileChooserDialog still remained open until the activity was completed.
def on_clicked(self, widget):
print("selecting file")
filepath = self.filechooser()
#self.take_time(filepath)
thread = threading.Thread(target=self.taking_time(filepath))
thread.daemon = True
thread.start()
# the taking_time function completes as expected but the FileChooserDialog stays open while it is running.
Any assistance to clarify what I am doing wrong here would be appreciated.
Upvotes: 0
Views: 35
Reputation: 54718
When you do threading.Thread(target=self.taking_time(filepath))
, you are NOT passing the function object. Instead, you are CALLING the function. It will run in-line, taking up real time, and the return from that function is passed to Thread
. You need to set target=self.taking_time
with args=[filepath]
.
thread = threading.Thread(target=self.taking_time, args=[filepath])
Upvotes: 0