Apr-11-2023, 06:23 PM
I am having trouble writing a python tkinter GUI. I am trying to create a popup window which has a grid of buttons that then returns a value. In the code example below I have flatted the loop to illustrate the problem, but I plan to use a loop. The problem I am having is that if I pass the routine "setval" a string, then it works fine. If I try to pass the routine "setval" a variable, then it gets the last value of names[kk].
I considered a grid of menu buttons. However, I plan to have over 100 buttons in this project and each with 50 choices. In the past this many radio buttons creates performance issues.
I considered a grid of menu buttons. However, I plan to have over 100 buttons in this project and each with 50 choices. In the past this many radio buttons creates performance issues.
#!/usr/bin/python3 from tkinter import * root = Tk() root.title("Root Window") root.geometry("450x300") def setval(window,var,value): var.set(value) print(value) print(type(value)) window.destroy() def open_popup1(): popup1 = Toplevel(root) popup1.title("choices") # plan to have 50 choices choices = ("red", "one", "blue", "two", "err") kk=0 Button(popup1, text = choices[kk], command=lambda: setval(popup1,button_text,choices[kk])).grid(row=0,column=0) kk=1 Button(popup1, text = choices[kk], command=lambda: setval(popup1,button_text,choices[kk])).grid(row=0,column=1) kk=2 Button(popup1, text = choices[kk], command=lambda: setval(popup1,button_text,"blue")).grid(row=1,column=0) kk=3 Button(popup1, text = choices[kk], command=lambda: setval(popup1,button_text,"two")).grid(row=1,column=1) kk=4 foo=choices[kk] popup1.mainloop() # kk=0 # for rr in range(0,2): # for cc in range(0,2): # Button(top1, text = choices[kk], command=lambda: setval(top1,button_text,choices[kk])).grid(row=rr,column=cc) # kk=kk+1 button_text = StringVar() button_text.set("a") # plan to have 100 buttons button = Button(root, textvariable=button_text, command = open_popup1) button.pack() root.mainloop()ss