tkinter与turtle交互
时间: 2023-09-14 11:07:46 浏览: 88
绘制函数_pythontkinter_Turtle_
可以使用tkinter创建一个GUI界面,在界面中嵌入turtle画布,然后在界面中添加按钮等控件,通过控件与turtle交互。
以下是一个简单的例子:
```python
import tkinter as tk
import turtle
# 创建窗口
root = tk.Tk()
root.title("Turtle Demo")
# 创建画布
canvas = turtle.ScrolledCanvas(root, width=400, height=400)
canvas.pack()
# 创建turtle
t = turtle.RawTurtle(canvas)
# 添加按钮控件
def forward():
t.forward(50)
def left():
t.left(90)
def right():
t.right(90)
btn_forward = tk.Button(root, text="Forward", command=forward)
btn_forward.pack(side=tk.LEFT)
btn_left = tk.Button(root, text="Left", command=left)
btn_left.pack(side=tk.LEFT)
btn_right = tk.Button(root, text="Right", command=right)
btn_right.pack(side=tk.LEFT)
# 运行窗口
root.mainloop()
```
运行程序后,可以通过点击按钮控制turtle的移动。
阅读全文