A smallish Tkinter question

2005-04-21 Thread 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()

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A smallish Tkinter question

2005-04-21 Thread Kent Johnson
[EMAIL PROTECTED] 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, 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()
--
http://mail.python.org/mailman/listinfo/python-list


Re: A smallish Tkinter question

2005-04-21 Thread Martin Franklin
[EMAIL PROTECTED] 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, 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.
--
http://mail.python.org/mailman/listinfo/python-list


Re: A smallish Tkinter question

2005-04-21 Thread Fredrik Lundh
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, 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

--
http://mail.python.org/mailman/listinfo/python-list


Re: A smallish Tkinter question

2005-04-21 Thread 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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A smallish Tkinter question

2005-04-25 Thread 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


pgp6U4b8lkFDH.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list