Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > A smallish Tkinter question

Reply
Thread Tools

A smallish Tkinter question

 
 
mediocre_person@hotmail.com
Guest
Posts: n/a
 
      04-21-2005
"""
What I want: A little window to open with a 0 in it. Every second, the
0 should increment by 1.
What I get: A one second delay, see the window with a 1 in it, and then
nothing appears to happen. Never see the 0, never see a 2. Any quick
clues? Thanks. Nick. (Python 2.4, Win9.
"""

from Tkinter import *
from time import sleep

class Clock:
def __init__(self, parent):
self.time = 0
self.display = Label(parent)
self.display["font"] = "Arial 16"
self.display["text"] = str(self.time)
self.display.pack()

def run(self): #also tried self,parent
sleep(1.0)
self.time = self.time + 1
self.display["text"] = str(self.time)

win = Tk()
app = Clock(win)
app.run() #also tried run(win) with self,parent above
win.mainloop()

 
Reply With Quote
 
 
 
 
Kent Johnson
Guest
Posts: n/a
 
      04-21-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> """
> What I want: A little window to open with a 0 in it. Every second, the
> 0 should increment by 1.
> What I get: A one second delay, see the window with a 1 in it, and then
> nothing appears to happen. Never see the 0, never see a 2. Any quick
> clues? Thanks. Nick. (Python 2.4, Win9.


Your run() method doesn't loop, so it only updates once. But there is a fundamental problem; the
run() method is called before you even start Tkinter by calling mainloop(). That's why you never see
the 0. If you change run to loop forever, it won't return and you will never even get to the
mainloop() call.

Tkinter supports timed, asynchronous callbacks. Here is a program that uses a callback to display a
ticker:
from Tkinter import *

count = 0

def update():
''' Callback method updates the label '''
global count
count += 1
label.configure(text=str(count))

# Schedule another callback
root.after(1000, update)


root=Tk()
label=Label(root,text="0")
label.pack()

b=Button(root,text="Bye",command='exit')
b.pack()

# Schedule the initial callback
root.after(1000, update)

root.mainloop()


Kent
> """
>
> from Tkinter import *
> from time import sleep
>
> class Clock:
> def __init__(self, parent):
> self.time = 0
> self.display = Label(parent)
> self.display["font"] = "Arial 16"
> self.display["text"] = str(self.time)
> self.display.pack()
>
> def run(self): #also tried self,parent
> sleep(1.0)
> self.time = self.time + 1
> self.display["text"] = str(self.time)
>
> win = Tk()
> app = Clock(win)
> app.run() #also tried run(win) with self,parent above
> win.mainloop()
>

 
Reply With Quote
 
 
 
 
Martin Franklin
Guest
Posts: n/a
 
      04-21-2005
(E-Mail Removed) wrote:
> """
> What I want: A little window to open with a 0 in it. Every second, the
> 0 should increment by 1.
> What I get: A one second delay, see the window with a 1 in it, and then
> nothing appears to happen. Never see the 0, never see a 2. Any quick
> clues? Thanks. Nick. (Python 2.4, Win9.
> """
>
> from Tkinter import *
> from time import sleep
>
> class Clock:
> def __init__(self, parent):
> self.time = 0
> self.display = Label(parent)
> self.display["font"] = "Arial 16"
> self.display["text"] = str(self.time)
> self.display.pack()
>
> def run(self): #also tried self,parent
> sleep(1.0)
> self.time = self.time + 1
> self.display["text"] = str(self.time)
>
> win = Tk()
> app = Clock(win)
> app.run() #also tried run(win) with self,parent above
> win.mainloop()
>


Nick,

Look at the after method of the Tk widget also google for Tkinter clock
examples should get results

Cheers,
Martin.

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      04-21-2005
Mediocre Person wrote:

> What I want: A little window to open with a 0 in it. Every second, the
> 0 should increment by 1.
> What I get: A one second delay, see the window with a 1 in it, and then
> nothing appears to happen. Never see the 0, never see a 2. Any quick
> clues? Thanks. Nick. (Python 2.4, Win9.


simple solution:

def run(self): #also tried self,parent
sleep(1.0)
self.time = self.time + 1
self.display["text"] = str(self.time)
self.update() # make sure to process all outstanding events

smarter solution:

def run(self):
self.display["text"] = str(self.time)
self.time = self.time + 1
self.after(1000, self.run)
# no need to use update in this case

even smarter solution:

import time

TICK = 1.0 # time between updates, in seconds

def run(self):
self.display["text"] = str(self.time)
self.time = self.time + 1
delay = TICK - (time.time() % TICK)
self.after(int(delay*1000), self.run)
# no need to use update in this case

also see:

http://tkinter.unpythonic.net/wiki/TkinterDiscuss

</F>

 
Reply With Quote
 
mediocre_person@hotmail.com
Guest
Posts: n/a
 
      04-21-2005
***Many*** thanks. I was getting right confused, but this is pretty
clear. I had also tried, in run(...):
while True:
sleep(1.0)
...

But it's clear from your response why this also didn't work!

Nick.

 
Reply With Quote
 
Miki Tebeka
Guest
Posts: n/a
 
      04-25-2005
Hello Nick,

> """
> What I want: A little window to open with a 0 in it. Every second, the
> 0 should increment by 1.
> What I get: A one second delay, see the window with a 1 in it, and then
> nothing appears to happen. Never see the 0, never see a 2. Any quick
> clues? Thanks. Nick. (Python 2.4, Win9.
> """

Read: http://www.pythonware.com/library/tkinter/introduction/ and mainly
http://www.pythonware.com/library/tk...-and-other.htm


from Tkinter import *

class Clock(Frame): #<<<
def __init__(self, parent):
Frame.__init__(self, parent) #<<<
self.time = 0
self.display = Label(parent)
self.display["font"] = "Arial 16"
self.display["text"] = str(self.time)
self.display.pack()

def run(self): #<<<
self.loop()

def loop(self): #<<<
self.time = self.time + 1
self.display["text"] = str(self.time)
self.after(1000, self.loop)

win = Tk()
app = Clock(win)
app.run() #also tried run(win) with self,parent above
win.mainloop()

HTH.
--
------------------------------------------------------------------------
Miki Tebeka <(E-Mail Removed)>
http://tebeka.bizhat.com
The only difference between children and adults is the price of the toys

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (Cygwin)

iD8DBQFCbNZg8jAdENsUuJsRAjVXAKCL/emJSMhwcNWowcB/VjO8vPOMCACeKaEM
I2n7/atoCIIxAPzIPqox+dY=
=nurg
-----END PGP SIGNATURE-----

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
smallish camera backpacks Philip Bailey Digital Photography 8 06-25-2007 06:56 PM
crosspage posting (smallish) problem =?Utf-8?B?Q2hhcmxlc0E=?= ASP .Net 3 02-16-2007 06:55 PM
Smallish Code Lyndon Samson Ruby 1 04-01-2005 09:45 AM
Re: [Tkinter-discuss] Please help -- Tkinter Scale widget withDoubleVar is acting weird Jeff Epler Python 0 08-23-2004 02:31 PM
Re: [Tkinter-discuss] Please help -- Tkinter Scale widget withDoubleVar is acting weird Jeff Epler Python 0 08-20-2004 12:07 PM



Advertisments