By referring to Python tkinter creating buttons in for loop passing command arguments , I modified the code to become like this:
self.listarray = ["Option A", "Option B", "Option C", "Option D"]
for i in range(len(self.listarray)):
self.buttonlist.append(tk.Button(self, text=self.listarray[i], command=lambda i=i: self.printthis(i)))
self.buttonlist[i].grid(column=1, row=i+1, sticky="W")
self.mainloop()
def printthis(self, thenum):
print thenum
self.destroy()
so that there will be four buttons of "Option A", "Option B", and so on...
But, what happened was that my window freezes and unable to do anything. So I closed it.
This is not a problem when I changed this line:
self.buttonlist[i].grid(column=1, row=i+1, sticky="W")
become this
self.buttonlist[i].pack()
But I want to customise the location of my button as well. Which part of my codes are wrong?