1
I developed a software that sends consumer data to a database. I made another application to receive this data and generate a graph.
My goal is to create specific button for each consumption day. But when creating the buttons in a list using the loop for
, the command sends the final index and not the expected index.
For example, the first button has index 0, but clicking on it sends the last index of the list as argument to the function that receives the command.
def tela_inicio(self):
self.menu.destroy()
self.menu = Tk()
self.menu.title("Monitoramento - Sabrina")
self.menu.geometry("1060x380+0+0")
self.voltar = Button(self.menu, text = "Voltar", bg = "#040c31", fg = "white")
self.voltar["command"] = self.c_menu
self.f_dias() #Essa função me retorna os dias monitorados
botao = list()
for i in range(len(self.dia)):
#ao criar o botao o texto dele fica correto
botao.append(Button(self.menu, text = f"{self.dia[i]}"))
botao[i].grid()
#A função grafico recebe o dia como argumento para plotar um grafico com os dados referente ao mesmo
botao[i]["command"] = lambda: self.grafico(self.dia[i])
self.voltar.grid()
self.menu.mainloop()
Éder, unfortunately I had a problem with my laptop. But as soon as I get the chance, I’ll test it, but looking at it, I think you’ll still have the same problem. You can test for us by making a function that returns the text of the button clicked please?
– Jonas Duarte
Now, Jonas, when you press the button, you print the key and the value of the dictionary the day you name the button. Simply adapt the display button function in your project.
– Éder Garcia
Thank you so much, Eder! Got me out of a giant lock. I was locking everything because of this kkk. Thank you very much
– Jonas Duarte
I’m glad you helped Jonas, hug and see you next time!
– Éder Garcia