A smallish Tkinter question

M

mediocre_person

"""
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, Win98).
"""

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()
 
K

Kent Johnson

"""
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, Win98).

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()
 
M

Martin Franklin

"""
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, Win98).
"""

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.
 
F

Fredrik Lundh

Mediocre said:
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, Win98).

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>
 
M

mediocre_person

***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.
 
M

Miki Tebeka

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, Win98).
"""
Read: http://www.pythonware.com/library/tkinter/introduction/ and mainly
http://www.pythonware.com/library/tkinter/introduction/x9507-alarm-handlers-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 <[email protected]>
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-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top