3

I have two groups of codes and the first part is a turtle graphics window and second part is a Tkinter window. How should I those two parts together to one window?

My first part of the code

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

Second part

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

Thanks a lot ;)

user2006082
  • 135
  • 1
  • 2
  • 7

1 Answers1

6

Uhm, I'm not sure if mixing them is a good idea. This turtle module frequently uses the update command from Tcl, and this will very likely cause problems when more involved code is added in the mix (it is nice that apparently turtle can live with it). Anyway, one way to mix both is by using RawTurtle in place of Turtle, so you can pass your own Canvas which turtle will adjust for its needs.

Here is an example (I also replaced the infinite loop by an infinite re-schedule, basically):

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()
mmgp
  • 18,901
  • 3
  • 53
  • 80
  • thanks that is exactly what I want also, for the Tkinter.Label right now it has text "hello" in it, is it possible to replace that label by the position of turtle1, position of turtle2 and a timer(or the time from your computer)?(if that's not possible to put those variables in one line, three separated lines will be okay as well) thanks again ;) – user2006082 Jan 25 '13 at 23:54
  • I have seen people doing it with the entrybox but sadly I don't know the codes for that... – user2006082 Jan 25 '13 at 23:57
  • @user2006082 it is certainly possible, and it doesn't need an entry box for that. Just get the positions of the turtles and update the label's text to show it. – mmgp Jan 26 '13 at 00:08
  • @user2006082 `mylabel['text'] = 'somenewtext'` – mmgp Jan 26 '13 at 01:01