[Tutor] (no subject)
Glen Bunting
Glen@ihello-inc.com
Wed, 11 Apr 2001 10:27:42 -0700
Daniel,
Your version works perfectly except for one thing, when I click on the quit
button, nothing happens. I will continue to use the program the way you
wrote it, but I have a few questions about the way I was trying to write it.
1: The after() method does not redraw or refresh the speed variable, all it
does is reprint the variable below the original after the time specified.
2: As long as Tkinter.mainloop() remains in the while loop, the while loop
does not continue until after I quit Tkinter. Once I do that the loop
continues. If I take Tkinter.mainloop out of the while loop, the loop
continues like it should , but the gui never pops up.
Any suggestions?
The next thing I might be interested in doing is to graph the results. Is
that the next logical step, or would that be a little to advanced for a
newbie?
Thanks
Glen
-----Original Message-----
From: Daniel Yoo [mailto:dyoo@hkn.eecs.berkeley.edu]
Sent: Wednesday, April 11, 2001 2:10 AM
To: Glen Bunting
Cc: 'tutor@python.org'
Subject: Re: [Tutor] (no subject)
On Tue, 10 Apr 2001, Glen Bunting wrote:
> Below is a simple script I wrote to download a file, time how long it
takes
> to download and display the bandwidth usage in Tkinter. It works
initially.
> But the while loop does not work. After the initial run, the program just
> hangs there and I cannot even exit out of the Tkinter that pops up. What
am
> I doing wrong?
When we hit the mainloop() command, Tkinter takes control, and doesn't
exit the mainloop() function until we shut down the gui by closing all
windows (or calling quit()). It's meant to be the last command in a
program, so that's what's causing the while loop to "hang".
It looks like you want to update your display every once in a while: try
using the after() method: it tells Tkinter to call a function "after" a
certain period of time. For example:
###
from Tkinter import *
root = Tk()
def addLabelContinuously():
Label(root, text='hola').pack()
root.after(1000, addLabelContinuously)
addLabelContinuously()
mainloop()
###
is a funny little program that, after every second, pack()s in a new label
into our main window. Not very useful, but it illustrates how to use
after() a little bit.
I hope you don't mind, but I wanted to try converting your code with OOP.
As a result, I've mutilated your code somewhat beyond recognition.
###
from Tkinter import *
import os, urllib, time
class GUI(Frame):
def __init__(self, root):
Frame.__init__(self, root)
self.speedvar = StringVar()
self.updateSpeedvar()
Label(self, text='Kilobytes/sec:').pack()
Label(self, textvariable=self.speedvar).pack()
Button(self, text='QUIT', command=self.quit,
bg='red', fg='white').pack()
def updateSpeedvar(self):
first = time.time()
urllib.urlretrieve('http://www.esynch.com/mw_test.dat', 'test')
second = time.time()
speed1 = second - first
print speed1
speed = round(speed1, 1)
self.speedvar.set(speed)
self.after(500, self.updateSpeedvar)
if __name__ == '__main__':
root = Tk()
gui = GUI(root)
gui.pack()
mainloop()
###