A smallish Tkinter question

Discussion in 'Python' started by mediocre_person@hotmail.com, Apr 21, 2005.

  1. Guest

    """
    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()
     
    , Apr 21, 2005
    #1
    1. Advertising

  2. Kent Johnson Guest

    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()
    >
     
    Kent Johnson, Apr 21, 2005
    #2
    1. Advertising

  3. 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.
     
    Martin Franklin, Apr 21, 2005
    #3
  4. 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

    </F>
     
    Fredrik Lundh, Apr 21, 2005
    #4
  5. Guest

    ***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.
     
    , Apr 21, 2005
    #5
  6. Miki Tebeka Guest

    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 <>
    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-----
     
    Miki Tebeka, Apr 25, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Epler
    Replies:
    0
    Views:
    518
    Jeff Epler
    Aug 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    468
    Jeff Epler
    Aug 23, 2004
  3. =?Utf-8?B?Q2hhcmxlc0E=?=

    crosspage posting (smallish) problem

    =?Utf-8?B?Q2hhcmxlc0E=?=, Feb 15, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    425
    Teemu Keiski
    Feb 16, 2007
  4. Pierre Dagenais
    Replies:
    0
    Views:
    329
    Pierre Dagenais
    Aug 3, 2008
  5. Lyndon Samson

    Smallish Code

    Lyndon Samson, Apr 1, 2005, in forum: Ruby
    Replies:
    1
    Views:
    95
Loading...

Share This Page