Reputation: 11
I am trying to click the image on one of two buttons and have that image appear on a much larger button. Using the variable "cnt" that is set to 1 or 2 depending on the button that you choose which flags the if statements to change the button to that image. Problem I am having is that the "cnt" variable never changes outside of the function even though it is a global variable. I am new to Python and tried to find an answer but could not. Thanks for any help!
from Tkinter import *
root = Tk()
col = 0
row = 0
global photo
global photo2
global photo1
global cnt
x = 0
cnt = 0
# button_flag = True
label = Label(root)
entry = Entry(root)
photo1 = PhotoImage(file="reyes.gif")
photo2 = PhotoImage(file="A_180_60.gif")
def click(x):
"""
respond to the button click
"""
global cnt
if x == 1:
print "One"
cnt = 1
print cnt
elif x == 2:
print "Two"
cnt = 2
print cnt
# Object of the Tkinter StringVar class
# buttonstr = StringVar()
if cnt == 0:
fullsize = Button(root, image=photo1, command=lambda: click(10), height=400, width=400)
elif cnt == 1:
fullsize = Button(root, image=photo2, command=lambda: click(10), height=400, width=400)
elif cnt == 2:
fullsize = Button(root, image=photo1, command=lambda: click(10), height=400, width=400)
button1 = Button(root,image=photo2, command=lambda: click(1))
button2 = Button(root, image=photo2, command=lambda: click(2))
fullsize.grid(column = 8, row = 0, columnspan=40, rowspan = 40, sticky = S)
button1.grid(column = 0, row = 0,sticky = N)
button2.grid(column = 0, row = 1, sticky = N)
root.mainloop()
Upvotes: 1
Views: 631
Reputation: 238081
I modified your code to make it work as you want I think:
from Tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.geometry("400x400")
col = 0
row = 0
global photo
global photo2
global photo1
global cnt
global fullsize
global fullsize_photo
x = 0
cnt = 0
# button_flag = True
label = Label(root)
entry = Entry(root)
img1 = Image.open('empty1.gif')
img2 = Image.open('empty2.gif')
photo1 = ImageTk.PhotoImage(img1)
photo2 = ImageTk.PhotoImage(img2)
def click(x):
"""
respond to the button click
"""
global cnt
global fullsize_photo
w,h = fullsize.winfo_width(), fullsize.winfo_height()
if x == 1:
print "One"
cnt = 1
print cnt
fullsize_photo = ImageTk.PhotoImage(img1.resize((w,h)))
elif x == 2:
print "Two"
cnt = 2
print cnt
fullsize_photo = ImageTk.PhotoImage(img2.resize((w,h)))
fullsize['image'] = fullsize_photo
button1 = Button(root, image=photo1, command=lambda: click(1))
button2 = Button(root, image=photo2, command=lambda: click(2))
button1.grid(column = 0, row = 0,sticky = N)
button2.grid(column = 0, row = 1, sticky = N)
fullsize = Button(root)
fullsize.grid(column = 1, row = 0, rowspan=2, columnspan=1, sticky=NSEW)
# strech the grid cell containing fullsize
Grid.columnconfigure(root,1,weight=1)
Grid.rowconfigure(root,1,weight=1)
root.mainloop()
How it works is shown below:
Few words of explanation. Sorry for not going over each of the lines. In short, I use ImageTk from PIL (or currently know as pillow) for re-sizing and reading images. Its better than tkinter's own class. Also you need to realize that tkinter is event driven. So your if conditions, if cnt == 0:
wont work as you would expect. You need to change the image on the big button in a callback click
. And this is what is happening.
I did not want to change your coode too much, but there are batter ways of doing what you are trying to do. Instead of globals, probably would be better to do this as a subclass of Frame, etc.
Upvotes: 1