'How can I make radio buttons add and remove entry boxes without errors? Python - Tkinter
I am very new to Python and Tkinter so sorry if the code and variable names are all over the place
What I am trying to do is make it so on radio button selection it adds/removes entry boxes. So if I select radio button 1 for example, I want it to add its associated entry boxes and remove any that would be displayed from the other radio buttons. While the following code does work, it also produces the following errors:
"if cube_dime.cube_dime_tbox.winfo_exists() and present_select.get() != "cube": AttributeError: 'function' object has no attribute 'cube_dime_tbox' Exception in Tkinter callback"
Basically, what I am asking is how can I make it so the system either ignores the errors or if there is any way to correct them in my case.
from tkinter import *
main_window = Tk()
main_window.title("Wrap It!")
present_select = StringVar()
present_select.set(0)
def cube_dime():
if present_select.get() == "cube":
cube_dime.cube_dime_tbox = Entry(main_window, width=10, text="width")
cube_dime.cube_dime_tbox.grid(column=1,row=3, columnspan=2)
if cube_dime.cube_dime_tbox.winfo_exists() and present_select.get() != "cube":
cube_dime.cube_dime_tbox.destroy()
if present_select.get() == "cuboid":
cube_dime.cuboid_dime_tbox = Entry(main_window, width=10, text="width")
cube_dime.cuboid_dime_tbox.grid(column=3, row=3, columnspan=2)
if cube_dime.cuboid_dime_tbox.winfo_exists() and present_select.get() != "cuboid":
cube_dime.cuboid_dime_tbox.destroy()
if present_select.get() == "cylinder":
cube_dime.cylinder_dime_tbox = Entry(main_window, width=10, text="width")
cube_dime.cylinder_dime_tbox.grid(column=5, row=3, columnspan=2)
if cube_dime.cylinder_dime_tbox.winfo_exists() and present_select.get() != "cylinder":
cube_dime.cylinder_dime_tbox.destroy()
present_radio1 = Radiobutton(main_window,text="Cube", variable=present_select, value="cube", font = ("Ariel", 15), command=cube_dime)
present_radio1.grid(column=1, row=0, ipadx=5, sticky="w", columnspan=2, pady=10)
present_radio2 = Radiobutton(main_window,text="Cuboid", variable=present_select, value="cuboid", font = ("Ariel", 15), command=cube_dime)
present_radio2.grid(column=3, row=0, columnspan=2)
present_radio3 = Radiobutton(main_window,text="Cylinder", variable=present_select, value="cylinder", font = ("Ariel", 15), command=cube_dime)
present_radio3.grid(column=5, row=0, ipadx=15, sticky="e", columnspan=2)
main_window.mainloop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
