【问题标题】:Why doesn't this Python 2.7 Turtle code process penup?为什么这个 Python 2.7 Turtle 代码不处理 penup?
【发布时间】:2018-01-26 15:00:14
【问题描述】:

下面是我的代码(它不处理 penup)。这段代码有什么问题? (对不起我的英语不好。)

import turtle

import time

t = turtle.Turtle()

wn = turtle.Screen()

t.ht()

t.pensize(6)

t.speed(0)

wn.title("Trivia")

class trivia():

    def __init__(self):

        self.BlueBtn()

    def Btn(self):
        t.begin_fill()
        for p in xrange(2):
            t.left(90)
            t.fd(170)
            t.lt(90)
            t.fd(470)
        t.end_fill()


    def BlueBtn(self):               #button a - the blue button
        t.penup()
        t.setx(-370)
        t.pendown()

        t.color("blue","aqua")   #make the button's color
        self.Btn()               #make the button
        self.RedBtn()

    def RedBtn(self):                #button b - the red button
        t.pu()
        t.setx(370)
        t.pd()
        t.color("darkred","red")   #make the button's color
        self.Btn()
        self.GreenBtn()

    def GrennBtn(self):               #button c - the green button
        t.pu()
        t.sety(-400)
        t.pd()

        t.color("darkgreen","green") #make the button's color
        self.Btn(self)                   #make the button
        self.GoldBtn()

    def GoldBtn(self):                #make button d - the gold button
        t.pu()
        t.setx(-370)
        t.pd()

        t.color("gold","khaki")   #make the button's color
        self.Btn()                #make the button


turtle.mainloop()                 #exit from the turtle screen while pressing on the cross

game = trivia()

【问题讨论】:

  • 请编辑问题以使代码部分更易于阅读。使用 Ctrl+K 正确缩进所选代码段
  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。另请阅读How to create a Minimal, Complete, and Verifiable example.

标签: python-2.7 turtle-graphics


【解决方案1】:

这段代码的主要问题是这个序列:

turtle.mainloop()
game = trivia()

当您调用mainloop() 时,您将控制权交给了 Tk 事件循环,因此它应该是您做的最后一件事。此后的命令在海龟关闭之前不会被执行。所以反过来:

game = trivia()
turtle.mainloop()

还有很多小错误(例如,self.Btn(self) 传递了两个 self 参数而不是一个),我试图在重写应该在 Python 2 或 3 下运行的代码时解决这些错误:

from turtle import Turtle, Screen, mainloop

class trivia():

    def __init__(self):
        self.BlueBtn((-550, 100))
        self.RedBtn((100, 100))
        self.GreenBtn((100, -250))
        self.GoldBtn((-550, -250))

    def Btn(self, position):
        turtle.penup()
        turtle.setposition(position)
        turtle.pendown()

        turtle.begin_fill()

        for _ in range(2):
            turtle.forward(450)
            turtle.left(90)
            turtle.forward(150)
            turtle.left(90)

        turtle.end_fill()

    def BlueBtn(self, position):  # button a - the blue button
        turtle.color('blue', 'cyan')  # I don't have 'aqua'
        self.Btn(position)

    def RedBtn(self, position):  # button b - the red button
        turtle.color('darkred', 'red')
        self.Btn(position)

    def GreenBtn(self, position):  # button c - the green button
        turtle.color('darkgreen', 'green')
        self.Btn(position)

    def GoldBtn(self, position):  # make button d - the gold button
        turtle.color('gold', 'khaki')
        self.Btn(position)

screen = Screen()
screen.setup(1200, 600)
screen.title('Trivia')

turtle = Turtle(visible=False)
turtle.speed('fastest')
turtle.pensize(6)

game = trivia()

mainloop()  # exit from the turtle screen by pressing on the cross

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 2017-04-09
    • 1970-01-01
    相关资源
    最近更新 更多