Python 3 : How to create commands in loop for buttons grid? -


i began learning python me create little ui manage windows clients on renderfarm. here example of problem :

import tkinter tk  #my variable : number of clients specified user numclients = 10  class app(tk.tk):     def __init__(self):         tk.tk.__init__(self)          in range (0,numclients):             = i+1              #name buttons, commands , texts             button = ("b",i,"_01")             cmd = ("command"+str(i))             rn = ("rn0"+str(i))              #create buttons on grid text             self.button = tk.button(self, text= rn, command = self.cmd)             self.button.grid(row=i, column=0, sticky="s, n, e, w")      #the command want duplicate each button     def cmd (self):         #print button name         print (rn)           app = app() app.mainloop() 

i need create many rows have clients (specified in variable "numclients") , associate specific command each 1 of them, :

button1 pressed => print text "rn01" button2 pressed => print text "rn02" 

... etc.

i don't how loop command looped button , associate both. saw somewhere there might "late binding" problem looping functions. tried using lists never succeed.

i hope can me that.

thanks , patience.

like works better :

def cmd(self, name): print(name) ...     self.button = tk.button(self, text= rn,                             command=lambda name=rn: self.cmd(name) 

and if want more variables :

def cmd(self, var1, var2):     print(var1)     print(var2) ...     self.button = tk.button(self, text= rn,                             command=lambda var1=button, var2=rn : self.cmd(var1, var2)) 

thanks again help.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -