Tkinter t将按钮图像从一个按钮更改为另一个按钮
我试图点击两个按钮中的一个按钮上的图像,并将该图像显示在一个更大的按钮上。使用变量“cnt”,该变量设置为1或2,具体取决于您选择的按钮,该按钮标记if语句以将按钮更改为该图像。我遇到的问题是,“cnt”变量永远不会在函数之外更改,即使它是一个全局变量。我是Python新手,试图找到答案,但未能找到。谢谢你的帮助Tkinter t将按钮图像从一个按钮更改为另一个按钮,tkinter,Tkinter,我试图点击两个按钮中的一个按钮上的图像,并将该图像显示在一个更大的按钮上。使用变量“cnt”,该变量设置为1或2,具体取决于您选择的按钮,该按钮标记if语句以将按钮更改为该图像。我遇到的问题是,“cnt”变量永远不会在函数之外更改,即使它是一个全局变量。我是Python新手,试图找到答案,但未能找到。谢谢你的帮助 from Tkinter import * root = Tk() col = 0 row = 0 global photo global photo2 global photo1
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()
我修改了您的代码以使其按您的要求工作我认为:
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()
其工作原理如下所示:
几句解释的话。对不起,没有把每一行都看一遍。简而言之,我使用PIL的ImageTk(或目前称为枕头)来重新调整大小和读取图像。它比特金特自己的班级好。您还需要认识到tkinter是事件驱动的。因此,您的if条件,if cnt==0:
不会像您预期的那样工作。您需要在回调中更改大按钮上的图像单击。这就是正在发生的事情
我不想太多地改变你的想法,但是有更好的方法来做你想做的事情。如果不使用globals,最好将其作为Frame等的子类来执行。也不要在外部声明它global
,只需在需要更改其值时执行,即在内部click()
。除了声明之外,cnt=0就足够了。看看这个。非常感谢您的快速回答。它让我对正在发生的事情有了更清晰的了解。你添加的动画也很有帮助!