简体   繁体   English

更新 Tkinter Canvas 中的多个 create_text 对象

[英]Updating multiple create_text objects in Tkinter Canvas

I want to continuously update a time output overlayed on a canvas with a background.我想不断更新时间 output 覆盖在具有背景的 canvas 上。 I can do this when the text is on a label, but the label doesn't allow me to add multiple text objects without creating a background around the text.当文本位于 label 上时,我可以这样做,但是 label 不允许我添加多个文本对象而不在文本周围创建背景。 I am able to add a static time, but as soon as I try to create a function to simply create a text I seem to get an error我可以添加一个 static 时间,但是一旦我尝试创建一个 function 来简单地创建一个文本,我似乎得到了一个错误

So this code works fine:所以这段代码工作正常:

timenow = time.strftime('%H:%M:%S')

root = Tk()
c = Canvas(root, width = 500, height = 333)
c.pack()
image = PhotoImage(file = 'C:/Users/Khalid Byra/Desktop/Python Alarm Clock Project/wallpaper.gif')
c.create_image(0, 0, image=image, anchor=NW)
c.create_text(250, 166.5, text = timenow, font=('Arial', 24), fill = 'white', anchor = CENTER)

However, when I try change it to a method like this:但是,当我尝试将其更改为这样的方法时:

c.create_image(0, 0, image=image, anchor=NW)
def write(x=250, y=166.5, text=timenow, font=('Arial', 24), fill='white', anchor=CENTER):
    c.create_text(x, y, text, fill, anchor, text)
write()

I get the following error:我收到以下错误:

*(args + self._options(cnf, kw))))

_tkinter.TclError: unknown option "22:27:34" _tkinter.TclError:未知选项“22:27:34”

Ok nevermind I figured it out, was supposed to do this:好吧,没关系,我想通了,应该这样做:

def write(x=250, y=166.5, text=timenow, font=('Arial', 32), fill='white', anchor=CENTER): c.create_text(x, y, text=text, fill=fill, anchor=anchor) def write(x=250, y=166.5, text=timenow, font=('Arial', 32), fill='white', anchor=CENTER): c.create_text(x, y, text=text, fill=填充,锚=锚)

My next problem is that I am using this method to update the clock:我的下一个问题是我正在使用这种方法来更新时钟:

def tick():
    global timenow
    time2 = time.strftime('%H:%M:%S')
    if time2 != timenow:
        timenow = time2
        write(text = time2)
    c.after(200, tick)

tick()

But the problem here is that it puts new text over the old one, making it a mess.但这里的问题是它把新文本放在旧文本上,使它变得一团糟。 So now I need to figure out to delete the old text as the new one appears?所以现在我需要弄清楚在新文本出现时删除旧文本? But it's not assigned to variable so I'm not sure how但它没有分配给变量,所以我不确定如何

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM