0

请看一下这段代码。

#normal template stuff
from Tkinter import *
from PIL import Image, ImageTk
win=Tk()
win.geometry('+0+0')
win.title(" Borderlands 2 Chicks")
#How to add intro photo
img = Image.open("intro.gif")
intro = ImageTk.PhotoImage(img)
right = Label(win, image=intro)
right.grid(row=0, column=1, rowspan=5)


#defernitions to call photos
def but1():
img = Image.open("captain_scarlett.gif")
intro = ImageTk.PhotoImage(img)
right1 = Label(win, image=intro)
right1.grid(row=0, column=1, rowspan=5,)    

def but2():
img = Image.open("Ellie.gif")
intro = ImageTk.PhotoImage(img)
right = Label(win, image=intro)
right.grid(row=0, column=1, rowspan=5)

def but3():
img = Image.open("Gaige Swin Suit.gif")
intro = ImageTk.PhotoImage(img)
right = Label(win, image=intro)
right.grid(row=0, column=1, rowspan=5)

def but4():
img = Image.open("Gaige.gif")
intro = ImageTk.PhotoImage(img)
right = Label(win, image=intro)
right.grid(row=0, column=1, rowspan=5)


#buttons on the left column
left1 = Button(win, text=" Captian Scarlet")
left1.configure(command=but1)
left2 = Button(win, text=" Button 2 ")
left2.configure(command=but2)
left3 = Button(win, text=" Button 3 ")
left3.configure(command=but3)
left4 = Button(win, text=" Button 4 ")
left4.configure(command=but4)
left1.grid(row=0, column=0)
left2.grid(row=2, column=0)
left3.grid(row=3, column=0)
left4.grid(row=4, column=0)

#how to get buttons to do something
def but1(): print "Button one was pushed"

mainloop()

这个应用程序起初显示良好。我按下第一个按钮,右栏变成白色。我需要的是在我按下任何按钮后替换右栏中当前照片的代码。我一直在学习Destroy()grid_forget()但我找不到适用于这个应用程序的示例代码。请帮忙。

4

1 回答 1

0

问题是您在函数中创建图像并将对这些图像的引用存储在局部变量中。函数完成后,局部变量超出范围,图像对象被垃圾回收。您需要保持对图像的持久引用。

代码中的另一个问题是每次按下按钮时都会创建一个新图像和新标签。随着时间的推移,这将不必要地占用大量内存。您无需不断创建新的按钮和图像。创建图像后,您可以更改图像的标签。

例如,您可以创建一个简单的图像列表,然后在按下按钮时重新配置标签。这是我将如何做的一个例子:

import Tkinter as tk

class View(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self._initialize_images()
        self.right1 = tk.Label(self, image=self.images[0])
        self.right1.grid(row=0, column=1, rowspan=5)

        b1 = tk.Button(self, text="Button 1", command=self.set_image_1)
        b2 = tk.Button(self, text="Button 2", command=self.set_image_2)
        b3 = tk.Button(self, text="Button 3", command=self.set_image_3)
        b4 = tk.Button(self, text="Button 4", command=self.set_image_4)

        b1.grid(row=0, column=0)
        b2.grid(row=2, column=0)
        b3.grid(row=4, column=0)
        b4.grid(row=6, column=0)

    # these can all be consolidated into a single function with the number
    # passed in as a parameter. I chose not to do that since that might
    # add confusion for someone just learning how to write Tkinter code
    def set_image_1(self):
        self.right1.configure(image=self.images[1])
    def set_image_2(self):
        self.right1.configure(image=self.images[2])
    def set_image_3(self):
        self.right1.configure(image=self.images[3])
    def set_image_4(self):
        self.right1.configure(image=self.images[4])

    def _initialize_images(self):
        self.images = []
        self.images.append(tk.PhotoImage(file="intro.gif"))
        self.images.append(tk.PhotoImage(file="captain_scarlett.gif"))
        self.images.append(tk.PhotoImage(file="Ellie.gif"))
        self.images.append(tk.PhotoImage(file="Gaige Swin Suit.gif"))
        self.images.append(tk.PhotoImage(file="Gaige.gif"))


if __name__ == "__main__":
    root = tk.Tk()
    view = View(root)
    view.pack(side="top", fill="both", expand=True)
    root.mainloop()

注意我选择使用面向对象的风格是因为它使创建 GUI 变得相当容易。使用一个类来定义你的主窗口为你提供了一个方便的地方来保持对其他对象的持久引用,而不会弄乱全局命名空间。

于 2013-02-22T14:52:42.460 回答