如何清除 Tkinter 画布?
Tkinter 是一个Python包,用于创建有效的 GUI 应用程序。 Tkinter 的 Canvas 小部件只不过是一个用于绘制图片、简单形状或任何复杂图形的矩形区域。我们可以在画布上放置任何小部件,如文本、按钮或框架。
这里的任务是生成一个可以清除 Tkinter Canvas 的Python脚本。为此将使用该模块的删除函数。这个方法有一个特殊的参数all代表画布上的所有组件。要清除此画布,请将此特殊参数提供给 delete 方法。因此,下面的行足以清除画布:
delete('all')
如果您想删除任何特定项目,那么您可以为该项目分配一个标签,而不是将该标签全部传递给 delete 方法。
下面给出了实现此特定功能的代码:
程序:
清除画布之前
Python3
# import tkinter
from tkinter import *
# make an object of Tk interface
window = Tk()
# Give the title to out window
window.title('GFG')
# creating canvas
canvas = Canvas(window, width=300, height=200)
canvas.pack()
# draw line to out canvas
canvas.create_line(0, 0, 300, 200)
canvas.create_line(0, 200, 300, 0)
# draw oval to out canvas
canvas.create_oval(50, 25, 250, 175, fill="yellow")
window.mainloop()
Python3
# import tkinter
from tkinter import *
# make an object of Tk interface
window = Tk()
# Give the title to out window
window.title('GFG')
# creating canvas
canvas = Canvas(window, width=300, height=200)
canvas.pack()
# draw line to out canvas
canvas.create_line(0, 0, 300, 200)
canvas.create_line(0, 200, 300, 0)
# draw oval to out canvas
canvas.create_oval(50, 25, 250, 175, fill="yellow")
# clear the canvas
canvas.delete('all')
window.mainloop()
输出:
清除画布后
蟒蛇3
# import tkinter
from tkinter import *
# make an object of Tk interface
window = Tk()
# Give the title to out window
window.title('GFG')
# creating canvas
canvas = Canvas(window, width=300, height=200)
canvas.pack()
# draw line to out canvas
canvas.create_line(0, 0, 300, 200)
canvas.create_line(0, 200, 300, 0)
# draw oval to out canvas
canvas.create_oval(50, 25, 250, 175, fill="yellow")
# clear the canvas
canvas.delete('all')
window.mainloop()
输出: