Problem with timing, again:

James Logajan JamesL at Lugoj.Com
Sun Jul 29 02:43:17 EDT 2001


Jeremy Moles wrote:
> 
> I had an earlier post about timing in a program. Well, this is what I have
> so far - exactly. It works, but it won't respond, and it won't let the
> button go. What am I doing wrong?

You should have the dostuff function return, not enter an infinite loop.
Instead of going into an infinite loop, you should look into using the
"after" widget method. That would allow you to schedule future actions. So
if you delete the 3 line starting at "while 1" and insert instead the lines:

      domorestuff()
      self.hi_there.after(15000, domorestuff)

you might have better luck. However, if domorestuff get shung up for any
reason (such as taking a long time to resolve a host name or to transfer
data) then things will hang a while. Other options include using threads or
look into creative use of the "createfilehandler" mainloop hook. One
reference on these techniques is chapter 18 of "Python and Tkinter
Programming" by John E. Grayson. This is an "okay" book that should be worth
investing in if you keep running into other Tkinter development issues.

> 
> ____________________________________________
> 
> from Tkinter import *
> 
> class App:
> 
>     def __init__(self, master):
> 
>         frame = Frame(master)
>         frame.pack()
> 
>         self.hi_there = Button(frame, text='Upload',
> command=self.dostuff).pack()
> 
>     def dostuff(self):
>       import socket,ftplib,time
>       x=socket.gethostbyname(socket.gethostname())
>       y=open('D:\\Python\\IP.html','w')
>       y.write(x)
>       y.close()
>       def domorestuff():
>          z=ftplib.FTP(host='ahost')
>          z.login(user='asuer',passwd='apassword')
>          z.cwd('public_html')
>          z.storlines('STOR IP.html',open('D:\\Python\\IP.html','r'))
>          z.close()
>       while 1:
>          domorestuff()
>          time.sleep(15)
> 
> root = Tk()
> root.title('IPUL')
> 
> app = App(root)
> 
> root.mainloop()
> 
> _______________________________________________
> 
> Anyways, the program works - but the gui itself freezes up - it's as if the
> button is stuck. You guys were so helpful last time, any advice this time?



More information about the Python-list mailing list