Reputation: 709
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
Upvotes: 1
Views: 1001
Reputation:
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()
Upvotes: 1
Reputation: 82889
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 executeafter
is in milliseconds, so using 10
the colours will switch instantaneously, which is probably not the best setup for testingafter
callbacks are executed at the exact same time; either put one at the end of the other callback function, or use different timesroot
panel, while in fact you want to change the background of the txt
LabelFor 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()
Upvotes: 3