turtle学习笔记之在tkinter画布上作图

前言

       因为毕设的原因,最近在同时学习tkinter和turtle,希望可以在自己需要的GUI上面画出图像。

查看文档

       查了一下python中关于turtle的文档,可以发现下面的描述:

  1. TurtleScreen 类定义图形窗口作为绘图海龟的运动场。它的构造器需要一个 tkinter.CanvasScrolledCanvas 作为参数。应在 turtle 作为某个程序的一部分的时候使用。
  2. Screen() 函数返回一个 TurtleScreen 子类的单例对象。此函数应在 turtle 作为独立绘图工具时使用。
  1. RawTurtle (别名: RawPen) 类定义海龟对象在 TurtleScreen 上绘图。它的构造器需要一个 Canvas, ScrolledCanvas 或 TurtleScreen 作为参数,以指定 RawTurtle 对象在哪里绘图。
  2. Turtle (别名: Pen),该类对象在 Screen 实例上绘图,如果实例不存在则会自动创建。

程序

import turtle
from tkinter import *

def drawp():
    theScreen = turtle.TurtleScreen(canva)
    path = turtle.RawTurtle(theScreen)
    path.forward(100)
    path.right(90)
    path.forward(150)
    theScreen.mainloop()

root = Tk()
root.title("the path")

canva = Canvas(root, width=400, height=400)
canva.pack()

aa = Button(root, text="Draw", command=drawp)
aa.pack()

root.mainloop()

总结

       相对于经常用到的 turtle 独立绘图,关键在于下面两条语句的区别:

#独立绘图
theScreen = turtle.Screen()
path = turtle.Turtle()

#在tkinter画布上绘图
theScreen = turtle.TurtleScreen(canva)
path = turtle.RawTurtle(theScreen)
#path = turtle.RawTurtle(canva) #参数换成canva也可以

           

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值