Color changing window demo of tkinter after timer events

TrustyTony 0 Tallied Votes 5K Views Share

This is one small snippet of after event usage. You could express if you find this kind of simple code to be usefull as snippet. Notice also vegaseat's sticky message threads in begining of Python forum in addition to code snippets.

# originally posted in thread http://www.daniweb.com/software-development/python/threads/298190/1282328#post1282328
try:
    from Tkinter import *
except:
    from tkinter import *

def change(a=0):
    color_label.config(bg = "blue" if a & 1 else "purple")
    color_label.after(400,change, a ^ 1 )

if __name__ == '__main__':
    win = Tk() 
    win.geometry("500x300")
    win.title('Demonstrating after event')
    color_label = Label(win)
    color_label.pack(expand=YES, fill=BOTH)
    change()
    
    win.mainloop()
JoshuaBurleson 23 Posting Whiz

A question on the topic of background colors, is there a way to make an entire Frame's background white, including unassigned space? And, without getting those nasty grey areas around labels and windows?

TrustyTony 888 pyMod Team Colleague Featured Poster

re: pyguy62: Only by putting bg='white' everywhere, tk, I do not know ttk themes:

try:
    from Tkinter import *
except:
    from tkinter import *

if __name__ == '__main__':
    win = Tk() 
    win.geometry("500x300")
    win.title('Demonstrating bg')
    win.config(bg='white')
    color_frame = Frame(bg='white')
    Label(win, bg='white', text='test').pack()
    color_frame.pack(expand=YES, fill=BOTH)
    
    win.mainloop()
jklotzner 0 Newbie Poster

To be able to run PyTony's script with nowadays python versions, color_label needs to be defined as global f.e.:

#!/usr/bin/python
# -*- coding: utf-8 -*-
# originally posted in thread http://www.daniweb.com/software-development/python/threads/298190/1282328#post1282328
# color_label needs to be defined global with todays python versions
"""
Created on Mon Jul 17 11:10:18 2017

@author: josef
"""

from __future__ import absolute_import, division, unicode_literals, print_function
from sys import version_info
if version_info.major >= 3:
    from tkinter import *   # python 3.2
elif version_info.major == 2:
    from Tkinter import *   # python 2.7
else:
    print ("what the hell you are running, if not Python 2 or 3 ?  :D")

def change(a=0):
    color_label.config(bg = "blue" if a & 1 else "purple")
    color_label.after(400,change, a ^ 1 )
    print ("i am in change",a)

def main():
    global color_label
    win = Tk() 
    win.geometry("500x300")
    win.title('Demonstrating after event')
    color_label = Label(win)
    color_label.pack(expand=YES, fill=BOTH)
    change()

    win.mainloop()

if __name__ == '__main__':
    main()
Gribouillis 1,391 Programming Explorer Team Colleague

@jklotzner Thank you for the input, but this has nothing to do with today's python vs yesterday's python. The difference is that your code is written in the local namespace of a main() function while pyTony's code is written in the global namespace. A python program does not need a main() function, although it is often used.

Apart from that do you know that before python 2, there was a python 1? It is still available at python.org. Python 1.5.2 was a very very successful realease :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.