Reputation: 382
I am new to Python and programming in general also new to stack overflow. Any help would be appreciated, I am just messing around with code for fun to get a feel for it.
My goal is to change the box from red to blue when the button is pushed. I set the var rec1c to 'red' then set it to 'blue' when the button is pushed. I ran the code and the box never turned blue. I put the print statements in to see if it changed, the box never turned blue. I am confused as to why because I kept pressing the button and the first print statement never turned back to red. the second time pressing both statements are blue i thought it would change back after thinking about689 it that's why i put those there
my questions are
Is it possible to accomplish my goal? If so how? Is there a better way to doing it than my way? also i tried putting all my code into the question but it cut a lot of it out so I deleted all but the necessary code. Assume the syntax is correct.
class PythonTest(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Python!")
self.style = Style()
self.style.theme_use("default")
self.pack(fill=BOTH, expand=1)
self.canw = 300
self.canh = 400
paper = Canvas(self, width=self.canw, height=self.canh)
paper.pack()
self.rec1c = 'red'
self.rec1o = 'red'
self.rec1 = paper.create_rectangle(0, 0, 300, 300, fill=self.rec1c, outline=self.rec1o)
self.translate = Button(self, text="Change Color!", command=self.toB)
self.translate.place(x=50, y=50)
def toB(self):
print(self.rec1c)
self.rec1c = 'blue'
print(self.rec1c)
Tk.update
Upvotes: 0
Views: 125
Reputation: 26688
To access paper
from toB()
use self.paper
and to change color add self.paper.itemconfig(self.rec1, fill=self.rec1c)
in toB
method.
class PythonTest(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Python!")
#self.style = Style()
#self.style.theme_use("default")
self.pack(fill=BOTH, expand=1)
self.canw = 300
self.canh = 400
self.paper = Canvas(self, width=self.canw, height=self.canh)
self.paper.pack()
self.rec1c = 'red'
self.rec1o = 'red'
self.rec1 = self.paper.create_rectangle(0, 0, 300, 300, fill=self.rec1c, outline=self.rec1o)
self.translate = Button(self, text="Translate to Pig Latin", command=self.toB)
self.translate.place(x=50, y=50)
def toB(self):
print(self.rec1c)
self.rec1c = 'blue'
print(self.rec1c)
self.paper.itemconfig(self.rec1, fill=self.rec1c)
Upvotes: 2
Reputation: 76194
Changing the value of self.rec1c
will not propagate any changes to canvas elements whose attributes were defined using self.rec1c
. self.rec1c
has a new value, but the canvas elements still happily point to the old value, and will continue to do so no matter what happens to self.rec1c
.
If you want the rectangle's attributes to change, you need to change them explicitly with itemconfig
.
self.paper = Canvas(self, width=self.canw, height=self.canh)
self.paper.pack()
self.rec1 = paper.create_rectangle(0, 0, 300, 300, fill="red", outline="red")
#later, in toB...
self.paper.itemconfig(self.rec1, fill="blue")
Upvotes: 0