欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python小实例混合使用turtle和tkinter让小海龟互动起来

程序员文章站 2022-06-19 15:23:35
turtle 窗口请看下图,turtle窗口图标是一片小叶子,估计它就是继承自 tkinter 库。tkinter 窗口参阅了一些资料,发现 turtle 方法可直接在 tkinter 的画布canv...

turtle 窗口

请看下图,turtle窗口图标是一片小叶子,估计它就是继承自 tkinter 库。

Python小实例混合使用turtle和tkinter让小海龟互动起来

tkinter 窗口

参阅了一些资料,发现 turtle 方法可直接在 tkinter 的画布canvas上操作:

Python小实例混合使用turtle和tkinter让小海龟互动起来

源代码

from tkinter import *
from turtle import rawturtle
 
def circ():
    tu.penup()
    tu.home()
    tu.clear()
    tu.speed(0)
    [x,y,r] = et1.get().split(',')
    try:
        x = int(x.replace('(',''))
        y = int(y.replace(')',''))
        r = int(r.strip())
    except:
        x,y,r = 0,0,50  #输入错误则赋予默认值
    
    tu.goto(x,y-r)
    tu.pendown()
    tu.circle(r)
    
def rect():
    tu.penup()
    tu.home()
    tu.clear()
    tu.speed(0)
    tu.color('red', 'yellow')
    tu.begin_fill()
    [d,rad] = et2.get().split(',')
    try:
        d = int(d)
        rad = int(rad)
    except:
        d,rad = 200,216  #输入错误则赋予默认值
    tu.pendown()
    tu.back(d//5)
    while true:
        tu.forward(d)
        tu.left(rad)
        if abs(tu.pos()[0]+d//5)<0.1 and abs(tu.pos()[1])<0.1:
            break
    tu.end_fill()
 
def taiji():
    tu.penup()
    tu.home()
    tu.clear()
    tu.speed(0)
    d = et3.get()
    try:
        d = int(d)
    except:
        d = 120  #输入错误则赋予默认值
    tu.hideturtle()
    tu.goto(d//2,-d)
    tu.pendown()
    tu.begin_fill()
    tu.color('black','black')
    tu.circle(d,extent=180)
    tu.circle(d//2,extent=180)
    tu.circle(-d//2,extent=180)
    tu.end_fill()
    tu.circle(-d,extent=180)
    tu.penup()
    tu.goto(d//2,-d//6*4)
    tu.pendown()
    tu.begin_fill()
    tu.fillcolor("black")
    tu.circle(d//5,extent=360)
    tu.end_fill()
    tu.penup()
    tu.goto(d//2,d//3)
    tu.pendown()
    tu.begin_fill()
    tu.fillcolor("white")
    tu.circle(d//5,extent=360)
    tu.end_fill()
    tu.penup()
 
def main():
    global tu,et1,et2,et3
    root = tk()
    root.geometry('520x520+150+300')
    root.title('turtle在tkinter.canvas上的操作')
    root.resizable(false, false)
    canvas = canvas(root, width=640, height=400)
    canvas.pack()
 
    tu = rawturtle(canvas)
    tu.hideturtle()
 
    et1 = entry(root, width=12)
    et1.place(x = 30, y = 480)
    et1.insert(0,'(50,-20), 100')
    bt1 = button(root,text=' 画圆 ',command=circ)
    bt1.place(x = 60, y = 425)
 
    et2 = entry(root, width=12)
    et2.place(x = 190, y = 480)
    et2.insert(0,'200, 216')
    bt2 = button(root,text=' 多角星(或多边形) ',command=rect)
    bt2.place(x = 180, y = 425)
    
    et3 = entry(root, width=12)
    et3.place(x = 360, y = 480)
    et3.insert(0,'120')
    bt3 = button(root,text=' 太极 ',command=taiji)
    bt3.place(x = 380, y = 425)
 
    root.mainloop()
 
 
if __name__ == '__main__':
 
    main()

turtle和tkinter混合使用

turtle和tkinter两者搭配使用,使得 turtle 的画图参数可以由 tkinter 的控件当场设置,这样就实现了turtle小海龟的现场互动,运行效果见图二。

三个entry控件,分别设置:
  1. 圆心坐标和圆的直径;
  2. 多边形的边长和边的转动角度;
  3. 太极图的外圆直径。

(本篇完)

到此这篇关于python小实例混合使用turtle和tkinter让小海龟互动起来的文章就介绍到这了,更多相关python turtle 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!