0

The following code behaves differently in python 2.7.11 (after installing future) and 3.4.4

import tkinter as tk
from tkinter import ttk
import threading
from time import sleep

def do_something_slow():
    sleep(5)
    progressbar.grid_remove()

app = tk.Tk()
progressbar = ttk.Progressbar(app, mode='indeterminate')
progressbar.grid()
progressbar.start()
threading.Thread(target=do_something_slow).start()
app.mainloop()

In python 3.4.4 it shows a window with a progress bar that runs for 5 seconds, then disappears, but in python 2.7.11 it shows a window with a progress bar that runs for 5 seconds, then the window becomes unresponsive

I think the reason that it goes wrong when using python 2.7 is because I shouldn't be calling tkinter widgets on a different thread (tkinter is not thread safe - I should be using queues to communicate between the threads), but then why does it work in python 3.4? Is that version thread safe?

Grezzo
  • 2,220
  • 2
  • 22
  • 39
  • 2
    Briefly: no, it is not thread-safe. – TigerhawkT3 Oct 26 '16 at 11:12
  • 1
    By the way, you will have an easier time adding delays with `app.after()` rather than `threading` and `time.sleep()`. – TigerhawkT3 Oct 26 '16 at 11:13
  • Thanks, I'll read into `after()`, but I used a sleep in a thread for a reason; the code is greatly simplified but just enough to show the behaviour I'm seeing in my more complicated app – Grezzo Oct 26 '16 at 11:16
  • I expected the answer to be no, but I was hoping for an explanation as to why it works under 3.4.4 – Grezzo Oct 26 '16 at 11:20
  • 1
    An application that is thread-safe must necessarily share threads properly, but an application that is not thread-safe will not necessarily share threads improperly. – TigerhawkT3 Oct 26 '16 at 11:32
  • So it's just lucky that is works in 3.4.4? OK, that's a fair explanation I suppose. – Grezzo Oct 26 '16 at 13:09
  • As far as I can tell, it's still not thread-safe and it just happened to not break here, but I can reopen if you're wanting a more definite, detailed explanation. – TigerhawkT3 Oct 26 '16 at 13:17
  • No, I'm not expecting too details of an answer. It's fine to leave it closed. Thanks for your helpful comments. – Grezzo Oct 26 '16 at 13:18

0 Answers0