显示窗口后如何从 Tkinter 中删除画布 [英] How to delete the canvas from Tkinter after shown the window

查看:44
本文介绍了显示窗口后如何从 Tkinter 中删除画布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Tkinter 中创建一条线,更改高度后,我需要删除前一行并使用新高度创建一条新线,这是一个重复的过程.

I want to create a line in the Tkinter and after changing the height I need to delete the previous line and create a new line with the new height, this is a repeated process.

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

To this, I read the tutorial of the Tkinter of python and I think the After method might be useful. So, I write my idea but it is not a good method and I cannot create it. Also, I searched about a Shown event in the Tkinter, but I did not find the Shown event in the Tkinter for the window.

这是我建议的代码:

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

推荐答案

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

Don't need to delete, you can update all of lines:

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

这篇关于显示窗口后如何从 Tkinter 中删除画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