0

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?

Community
  • 1
  • 1
dee cue
  • 983
  • 1
  • 7
  • 23

1 Answers1

0

The part of your code that is wrong is the part where you use both grid and pack for widgets that have the same parent. You can use only one or the other. If you want to use grid for the buttons, you must use grid for every widget that has the same parent.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685