GVKun编程网logo

使用turtle.onclick的Python 3.0(python turtle onclick)

16

针对使用turtle.onclick的Python3.0和pythonturtleonclick这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展PythonturtleprintTaiChi

针对使用turtle.onclick的Python 3.0python turtle onclick这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展Python turtle print TaiChi、Python turtle 画蛇、Python Turtle 绘图、python turtle 雪花等相关知识,希望可以帮助到你。

本文目录一览:

使用turtle.onclick的Python 3.0(python turtle onclick)

使用turtle.onclick的Python 3.0(python turtle onclick)

所以这是我的问题,我必须为我的CS课做一张照片,这在使用Turtle进行估算时确实令人沮丧。我计划使用.onclick()来显示位置。

import turtle as tdef getPos(x,y):    print("(", x, "," ,y,")")    returndef main():    t.onclick(getPos)    t.mainloop()main()

乌龟文档似乎说onclick将在带有两个变量的函数中传递坐标。

http://docs.python.org/3.1/library/turtle.html#turtle.onclick

注意:当我单击箭头时,它可以工作,但这不是我想要的。我想单击屏幕上的其他位置以找出将箭头发送至的坐标!

任何帮助,将不胜感激。

答案1

小编典典

好吧,我想出了一个解决方法。它不是一个完美的解决方案,但效果很好。因为onclick仅在您单击箭头时才会响应,所以我使箭头覆盖了整个屏幕。然后我把它藏起来了。您需要做的是将鼠标悬停在您想去的位置上,按“
a”,当它变黑时,单击屏幕。然后外壳将显示您需要的坐标。确保始终返回(1000,0)。

import turtle as tdef showTurtle():    t.st()    returndef getPos(x,y):    print("(", x, "," ,y,")")    returndef hideTurtle(x,y):    t.ht()    returndef main():    t.speed(20)    t.shapesize(1000,1000)    t.up()    t.goto(1000,0)    t.ht()    t.onkey(showTurtle,"a")    t.listen()    t.onclick(getPos)    t.onrelease(hideTurtle)    t.mainloop()main()

另外,如果班上有人发现这个问题,我是宾汉姆顿的CS学生,如果您使用它,我建议不要留下任何痕迹。教授已经看到并会认识到这一点。

Python turtle print TaiChi

Python turtle print TaiChi

import turtle
turtle.pensize(20)
turtle.pencolor("black")
turtle.penup()
turtle.goto(0,300)
turtle.down()
turtle.begin_fill()
turtle.fillcolor("black")
turtle.circle(-300,180)
turtle.circle(-150,180)
turtle.circle(150,180)
turtle.end_fill()

turtle.circle(
300,180) turtle.penup() turtle.goto(-20,-170) turtle.pendown() turtle.pencolor("white") turtle.begin_fill() turtle.fillcolor("white") turtle.circle(20,360) turtle.end_fill() turtle.penup() turtle.goto(-20,140) turtle.pendown() turtle.begin_fill() turtle.fillcolor("black") turtle.pencolor("black") turtle.circle(20,360) turtle.end_fill()

 

Python turtle 画蛇

Python turtle 画蛇

本节我们将利用画笔相关指令来画一条蛇,如下图

仔细观察这个图,弯曲的蛇身,可以利用circle命令,来画弧线。然后向前直行,再画一个180度的弧线,让蛇头转回来,这样基本上就可以了。

程序示例:

import turtle

def drawSnake(rad, angle, len, neckrad):

for _ in range(len):# 画弧线,弯曲的蛇身

turtle.circle(rad, angle)

turtle.circle(-rad, angle)

turtle.circle(rad, angle/2)

turtle.forward(rad/2) # 直线前进

turtle.circle(neckrad, 180)#回头

turtle.forward(rad/4) # 直线前进

if __name__ == “__main__”:

turtle.setup(1500, 1400, 0, 0)

turtle.pensize(30) # 画笔尺寸

turtle.pencolor(“green”)

turtle.seth(-40) # 前进的方向

drawSnake(70, 80, 2, 15)

Python Turtle 绘图

Python Turtle 绘图

Turtle 绘图


海龟绘图 (Turtle Graphics),原本是发明给儿童学习编程的。现在也移植到许多高级语音中,Python 也内置了 turtle 库,基本上能够复原 Turtle Graphics 的功能。

先看看如何用 turtle 进行绘图:

# 导入turtle包的所有内容:
from turtle import *

# 设置笔刷宽度:
width(4)

# 前进:
forward(200)
# 右转90度:
right(90)

# 笔刷颜色:
pencolor(''red'')
forward(100)
right(90)

pencolor(''green'')
forward(200)
right(90)

pencolor(''blue'')
forward(100)
right(90)

# 调用done()使得窗口等待被关闭,否则将立刻关闭窗口:
done()

上面示例中实现的效果如下图:

矩形

在这个例子中,就是指挥海龟移动,而移动的轨迹就是绘制的线条。在这里,绘制矩形,就是让海龟前进,右转 90 度,反复 4 次。

上面的代码中,width() 用来设置画笔的宽度, pencolor() 用来设置画笔颜色。还有更多的操作可以参考官网的说明:

https://docs.python.org/3.7/library/turtle.html#turtle-methods

