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

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    In TkInter: Changing background color, then changing it again after a delay


    I'm trying to make a program that

    (1) displays a window with a button in it
    (2) when you press the button, the background of the window changes from one color to another...
    (3) ...and then changes back to its original color after a short delay (without pressing the button again).

    So far, I've managed (1) and (2), but I can't figure out how to implement (3). Here is my code:

    Code:
    from Tkinter import *  
    
    root = Tk()  
    
    def bthing():    
    
    root.configure(background="red")
    b = Button(text="OK", command=bthing) b.pack() root.configure(background="grey") root.geometry("400x400") root.mainloop()
    I can cause a delay before the background color changes by changing "def bthing(): root.configure(background="red")" to "def bthing(): root.after(1000, root.configure(background="red"))"

    But what I want is something like... "root.configure(background="red") THEN root.after(1000, root.configure(background="grey"))"

    Can someone point me in the right direction? I have no experience in programming.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,963
    Rep Power
    481
    Code:
    try:
        import tkinter
    except:
        import Tkinter as tkinter
    
    root = tkinter.Tk()
    
    def grey(*args,**kwargs):
        root.configure(background="grey")
    
    def bthing():
        root.configure(background="red")
        root.after(1000, grey)
    
    tkinter.Button(text="OK", command=bthing).pack()
    
    root.configure(background="grey")
    root.geometry("400x400")
    
    root.mainloop()
    How does this differ from your effort?
    root.after(1000, root.configure(background="grey"))
    function(arguments) causes python to evaluate the function immediately. I passed the name of a function, just as you did when you wrote command=bthing .
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo