Basically, I want to have a button which, when pressed, will add stuff to my window. Here’s some minimum code:
from Tkinter import *
def create_line (N):
""" """
Label (root, text= "Color ").grid(row=N, column=0, padx=3)
OptionMenu (root, v, *optionList).grid(row=N, column=1, padx=3)
Button (root, text="+", command=add_line(N)).grid(row=N, column=2, padx=3)
def add_line (M):
M = M +1
Label (root, text= "Color ").grid(row=M, column=0, padx=3)
OptionMenu (root, v, *optionList).grid(row=M, column=1, padx=3).grid(row=M,
column=2, padx=3)
return 1
root = Tk()
optionList = ("red", "green", "blue")
current_row = 0
v = StringVar()
v.set(optionList[0])
create_line(current_row)
mainloop()
If you comment out the code inside the add_line function (except the return line) and run the code, you will see a label, an options menu, and a button. I want the ‘+’ button to create another row with the same widgets. This minimum code is not my real app, but this is the core of what I can’t do.
I know it can be done, because I have an app I downloaded that changes the menu options dynamically when I button is pressed, but that technique doesn’t seem to work for me.
Thanks.
One problem that I see right away is:
should be:
As it is written, you’re calling the function
add_linewhen the Button is created, not when it is pressed.