首页 > 解决方案 > 无法从其他方法访问画布

问题描述

    class GUI(tk.Frame): 

        def __init__(self, master):
           frame = tk.Frame(master)
           frame.pack()

           c1 = tk.Canvas(frame, width=900, height=50, bg="blue")
           c1.create_line(0, 49, 900, 49, width=4, fill="#1f1f1f")
           c1.pack

           self.fishIMG = ImageTk.PhotoImage(Image.open("fish.png").resize((40, 40), Image.ANTIALIAS))
           fishIMG_C1 = c1.create_image(0, 3, image=self.fishIMG, anchor=tk.NW)

           button = tk.Button(master, text = "Test Button", command = GUI.move)



        def move():
            if c1.coords(fishIMG_C1) > [912.0, 10.0]:
               c1.move(fishIMG_C1, -912, 0)
            c1.move(fishIMG_C1, 5, 0)
            print(c1.coords(fishIMG_C1))
            c1.after(15, GUI.move)

我收到“c1 未定义”错误。我试图将 self 传递到该方法中并相应地更改代码,但没有任何帮助。任何人都可以帮助我吗?

该功能应该在满足条件时移动图像。

标签: tkinter

解决方案


推荐阅读