Thread: tkinter help

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    3
    Rep Power
    0

    Question tkinter help


    I have been coding in python for a while, and im trying to do some work with tkinter, basicly what i need is for tkinter to generate a window containgin text, this i can do with a simple hello world script, however the text changes every five seconds or so , and i need for tkinter to continusly update the text included.


    The following is aproximitly what i have done so far.


    ------------------------------------


    from Tkinter import *
    abc123 = "hello" #abc123 changes every five seconds or so
    root = Tk()
    w = Label(root, text="abc123")
    w.pack()
    root.mainloop()


    help/sugestions welcome, thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hi!

    Here is a simple snippet to show you how it can be done:
    Code:
    from Tkinter import *
    import time
    
    def change_it():
        for i in range(10):
            w.configure(text=i)
            root.update()
            time.sleep(1)
            
    root = Tk()
    w = Label(root, text="0")
    w.pack()
    Button(text="Go",command=change_it).pack()
    root.mainloop()
    Regards, mawe
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    3
    Rep Power
    0
    That works better than what i had concocted but, for some unknown ( to me at least ) reson the script refuses to contenue changeing teh variable with tkinter open. Only after closing the tkinter popup window does the variable change and open tkinter again with the new text.


    Even with the snippet you provided tkinter will only show the first variable change, and then essentialy freezes the script.

    is it possible i wonder, if there is a way to have the text displayed in the tkinter window to activly moniter the variable and when it changes, change the corisponding text within the tkinter window.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    3
    Rep Power
    0
    maybe a sort of refresh?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    Albuquerque, New Mexico
    Posts
    137
    Rep Power
    11
    i think that update() would do it...thats what the docs say aleast..
    update

    update(). Process all pending events, call event callbacks, complete any pending geometry management, redraw widgets as necessary, and call all pending idle tasks. This method should be used with care, since it may lead to really nasty race conditions if called from the wrong place (from within an event callback, for example, or from a function that can in any way be called from an event callback, etc.)

  10. #6
  11. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    I took the liberty of modifying Nick's code:
    Code:
    from Tkinter import *
    import time
    
    root = Tk()
    
    def change_it(): 
        for i in range(10): 
            myText.set(i)
            try: 
                root.update()
            except: 
                return
            time.sleep(1)
        root.after(1000, change_it) # Register the function again
    
    myText = StringVar(root) # A Tkinter String Variable
    
    Label(root, textvariable = myText).pack()
    Entry(root, textvariable = myText).pack()
    
    root.after(1000, change_it()) # Register a function to be called in the future
    root.mainloop()
    This uses a timer (after) to periodically call a function that updates using a Tkinter variable. I have used a variable as it shows how to link multiple widgets to one value and how you then don't need to track all the widgets yourself.

    The root.update() is in a try/except block to handle the situation where the user terminates the root window but the timed function is still gets called in the background.

    grimey
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    Hey i dont know if i should open another thread or just post my question here.
    But oh well here u go.

    I am also doing Tkinter at the moment and i am just reading for the most part.
    But i dont understand how to make a function and then the variables assigning.

    For example: ###This is a script i got from a website.

    from Tkinter import *

    class Application(Frame):
    def say_hi(self):
    print "hi there, everyone!"

    def say_bye(self):
    print "Bye everyone."

    def createWidgets(self):
    self.QUIT = Button(self)
    self.QUIT["text"] = "QUIT"
    self.QUIT["fg"] = "red"
    self.QUIT["bg"] = "blue"
    self.QUIT["command"] = self.quit

    self.QUIT.pack({"side": "left"})

    self.hi_there = Button(self)
    self.hi_there["text"] = "Hello",
    self.hi_there["command"] = self.say_hi

    self.hi_there.pack({"side": "left"})

    def __init__(self, master=None):
    Frame.__init__(self, master)
    self.pack()
    self.createWidgets()

    app = Application()
    app.mainloop()

    ### These functions do not have anything to assign but how would i be able that the user assigns them and then I apply them to my function.

    For example:

    class Plus:
    def plusx(x,y):
    print x+y # How would i assign these variables
    def __init__(self):
    ...
  14. #8
  15. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    Hi Monkeyman,

    I've updated my code to show how to read the values - it's really easy with Tkinter Variables
    Code:
    from Tkinter import *
    import time
    
    root = Tk()
    
    def change_it(): 
        for i in range(10): 
            myText.set(i)
            try: 
                root.update()
            except: 
                return
            time.sleep(1)
        root.after(1000, change_it) # Register the function again
    
    def get_it(): 
        value = myText.get()
        print "The value is :", value
        
    myText = StringVar(root) # A Tkinter String Variable
    
    Label(root, textvariable = myText).pack()
    Entry(root, textvariable = myText).pack()
    Button(root, text = "Get", command = get_it).pack()
    
    root.after(1000, change_it()) # Register a function to be called in the future
    root.mainloop()
    grimey
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    Thanks man
    hi Grim
    I have no clue why i deleted u

IMN logo majestic logo threadwatch logo seochat tools logo