【问题标题】:Using turtle's onclick in a loop在循环中使用海龟的 onclick
【发布时间】:2023-03-29 02:14:01
【问题描述】:

我正在尝试从函数内的字符串dict 创建一个简单的接口。每个字符串用于显示文本和一个可点击的海龟(使用onclick() 使海龟可点击)。这样我就可以点击海龟,然后输入一些值。

但是,如果我理解正确,当函数完成通过字典运行循环时,循环应该定义的所有变量都被重新定义为最后一个通过循环传递的变量,字典的最后一个元素.所以onclick() 函数只返回字典的最后一个元素。

有没有办法让onclick() 函数对字典的每个元素做出不同的反应/对应?我真的不想为字典的每个元素编写一堆onclick() 函数。我正在努力学习更好的方法。

list_alunos={'joao': ['5', 'm'],'maria': ['5', 'm'],'lobo': ['5', 'm'],'mau': ['5', 'm']}
def caca(dictx,file):
    import turtle
    mes=file
    vert=350
    hor=-600


    def got(t,x,y,d) :
        t.penup()
        t.goto(x,y)
        t.pendown()
        t.seth(d)

    def text(t,text, size, color, pos1, pos2):
        t.penup()
        t.goto(pos1, pos2)
        t.color(color)
        t.begin_fill()
        t.write(text, font=('Arial', size, 'normal'))
        t.end_fill()




    new_vert = vert
    for key in dictx:
        nome = key
        if vert == -340:
            new_vert = 350
            new_hor = hor + 250
        if vert!= -340:
            new_vert= new_vert-30
            new_hor = hor
            txt_vert = new_vert - 15
            txt_hor = new_hor + 20

        screen = turtle.Screen()

        width = 1200
        height = 1500
        turtle.screensize(width, height)



        tnome = turtle.Turtle(shape='turtle')
        tnome.color('pink')
        textnome = turtle
        tnome.speed('fastest')
        textnome.speed('fastest')

        text(textnome, '%s' %(nome), '20', 'pink', txt_hor,txt_vert)

        got(tnome,new_hor,new_vert,0)
        def tnome_handler(x, y):

            pos = list(dictx.keys()).index(nome)
            listt = list(dictx)

            pnt = screen.textinput(' pontuação', '%s:  '%(listt[pos]))
            pnt = [int(x) for x in pnt.split()]
            if len(pnt) == 5 :
                with open('%s.py' %(mes), 'a') as fd:
                    fd.write('\n%s.pontuacao(%i,%i,%i,%i,%i)' % (nome,pnt[0],pnt[1],pnt[2],pnt[3],pnt[4]))
                tnome.color('blue')
        tnome.onclick(tnome_handler)


caca(list_alunos,'mm')

【问题讨论】:

    标签: python python-3.x onclick turtle-graphics


    【解决方案1】:

    我相信您的代码的以下返工可以满足您的需求。它有效地为每个对其做出唯一响应的海龟/按钮生成自定义处理程序:

    from turtle import Screen, Turtle
    
    WIDTH, HEIGHT = 1200, 1500
    
    dict_alunos = {'joao': ['5', 'm'], 'maria': ['5', 'm'], 'lobo': ['5', 'm'], 'mau': ['5', 'm']}
    
    def text(t, text, size, color, x, y):
        t.penup()
        t.goto(x + size, y - size/2)
        t.color(color)
        t.write(text, align='left', font=('Arial', str(size), 'normal'))
        t.goto(x, y)
    
    def caca(dictionary, mes):
    
        def tnome_handler(nome, turtle, x, y):
    
            pnt = screen.textinput('pontuação', '%s:  ' % (nome))
            pnt = [int(x) for x in pnt.split()]
    
            if len(pnt) == 5:
                with open('%s.py' % (mes), 'a') as fd:
                    fd.write('s.pontuacao(%i, %i, %i, %i, %i)\n' % (nome, pnt[0], pnt[1], pnt[2], pnt[3], pnt[4]))
    
            turtle.color('blue')
    
        vert = 350
        hor = -600
    
        new_vert = vert
    
        for nome in dictionary:
    
            if vert == -340:
                new_vert = 350
                new_hor = hor + 250
    
            if vert != -340:
                new_vert = new_vert - 30
                new_hor = hor
                txt_vert = new_vert - 15
                txt_hor = new_hor + 20
    
            turtle = Turtle(shape='turtle')
            turtle.color('pink')
            turtle.speed('fastest')
            turtle.setheading(0)
    
            text(turtle, '%s' % (nome), 20, 'pink', txt_hor, txt_vert)
    
            turtle.onclick(lambda x, y, n=nome, t=turtle: tnome_handler(n, t, x, y))
    
    screen = Screen()
    screen.setup(WIDTH, HEIGHT)
    
    caca(dict_alunos, 'mm')
    
    screen.mainloop()
    

    我还重新编写了代码以适应更常见的编程设计。

    【讨论】:

      猜你喜欢
      • 2018-04-16
      • 2019-04-21
      • 2020-05-11
      • 2014-05-01
      • 2021-02-25
      • 2019-04-30
      • 2018-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多