如何使用按钮在模式之间进行切换

时间:2019-12-06 10:16:42

标签: python python-2.7 turtle-graphics

我创建了2种绘制模式,但是它们同时绘制,但是我希望它们能够分开工作,所以我创建了一个按钮,但是不知道如何定义该按钮的模式更改才能起作用

如何定义模式更改,以便可以在不同的绘图模式之间进行更改,例如徒手模式,圆圈模式,以便它们不能同时工作

from turtle import Screen, Turtle

beni=Screen()
beni.setup(400, 400, 10, 10)
beni.setworldcoordinates(-300, -300, 300, 300)

def mode_change(x, y):



#freehandmode
def freehandmode(x, y):
    t.ondrag(None)
    t.setheading(t.towards(x, y))
    t.goto(x, y)
    t.ondrag(freehandmode)



#circlemode
def draw_circle(x, y):
    beni.onclick(None)
    center = turtle.position()
    turtle.setposition(x, y)
    turtle.setheading(turtle.towards(center) - 90)
    turtle.pendown()
    turtle.circle(turtle.distance(center))
    turtle.penup()
    turtle.clearstamps()
    beni.onclick(pick_center)

def pick_center(x, y):
    beni.onclick(None)
    turtle.setposition(x, y)
    turtle.stamp()
    beni.onclick(draw_circle)

turtle = Turtle()
turtle.hideturtle()
turtle.shape('circle')
turtle.shapesize(0.5)
turtle.penup()

beni.onclick(pick_center)


#freehand turtle
t = Turtle('circle')
t.shapesize(1)
t.speed('fastest')
t.ondrag(freehandmode)

#modechange turtle
modechange = Turtle('circle')
modechange.pu()
modechange.shapesize(0.55, 1.45)
modechange.color('black')
modechange.setpos(0, 290)
modechange.onclick(mode_change)

#modebutton
modebutton = Turtle()
modebutton.shapesize(0.25)
modebutton.pu()
modebutton.setpos(-20, 300)
modebutton.setheading(270)
modebutton.pd()
modebutton.fd(20)
modebutton.setheading(0)
modebutton.fd(45)
modebutton.setheading(90)
modebutton.fd(20)
modebutton.setheading(180)
modebutton.fd(45)
modebutton.pu()
modebutton.setpos(-10, 263)
modebutton.setheading(0)
modebutton.color('green')
modebutton.write('Modi')
modebutton.hideturtle()

beni.mainloop()

1 个答案:

答案 0 :(得分:0)

主要是会计问题-每次切换模式时,您需要明确列出禁用一只乌龟并启用另一只乌龟所需的步骤。并且您需要跟踪当前模式。这是我对您的代码(实际上很大程度上是 my )进行的返工和简化,以实现此目的:

from turtle import Screen, Turtle, mainloop

def mode_change(x, y):
    global mode

    if mode == 'freehand':
        # disable freehand
        freehand.ondrag(None)
        freehand.hideturtle()

        # enable circle
        screen.ontimer(lambda: screen.onclick(pick_center))  # so screen doesn't inherit *this* click
        mode = 'circle'
    else:
        # disable circle
        screen.onclick(None)

        # enable freehand
        freehand.ondrag(freehandmode)
        freehand.showturtle()
        mode = 'freehand'

# freehand mode
def freehandmode(x, y):
    freehand.ondrag(None)

    freehand.setheading(freehand.towards(x, y))
    freehand.goto(x, y)

    freehand.ondrag(freehandmode)

# circle mode
def draw_circle(x, y):
    screen.onclick(None)

    center = circle.position()
    circle.setposition(x, y)
    circle.setheading(circle.towards(center) - 90)
    circle.pendown()
    circle.circle(circle.distance(center))
    circle.penup()
    circle.clearstamps()

    screen.onclick(pick_center)

def pick_center(x, y):
    screen.onclick(None)

    circle.setposition(x, y)
    circle.stamp()

    screen.onclick(draw_circle)

screen = Screen()
screen.setup(400, 400)

mode = 'freehand'

circle = Turtle('circle', visible=False)
circle.shapesize(0.5)
circle.speed('fastest')
circle.penup()

freehand = Turtle('circle')
freehand.speed('fastest')
freehand.ondrag(freehandmode)

# modechange turtle
modechange = Turtle('square')
modechange.penup()
modechange.setposition(0, 155)
modechange.pencolor('green')
modechange.write('Modi', align='center')

modechange.shapesize(2.25, 1.0)
modechange.setposition(0, 190)
modechange.onclick(mode_change)

mainloop()