示例中,最后的有个 done() 函数,这个函数主要的作用是阻塞,让窗口进入消息循环,等待被关闭。若是没有调用这个参数,程序会在图形绘制完后直接关闭窗口。

turtle 的函数都非常简单。当需要使用时,可以直接在官网进行查询印证使用。

在这里,也尝试使用 turtle 绘制 HELLO WORLD 字样。

# -*- coding: utf-8 -*-
"""
@Time: 2020/3/15 21:01
@File: turtle_.py
@Author: Damon
@Contact: yiluolion@126.com
"""

# put the import lib here
from turtle import *

width(4)

pu()
goto(-450, 0)
pd()
rt(90)
fd(80)
pu()
rt(180)
fd(40)
rt(90)
pd()
fd(60)
pu()
lt(90)
fd(40)
pd()
rt(180)
fd(80)

pu()
goto(-370, 0)
lt(90)
pd()
fd(60)
pu()
rt(180)
fd(60)
lt(90)
pd()
fd(80)
lt(90)
fd(60)
pu()
goto(-370, -40)
pd()
fd(60)


pu()
goto(-290, 0)
rt(90)
pd()
fd(80)
lt(90)
fd(60)

pu()
goto(-210, 0)
rt(90)
pd()
fd(80)
lt(90)
fd(60)

pu()
goto(-130, 0)
pd()
fd(60)
rt(90)
fd(80)
rt(90)
fd(60)
rt(90)
fd(80)

pu()
goto(0, 0)
rt(160)
pd()
fd(82)
lt(140)
fd(82)
rt(140)
fd(82)
lt(140)
fd(82)

pu()
goto(130, 0)
rt(70)
pd()
fd(60)
rt(90)
fd(80)
rt(90)
fd(60)
rt(90)
fd(80)

pu()
goto(210, 0)
rt(180)
pd()
fd(80)
pu()
rt(180)
fd(80)
rt(90)
pd()
fd(50)
rt(45)
fd(14.14)
rt(45)
fd(20)
rt(45)
fd(14.14)
rt(45)
fd(50)
# fd(60)
# rt(90)
# fd(40)
# rt(90)
# fd(60)
pu()
rt(180)
fd(40)
pd()
rt(60)
fd(44.72)

pu()
goto(290, 0)
rt(30)
pd()
fd(80)
lt(90)
fd(60)

pu()
goto(370, 0)
rt(90)
pd()
fd(80)
pu()
rt(180)
fd(80)
rt(90)
pd()
fd(50)
rt(45)
fd(14.14)
rt(45)
fd(60)
rt(45)
fd(14.14)
rt(45)
fd(50)

done()

上面的例子中,rt() 表示右转,lt() 表示左转,fd() 表示前进,pu() 表示提起画笔,pd 表示落下画笔。

下面看看实现的效果:

实现效果

turtle 使用非常简单。如果想要绘制一些图形,需要花时间的部分主要是设计 turtle 的走向。如果觉得有意思的话,也可以尝试一下。


以上就是本篇的主要内容

欢迎关注微信公众号《书所集录》

python turtle 雪花

python turtle 雪花

import turtle
import random
 
t = turtle
 
t . setup ( 800 , 600 ) #设置画布大小,参数1:x大小,参数2:y大小
t . bgcolor ( "black" ) #设置画布颜色,参数1:颜色名/颜色值如:#fff
t . speed ( 0 ) #设置画笔画图速度,参数1:速度,0为最快
 
t . begin_poly () #开始记录多边形
t . color ( "white" ) #设置画笔和图形颜色,参数1:颜色名/颜色值如:#fff
#绘制雪花
for i in range ( 0 , 8 ): #重复8次,从0开始
    t . forward ( 10 ) #画笔前进,参数1:像素
    t . right ( 90 ) #画笔右转:参数1:角度
    t . forward ( 1 )
    t . right ( 90 )
    t . forward ( 10 )
    t . left ( 135 ) #画笔左转:参数1:角度
t . end_poly () #结束记录多边形
t . register_shape ( "xue_hua" , t . get_poly ()) #注册多边形,参数1:名字,参数2:多边形
t . shape ( "xue_hua" ) #设置画笔形状,参数1:形状名
 
clones =[] #定义数组
 
for i in range ( 0 , 100 ): #循环100次,从0开始
    clones . append ( t . clone ()) #1、 clones.append:向数组中加入元素。2、t.clone():克隆turtle库对象t
 
t . hideturtle () #隐藏画笔
t . clear () #清屏
 
t . tracer ( False ) #关闭/开启画笔在画图过程中的动画,参数1:True开启,False关闭
while True : #无限循环
    for c in clones : #遍历clones对象
        c .penup() #抬起画笔
        c .color( "white" )
        c .goto( random . randint (- 400 , 400 ), random . randint (- 300 , 300 )) #1、goto:让画笔移到指定的x,y位置。2、random.randint:生成指定范围内的整形随机数
    t . update () #刷新屏幕一次,以显示画布上的元素的最新状态或者说位置。在禁用画笔动画时候使用
 
t.done() #保持住窗口

关于使用turtle.onclick的Python 3.0python turtle onclick的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Python turtle print TaiChi、Python turtle 画蛇、Python Turtle 绘图、python turtle 雪花等相关知识的信息别忘了在本站进行查找喔。

本文标签: