(Python初学者,如果问题太幼稚,请原谅) 在写着"Hello"的标签下方,创建一个标签或条形或其他任何东西来显示我的两只海龟的更新位置(我所说的更新是指当移动时,其位置的两个坐标同时变化) 进口特金特 进口海龟
def run_turtles(*args):
for t, d in args:
t.circle(250, d)
root.after_idle(run_turtles, *args)
root = Tkinter.Tk()
root.withdraw()
frame = Tkinter.Frame(bg='black')
Tkinter.Label(frame, text=u'Hello', bg='grey', fg='white').pack(fill='x')
canvas = Tkinter.Canvas(frame, width=750, height=750)
canvas.pack()
frame.pack(fill='both', expand=True)
turtle1 = turtle.RawTurtle(canvas)
turtle2 = turtle.RawTurtle(canvas)
turtle1.ht(); turtle1.pu()
turtle1.left(90); turtle1.fd(250); turtle1.lt(90)
turtle1.st(); turtle1.pd()
turtle2.ht(); turtle2.pu()
turtle2.fd(250); turtle2.lt(90)
turtle2.st(); turtle2.pd()
root.deiconify()
run_turtles((turtle1, 3), (turtle2, 4))
root.mainloop()
非常感谢!!
保存对标签的引用: turtleLabel = Tkinter.Label(frame, text=u'Hello', bg='grey', fg='white')
然后在run_turtles
函数中,您可以通过设置其文本来更新标签:
turtleLabel['text'] = "Here be coordinates"
您还可以使用 configure
方法更改文本:
turtleLabel.configure(text="Here be coordinates")
请注意,您不能在创建小部件的同一语句中调用pack
; pack
返回None
这将否定尝试保存对小部件的引用。