删除Tkinter画布文本项的内容

时间:2017-10-30 03:21:08

标签: python tkinter tkinter-canvas

如下图所示,函数1调用另一个函数(draw_text),以便我可以将输出/结果显示在画布中的标签中。这一切都很棒(感谢Stack Overflow !!)

    # Function 1 

    def Relay_1():
       arduinoData.write(b'1')
       draw_text(self,'This is a Test')

    # Function 2

   def Relay_():
       arduinoData.write(b'1')
       draw_text(self,'This is another test number 2')

    #Function 3

    def draw_text(self, text):
         self.canvas.create_text(340,330,anchor = 'center',text = text, font 
         = ('Arial', '10','bold'))

现在我的问题:

如何清除标签的内容"已创建,所以每次调用Function 1或2时,画布上的结果都会刷新/更新。目前,短信只是覆盖了自己。

3 个答案:

答案 0 :(得分:1)

Each time you create an object on a canvas, it returns an identifier. You can pass this identifier to the canvas delete method.

label_id = self.canvas.create_text(...)
...
self.canvas.delete(label_id)

You can also supply one or more tags to an item, and use the tag rather than the id:

self.canvas.create_text(..., tags=('label',))
...
self.canvas.delete('label')

答案 1 :(得分:0)

清除画布上的所有项目,以清晰的方式开始。

from Tkinter import ALL
...
self.canvas.delete(ALL)

答案 2 :(得分:0)

For complete details

from Tkinter import *


a = Tk()

canvas = Canvas(a, width = 500, height = 500)
canvas.pack()

myrect = canvas.create_rectangle(0,0,100,100)
canvas.delete(myrect) #Deletes the rectangle