0

(Python初学者,如果问题太幼稚,请原谅)在写着“Hello”的标签下方,创建一个标签或条形或其他任何东西来显示我的两只海龟的更新位置(我的意思是随着海龟移动其位置的两个坐标同时变化) import Tkinter import turtle

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()

非常非常感谢你!!

4

1 回答 1

0

保存对标签的引用:turtleLabel = Tkinter.Label(frame, text=u'Hello', bg='grey', fg='white')

然后在您的run_turtles-function 中,您可以通过设置其文本来更新标签:

turtleLabel['text'] = "Here be coordinates"

您还可以使用以下configure方法更改文本:

turtleLabel.configure(text="Here be coordinates")

请注意,您不能pack在创建小部件的同一语句中调用;pack返回None这将否定尝试保存对小部件的引用。

于 2013-01-27T16:23:27.767 回答