【问题标题】:Turtle Module for PythonPython 的海龟模块
【发布时间】:2014-04-28 01:33:49
【问题描述】:

有没有一种方法可以向使用 turtle 模块的窗口添加按钮?

import turtle
import tkinter as tk
from time import sleep


t = turtle.Turtle()
ts = t.getscreen()
ts.register_shape("plane.gif")
t.shape("plane.gif")
t.pencolor("white")
ts.onscreenclick(t.goto)
ts.bgpic('sky.gif')

【问题讨论】:

  • 你能在窗口中添加任何按钮吗?当按钮与海龟相关时,是否有任何具体问题?您遇到什么类型的问题,没有显示、无法正常工作或有什么不同?
  • 没问题,我只是想添加一个按钮让用户退出程序或进入下一个窗口。

标签: python turtle-graphics


【解决方案1】:

我的朋友做了一个:

import turtle
import random
t=turtle.Pen()
def forward():
    t.forward(50)
def left():
    t.left(90)
def right():
    t.right(90)
def red():
    t.color('red')
def green():
    t.color('green')
def blue():
    t.color('blue')
def yellow():
    t.color('yellow')
def black():
    t.color('black')
def special_color():
    t.color(random.random(),random.random(),random.random())
def up():
    t.up()
def down():
    t.down()
def circle():
    t.circle(30)
#def writepi():
#    t.write('π', font('Times New Roman',30,'bold'))
def circleshape():
    t.shape('circle')
#def square():
 #   t.shape('square')
def triangle():
    t.shape('triangle')
def arrow():
    t.shape('arrow')
def classic():
    t.shape('classic')
def turtle():
    t.shape('turtle')
def layegg():
    t.down()
    t.begin_fill()
    t.circle(4)
    t.end_fill()
    t.up()
def reset():
    t.home()
    t.clear()

from tkinter import*
tk=Tk()
forward=Button(tk,text='forward', command=forward)
forward.pack()

left=Button(tk,text='left', command=left)
left.pack()

right=Button(tk,text='right', command=right)
right.pack()

red=Button(tk,text='red', command=red)
red.pack()

green=Button(tk,text='green', command=green)
green.pack()

blue=Button(tk, text='blue', command=blue)
blue.pack()

yellow=Button(tk, text='yellow', command=yellow)
yellow.pack()

black=Button(tk, text='black', command=black)
black.pack()

specialcolor=Button(tk, text='random color', command=special_color)
specialcolor.pack()

up=Button(tk, text='up', command=up)
up.pack()

down=Button(tk, text='down', command=down)
down.pack()

circle=Button(tk, text='circle', command=circle)
circle.pack()

#pi=Button(tk, text='π', command=writepi)
#pi.pack()

cs=Button(tk, text='make it a circle', command=circleshape)
cs.pack()

#square=Button(tk, text='make it a square', command=square)
#square.pack()

triangle=Button(tk, text='make it a triangle', command=triangle)
triangle.pack()

arrow=Button(tk, text='make it an arrow', command=arrow)
arrow.pack()

clasic=Button(tk, text='back to normal', command=classic)
clasic.pack()

turtle=Button(tk, text='make it a turtle', command=turtle)
turtle.pack()

egglay=Button(tk, text='lay an egg', command=layegg)
egglay.pack()

resset=Button(tk, text'reset it all', command=reset)
resset.pack()

【讨论】: