Raspberry Pi Python Tkinter更新标签imag

2024-04-26 02:25:00 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,我正在尝试更新以下代码中的图像,我正在尝试调用自身变更行()为了这样做。一个问题是,我不知道如何编辑此代码,以便在调用后进行更新?目前我试图更改一个全局变量,然后销毁标签并使用新参数重新创建它,但是我看到了使用LabelR.config文件(image=y)以更新标签。我试过了,但它不允许我从任何其他函数引用LabelR。任何帮助都将不胜感激!!我也对主要的代码更改持开放态度。在

def __init__(self, *args, **kwargs):
    tk.Tk.__init__(self, *args, **kwargs)
    self.bind("<KeyRelease-a>", self.on_a_release)
    self.bind("<KeyRelease-d>", self.on_d_release)
    self.bind("<KeyRelease-w>", self.on_w_release)
    self.bind("<KeyRelease-s>", self.on_s_release)
    self.bind("<Key>", self.key)
    self.title("RC Command Center")
    self.geometry("500x500")
    self.initUI()
    self.arrowLeft()
    self.arrowRight()

def arrowLeft(self):
    leftArrow = ImageTk.PhotoImage(Image.open("arrowLeft.png"))
    labelL = Label(image=leftArrow)
    labelL.image = leftArrow
    labelL.pack(side="left")

def arrowRight(self):
    x = ImageTk.PhotoImage(Image.open(y))
    #photos = [ImageTk.PhotoImage(Image.open("arrowRight.png")), ImageTk.PhotoImage(Image.open("arrowRightR.png"))]
    labelR = Label(image=x)
    labelR.image = x
    labelR.pack(side="right")


def changeRarrow(self):
     global y
     y = "arrowRightR.png"
     labelR.destroy()
     self.arrowRight()

Tags: 代码imageselfreleasepngbindondef
1条回答
网友
1楼 · 发布于 2024-04-26 02:25:00

你的问题似乎是垃圾收集器。 两种已知的解决方法适合您的情况。 为你的应用程序使用一个类,并将图像声明为一个类变量(self),或者使其全局化。在

“X”被引用,而不是深度复制,作为局部变量的结果,它被垃圾回收。 垃圾收集引用意味着对所使用的位置根本没有引用。在

相关问题 更多 >