tkinter将按钮图像从一个按钮更改为另一个按钮

时间:2015-01-23 04:47:35

标签: tkinter

我正在尝试单击两个按钮之一上的图像,并将该图像显示在一个更大的按钮上。使用设置为1或2的变量“cnt”,具体取决于您选择的按钮,标记if语句以将按钮更改为该图像。我遇到的问题是“cnt”变量永远不会在函数外部发生变化,即使它是一个全局变量。我是Python的新手,并试图找到答案,但不能。谢谢你的帮助!

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()

1 个答案:

答案 0 :(得分:1)

我修改了你的代码,让它按照我的想法运作:

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()

工作原理如下所示:

enter image description here

几句解释。很抱歉没有遍历每一行。简而言之,我使用PIL的ImageTk(或目前称为枕头)来重新调整大小和读取图像。它比tkinter自己的课程更好。您还需要意识到tkinter是事件驱动的。所以你的if条件if cnt == 0:不会像你期望的那样工作。您需要在回调click中更改大按钮上的图像。这就是正在发生的事情。

我不想太多地改变你的coode,但是有一些方法可以做你想做的事情。而不是全局变量,作为Frame的子类等可能会更好。