将turtle模块与tkinter canvas集成在一起

时间:2017-06-20 12:46:37

标签: python python-3.x tkinter turtle-graphics

我正在尝试将Turtle模块集成到我用TKInter创建的界面中,目前我有一个画布,我想让乌龟画到(参见示例1)。然而,我迷失了如何得到它的吸引力。

Example

1 个答案:

答案 0 :(得分:9)

import turtle
import tkinter as tk

def forward():
    t.forward(100)

def back():
    t.back(100)

def left():
    t.left(90)

def right():
    t.right(90)

root = tk.Tk()
canvas = tk.Canvas(master = root, width = 500, height = 500)
canvas.pack()

t = turtle.RawTurtle(canvas)
t.pencolor("#ff0000") # Red

t.penup()   # Regarding one of the comments
t.pendown() # Regarding one of the comments

tk.Button(master = root, text = "Forward", command = forward).pack(side = tk.LEFT)
tk.Button(master = root, text = "Back", command = back).pack(side = tk.LEFT)
tk.Button(master = root, text = "Left", command = left).pack(side = tk.LEFT)
tk.Button(master = root, text = "Right", command = right).pack(side = tk.LEFT)

root.mainloop()

我之前从未使用过这个模块但是我写的东西似乎做了你想做的事情

<强>参考

http://www.eg.bucknell.edu/~hyde/Python3/TurtleDirections.html

https://www.reddit.com/r/learnpython/comments/4qdcmw/can_you_add_turtle_graphics_to_a_tkinter_window/