0

I have a Tkinter-GUI in Python 3.3 with a progress bar and a button that starts another thread. This thread fills the progress bar within 10 seconds and also prints the numbers from 0 to 99 on the console during this time.
It works as it is supposed to as long as I am not moving or resizing the GUI window. If I delete the marked line progress.step(1) so that the worker-thread doesn't touch the progress bar and as a consequence doesn't affect the GUI at all, it prints the numbers continuously on the console even if I am manipulating the window.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from tkinter import *
import tkinter.ttk as ttk
import _thread
import time

root = Tk()

def start():
    _thread.start_new_thread(thread, ())

def thread():
    for i in range(0, 100):
        time.sleep(0.1)
        progress.step(1)     #<-----
        print(i)

progress = ttk.Progressbar()
progress.pack()
button = Button(root, text="Start", command=start)
button.pack()

root.mainloop()

Why is this happening and what is usually done to avoid worker-thread interruption by GUI manipulation?

Thanks in advance!

S818
  • 391
  • 3
  • 15

1 Answers1

2

IIRC, then this depends on the window manager. Some lock the window (and all children and that includes your progress bar) while you move/resize the window.

Newer window managers like KDE allow a program to continue to render into its window even while the window is being manipulated.

[EDIT] Most UI frameworks are not thread safe. UI frameworks used from Python are sometimes exempt since Python has the GIL which makes sure that only a single thread ever changes global state (i.e. widget attributes).

There are a couple of related questions how to use multiple threads with Tkinter:

In a nutshell, you shouldn't do any UI related work in a thread. Instead, send events to the mainloop.

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Ahh, darn. I am using Windows 7... So I guess I have to get along with that problem with Python+Tkinter on this os? Seems strange because Windows indeed is capable of rendering things and working in the background while manipulating GUIs. The music doesn't stop if I move WMP over the screen. Do you know if this error occurs with other GUI libraries, too? (By the way, I know that WMP isn't writton in Python! ;-)) – S818 Jul 23 '14 at 15:36
  • :-/ I agree that it should work on Windows in principle. – Aaron Digulla Jul 23 '14 at 15:42
  • Hm. Does my code break any Python programmer's conventions? Anything I can't think of? Would you code it in a different way? (I'm not the most experienced programmer myself.) – S818 Jul 23 '14 at 15:52
  • I just managed to put the GUI into a subordinate thread itself, but that didn't change anything. – S818 Jul 23 '14 at 16:22