【问题标题】:How to Combine Tkinter windows?如何组合 Tkinter 窗口?
【发布时间】:2013-01-08 23:41:20
【问题描述】:

我有两组代码,第一部分是海龟图形窗口,第二部分是 Tkinter 窗口。我应该如何将这两个部分放在一个窗口中?

我的第一部分代码

from turtle import *

def move(thing, distance):
    thing.circle(250, distance)

def main():
    rocket = Turtle()
    ISS = Turtle()
    bgpic('space.gif')
    register_shape("ISSicon.gif")
    ISS.shape("ISSicon.gif")

    rocket.speed(10)
    ISS.speed(10)
    counter = 1
    title("ISS")
    screensize(750, 750)
    ISS.hideturtle()
    rocket.hideturtle()
    ISS.penup()
    ISS.left(90)
    ISS.fd(250)
    ISS.left(90)
    ISS.showturtle()
    ISS.pendown()
    rocket.penup()
    rocket.fd(250)
    rocket.left(90)
    rocket.showturtle()
    rocket.pendown()
    rocket.fillcolor("white")

    while counter == 1:
        move(ISS, 3)
        move(rocket, 4)

main()

第二部分

from Tkinter import *

control=Tk()
control.title("Control")

control.geometry("200x550+100+50")
cline0=Label(text="").pack()
cline1=Label(text="Speed (km/s)").pack()

control.mainloop()

非常感谢;)

【问题讨论】:

    标签: windows python-2.7 tkinter turtle-graphics


    【解决方案1】:

    嗯,我不确定混合它们是否是个好主意。这个turtle 模块经常使用来自Tcl 的update 命令,当在混合中添加更多相关代码时,这很可能会导致问题(很高兴turtle 显然可以使用它)。无论如何,混合两者的一种方法是使用RawTurtle 代替Turtle,这样您就可以传递自己的Canvasturtle 将根据需要进行调整。

    这是一个例子(我也用无限重新调度替换了无限循环,基本上):

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

    【讨论】:

    • 谢谢,这也正是我想要的,因为 Tkinter.Label 现在它有文本“hello”,是否可以用 turtle1 的位置、turtle2 的位置替换该标签和计时器(或您计算机上的时间)?(如果无法将这些变量放在一行中,则三个单独的行也可以)再次感谢;)
    • 我见过有人用输入框做的,但遗憾的是我不知道那个密码...
    • @user2006082 这当然是可能的,而且它不需要输入框。只需获取海龟的位置并更新标签的文本以显示它。
    • @user2006082 mylabel['text'] = 'somenewtext'
    猜你喜欢
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 2010-09-11
    • 2018-05-30
    • 2013-10-21
    • 2022-01-22
    相关资源
    最近更新 更多