tkinter - 无法从其他方法访问画布
问题描述
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 传递到该方法中并相应地更改代码,但没有任何帮助。任何人都可以帮助我吗?
该功能应该在满足条件时移动图像。
解决方案
推荐阅读
- vue.js - 为什么当我返回第一页时 beforeRouteUpdate 不起作用?
- java - Eclipse Compiler for Java 中可能存在的错误
- java - 没有可用的“concert.PerformanceImp”类型的合格 bean
- java - 从链接列表中删除重复项
- hibernate - Hibernate 从 manyTomany 关系中删除实体
- javascript - 如何使用纯 JavaScript 获取数组中所有选中的复选框?
- java - 无法运行基于 Java 10 的 Spring Initializer 创建的基于 Maven 的 Spring 项目
- stackexchange-api - 如何获得徽章授予时间的时间戳?
- javascript - 我如何使它成为多个匹配项?
- python-3.x - Pygame 无法在屏幕上方绘制