"[Original Post] I am trying to use the Radiobuttons to display more input fields after I select a specific option in my tkinter window, but the value of my 'selection' variable always default to 0, and doesn't ever change.[now fixed]
And, in addition to this, I am not sure how to keep updating my tkinter window as I switch between the options. I tried to implement the tkinter update() function, but it also hasn't worked for me yet. Any helpful suggestions will be highly appreciated.[Original Post Ends]"
This is a follow-up to my original post, is there a way to clear up the multiple input fields after I switch between the number of the entries. ie. How do I refresh the tkinter window to keep the number of the entries consistent with my selection?
I posted my demo code below...
from tkinter import *
import time
def selection_window():
selection = var.get()
### Debug ###
print("going in...")
print(selection)
if selection == 1:
Label(master, text="Info").grid(row=0, column=2)
Label(master, text=" Input:").grid(row=1, column=2)
e3 = Entry(master)
e3.grid(row=1, column=3)
print("going in2...") ### Debug ###
elif selection == 2:
Label(master, text="Info").grid(row=0, column=2)
Label(master, text=" Input:").grid(row=1, column=2)
e3 = Entry(master)
e3.grid(row=1, column=3)
#####################################################################
#####################################################################
Label(master, text="Info2").grid(row=0, column=4)
Label(master, text=" Input2:").grid(row=1, column=4)
e23 = Entry(master)
e23.grid(row=1, column=5)
print("going in3...") ### Debug ###
if __name__ == '__main__':
master = Tk()
master.title('Demo A')
#####################################################################
### Display - Multiple fields
Label(master, text="More Info").grid(row=0, column=0)
Label(master, text="Type:").grid(row=1, column=0)
e7 = Entry(master)
e7.grid(row=1, column=1)
#####################################################################
#####################################################################
Button(master, text='Quit', command=master.quit).grid(row=30, column=0, sticky=W, pady=10)
#####################################################################
#####################################################################
var = IntVar()
Label(master, text="Select options").grid(row=33, column=0, sticky=W)
Radiobutton(master, text="1", variable=var, value=1).grid(row=33, column=1, sticky=W)
Radiobutton(master, text="2", variable=var, value=2).grid(row=33, column=2, sticky=W)
Button(master, text="Options+", command=selection_window).grid(row=34, column=0, sticky=W, pady=10)
master.geometry("640x360")
#####################################################################
## Update window
#####################################################################
master.mainloop()
time.sleep(0.1)
### other method to update window(NOT WORKING)
# master.update_idletasks()
# master.update()