1

I have a script that has a Tkinter module in it that i would like to change the background color in 3min intervals e.g green for 3mins then orange then red. I have the code to display the green but can't get it to change.

When I make a function in my code it gets a few different errors including 'root not defined, global name "root" no defined' although it is.

Also on a side note kill the Tk display after 15 mins so once all 3 colours have been though.

from __future__ import absolute_import
from . import BasePlugin
import os, sys
import time
from Tkinter import *

def Orange (*args,**kwargs):
    root.config(background="Orange")
def Red(*args,**kwargs):
    root.config(background="Red")

class dis(BasePlugin):
       def execute(self, msg, unit, address, when, printer, print_copies):
        mseg = str('%s - %s' % (msg, unit))
        root = Tk()
        root.title('label')
        txt = Label(root, font= 'times 20 bold', bg='Green')
        txt.config(text= mseg)
        txt.pack(fill=BOTH, expand=0)
        root.after(10,Orange)
        root.after(10,Red)

        root.mainloop(0)

PLUGIN = dis

I have also tried

from __future__ import absolute_import
from . import BasePlugin
import os, sys
import time
from Tkinter import *

def Orange (*args,**kwargs):
    txt.config(background="Orange")
def Red(*args,**kwargs):
    txt.config(background="Red")

class dis(BasePlugin):
       def execute(self, msg, unit, address, when, printer, print_copies):
        mseg = str('%s - %s' % (msg, unit))
        root = Tk()
        root.title('label')
        txt = Label(root, font= 'times 20 bold', bg='Green')
        txt.config(text= mseg)
        txt.pack(fill=BOTH, expand=0)
        txt.after(10,Orange)
        txt.after(10,Red)

        root.mainloop(0)

PLUGIN = dis

If I place root = Tk() anywhere else I get a small gray TK box that I don't want.

P.S I know that it's set to 10 seconds that's only so I can test it

1
  • 1
    it's currently set to 10 milliseconds, not 10 seconds. The argument to after is in milliseconds. Commented May 13, 2015 at 10:37

2 Answers 2

3

There are (at least) four problems with your code, but that's difficult to tell, since you are not showing us all the details. In particular, you never seem to call execute, but I'll assume that this happens somewhere else, maybe via the super class...

  • root is defined inside execute, thus to access it in your callback functions, you either have to make it global, or a member of the dis instance, or put the callback functions inside execute
  • the delay in after is in milliseconds, so using 10 the colours will switch instantaneously, which is probably not the best setup for testing
  • as it stands, both after callbacks are executed at the exact same time; either put one at the end of the other callback function, or use different times
  • you change the background of the root panel, while in fact you want to change the background of the txt Label

For example, you could try like this (minimal stand-alone example)

class dis:
    def execute(self):
        def orange():
            txt.config(bg="Orange")
            root.after(2000, red)
        def red():
            txt.config(bg="Red")
            root.after(2000, kill)
        def kill():
            root.destroy()
        root = Tk()
        txt = Label(root, text="some text", font='times 20 bold', bg='Green')
        txt.pack(fill=BOTH, expand=0)
        root.after(2000, orange)
        root.mainloop()
dis().execute()

Or shorter, just using a bunch of lambda:

class dis:
    def execute(self):
        root = Tk()
        txt = Label(root, text="some text", font='times 20 bold', bg='Green')
        txt.pack(fill=BOTH, expand=0)
        root.after(2000, lambda: txt.config(bg="Orange"))
        root.after(4000, lambda: txt.config(bg="Red"))
        root.after(6000, root.destroy)
        root.mainloop()
dis().execute()
Sign up to request clarification or add additional context in comments.

1 Comment

Used top example but removed the dis().execute() and added (self, msg, unit, address, when, printer, print_copies): to def execute up the top. THANKS for the HELP
1

Or a little more generic using a list

class dis():
    def __init__(self):
        mseg = ("test message")
        self.color_list=["green", "orange", "red"]
        self.ctr=0
        root = Tk()
        root.title('label')
        self.txt = Label(root, font= 'times 20 bold', width=20)
        self.txt.config(text= mseg)
        self.txt.pack(fill=BOTH, expand=0)
        self.change_color()

        root.mainloop()

    def change_color(self):
        self.txt.config(background=self.color_list[self.ctr])
        self.ctr += 1
        if self.ctr > 2:
           self.ctr=0
        self.txt.after(500, self.change_color)

PLUGIN = dis()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.