如何在显示窗口后从Tkinter中删除画布

时间:2017-07-05 14:27:49

标签: windows python-3.x tkinter tkinter-canvas

我想在Tkinter中创建一条线,在更改高度后,我需要删除上一行并创建一个具有新高度的新线,这是一个重复的过程。

为此,我阅读了python的Tkinter教程,我认为After方法可能很有用。所以,我写了我的想法,但它不是一个好方法,我不能创建它。另外,我搜索了Tkinter中的Shown事件,但是我没有在Tkinter中找到该窗口的Shown事件。

以下是我建议的代码:

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()

2 个答案:

答案 0 :(得分:1)

正如Bryan所提到的,您可以使用itemconfigcoords方法修改画布上的项目(请参阅Change the attributes of a tkinter canvas object

然后,要创建animation loop with the after method,您需要让动画函数多次调用自身。

示例

enter image description here

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()

答案 1 :(得分:1)

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

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()