显示窗口后如何从Tkinter中删除canvas

我想在Tkinter中创build一个行,在改变高度后,我需要删除前一行,并创build一个新的高度, 这是一个重复的过程。

对此,我阅读了Python的Tkinter的教程,我认为After方法可能是有用的。 所以,我写我的想法,但这不是一个好方法,我不能创build它。 另外,我search了Tkinter中的Shown事件,但是我没有在Tkinter中find窗口Shown事件。

这是我build议的代码:

 from tkinter import * from tkinter import messagebox import numpy as np import random window = Tk() window.geometry("600x400+650+200") window.resizable(0, 0) window.title("Ball Balancing Game - QLearning") canvas = Canvas() def uptodate(): canvas.delete(line1) line1 = canvas.create_line(100, 200, 500, h) # for i in range(len(h) - 1): # for j in range(len(h) - 1): # # line1 = canvas.create_line(100, h[i], 500, h[j]) # line2 = canvas.create_line(100, h[i+1], 500, h[j+1]) # # lines = canvas.create_line(0,0,600,400) # # balls = canvas.create_oval(200, 150, 230, 110, outline="gray", fill="gray", width=6) # canvas.pack(fill=BOTH, expand=1) # canvas.delete(line1) # window.update() # window.after() # canvas.delete(lines) # canvas.update() # lines.destroy() # i = w.create_line(xy, fill="red") line = canvas.create_line(100, 200, 500, 200) canvas.pack(fill=BOTH, expand=1) window.after(1000, uptodate()) window.mainloop() 

不需要删除,你可以更新所有的行:

 import tkinter as tk import random import numpy as np class App(tk.Tk): def __init__(self): tk.Tk.__init__(self) self.frame = tk.Frame(self, background="bisque") self.frame.pack(side="top", fill="both", expand=True) self.canvas = tk.Canvas(self.frame) self.line = self.canvas.create_line(100, 100, 300, 200, fill="blue") self.canvas.pack(side="top", fill="both", expand=True) self.draw_line() def remove_line(self, line): self.canvas.delete(line) def get_h(self): h1 = [-1, -0.86, -0.71, -0.57, -0.43, -0.29, -0.14, 0, 0.14, 0.29,0.43, 0.57, 0.71, 0.86, 1] map_h = np.linspace(100, 300, 15) map_h = np.around(map_h, 3) map_h = map_h.tolist() rnd_h1 = map_h[h1.index(random.choice(h1))] rnd_h2 = map_h[h1.index(random.choice(h1))] return rnd_h1, rnd_h2 def draw_line(self): rnd_h1, rnd_h2 = self.get_h() self.canvas.coords(self.line, 100, rnd_h1, 300, rnd_h2) self.after(1000, self.draw_line) app = App() app.mainloop() 

正如Bryan所提到的,您可以使用itemconfigcoords方法修改画布上的项目(请参阅更改 itemconfig 画布对象的属性 )

然后, 要用after方法创建一个动画循环 ,需要让动画函数多次调用自己。

例如

在这里输入图像说明

 import tkinter as tk import numpy as np window = tk.Tk() canvas = tk.Canvas(window, width=120, height=100) canvas.pack(fill="both", expand=1) line1 = canvas.create_line(30, 50, 100, 50, fill="red", width=2) count = 0 def update(): """ This function updates the line coordinates and calls itself 62 times """ global count count += 1 canvas.coords(line1, 30, 50, 100, 50 + 30*np.sin(count/5)) if count <= 62: window.after(100, update) # Start the animation update() # Launch the app window.mainloop()