0

I wrote a little app that controls an experiment on a robot over a serial line. I added a TKinter window that gave out a percent completed update. It works great, so long as TKinter stays the active window. As soon as I switch to Word or Chrome, it crashed. Here's an simplified example of what I'm doing

from Tkinter import *
import time

master = Tk()
w = Label(master, text="")
w.pack()
def complicatedwhileloop():
    m = 0
    while m < 100:
        texttodisplay = m*m 
        w.config(text=str(texttodisplay))
        master.update_idletasks() 
        m = m+1
        time.sleep(1)
b = Button(master, text="OK", command=complicatedwhileloop)
b.pack()
mainloop()

How can I make it so that I can monitor the progress but still use other applications.

Daniel Sims
  • 41
  • 1
  • 6
  • 1
    What platform? Also, likely unrelated to the problem (but maybe not...), you should never call sleep in a gui. There are better ways to do what you want involving the use of tkinter's `after` command. See, for example, http://stackoverflow.com/q/2400262/7432 – Bryan Oakley Jul 02 '14 at 18:30
  • 1
    Putting time.sleep() in any GUI program is almost always a bad idea have you looked at .after()? – Matt Jul 02 '14 at 18:32
  • 2
    Have you tried `master.mainloop()` ? – jcfollower Jul 02 '14 at 20:45
  • You guys miss understand, the time.sleep(1) is just to simulate all the code that moves the robot arms, takes a sample and writes it to a file. I wanted a compact example, there is no sleep in the real code. @Bryan Windows 7 – Daniel Sims Jul 03 '14 at 01:24
  • I would recommend using a StringVar for the test to make it easier, not what you're looking for but just a suggestion. – Dan Alexander Jul 03 '14 at 07:23
  • I think I just herped and derped on this code. I'll rewrite it using the after command. – Daniel Sims Jul 15 '14 at 22:23
  • Doing root.update_idletasks() root.update() root.update_idletasks() seems to fix it – Daniel Sims Jul 17 '14 at 20:26

0 Answers0