【发布时间】: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