I am new to tkinter and I am trying to create a program that function as following:
- on the main window the user can select any option from the presented options using radio-buttons.
- after the selection the user clicks the submit button which will prompt the user with a new window that contains 2 radio buttons.
- if the user selects the 1st option an option menu will be shown for the user to choose from
- if the user selects the 2nd option the option menu will be hidden (if it's present)
- the user clicks the submit button and the program terminates.
Now point 4 is what I am struggling with. what I am getting is that if I have chose the first option and the option-menu got shown it does not get hidden when I choose the second option (Say for example I changed my mind and I want to proceed with the second option)
I have used the .grid_forget()
and .configure(state="disabled")
but the option menu is still there.
I have read the following posts:
- How to disable a tkinter OptionMenu
- How can I hide tkinter widgets based a radiobutton selection?
- Python Tkinter OptionMenu Complete Tutorial
and tried to adopt the information and answers in these posts and article but with no result. so, if any one could point to what I am missing and how to fix it, it will be much apricated.
EDITS: here is the minimal reproduceable code
import tkinter as tk
from tkinter import *
def new_window():
# creating and setting up the new window
window = Toplevel()
window.geometry('500x400')
window.resizable(0, 0)
# creating a frame to group the radio buttons
new_window_radio_btn_frame = LabelFrame(window, padx=5, pady=5)
new_window_radio_btn_frame.pack(padx=10, pady=10)
# creating radiobutton
new_window_radio_btn_option_lst = [('Append to existing data', 'append'), ('Override existing data', 'override')]
for j, (text, vlu) in enumerate(new_window_radio_btn_option_lst):
Radiobutton(new_window_radio_btn_frame, text=text, variable=radio_btn_selection, value=vlu,
font=('Times New Roman', 12),
command=lambda: add_drop_down(new_window_radio_btn_frame, radio_btn_selection.get()
)).grid(row=j + 2, column=0, sticky=tk.W, pady=20, padx=20)
# adding frame for the submit button
submit_btn_frame = LabelFrame(window, padx=5, pady=5)
submit_btn_frame.pack(padx=10, pady=10)
def add_drop_down(frame, choice):
# creating drop down menu options list
drop_down_lst_options = ['Discard Duplicate data', 'Keep Duplicate data']
drop_down_menu = OptionMenu(frame, menu_choice, *drop_down_lst_options)
# show or hide the drop-down list
if choice == 'append':
drop_down_menu.grid_forget()
drop_down_menu.grid(row=2, column=0, padx=200)
else:
drop_down_menu.grid_forget()
drop_down_menu.configure(state="disabled")
menu_choice.set('NA')
# create the main window
main = Tk()
main.title('Data Temp Generator')
main.withdraw()
main.geometry('500x400')
main.resizable(0, 0)
# creating variable to store the selection of the radio button of the secondary window
radio_btn_selection = StringVar()
radio_btn_selection.set('NA')
# variable to store the menu choice
menu_choice = StringVar()
menu_choice.set('Discard Duplicate data')
new_window()
main.mainloop()
and here is a couple of screenshots to illustrate what I want.
the user chose the 1st option so, as expected the option menu is now displayed.
now if the user changes his/her mind and chose the 2nd option this is what I get.
instead, what I want is for the option menu to be hidden like the following picture.