在画布上不同位置无限生成文本(Tkinter)



我知道我的目的非常简单,但它在我的代码中不起作用。我需要把这个";HELLO";不同位置的文本(每次向下100(。我正在尝试使用"after"方法,但它没有任何作用。结果是,画布将显示为白色背景,只有位置100、100处的文本。位置不会改变(即使在10秒后(。请告诉我哪里出了问题。

这是我的代码:

from tkinter import *
import time
def create():
global y_position
y_position = 100
word = canvas.create_text (100, y_position, text = 'HELLO')
y_position += y_position + 100

root.after(500, create)

root = Tk ()
canvas = Canvas (root, height = 1200, width = 800, bg = 'white')
canvas.pack()
root.after(0, create)
mainloop()

尝试这样做(我不确定这是否是你想要做的,但它会在上一次迭代100之后每500毫秒创建一个新的"HELLO"文本(:

from tkinter import *
import time
y_position = 100
def create():
global y_position

word = canvas.create_text (100, y_position, text = 'HELLO')
y_position += 100

root.after(500, create)

root = Tk ()
canvas = Canvas (root, height = 1200, width = 800, bg = 'white')
canvas.pack()
root.after(0, create)
mainloop()

最新更新