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:
-
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.
CodePudding user response:
Thanks to the comment of @acw1668 I was able to solve my problem. what I was missing is that previously I created a new option-menu whenever
add_drop_down()
is called (as suggested by @acw1668). the edits that I made is:- create the option-menu inside the new window function and initially hide it.
- make the functionality of the
add_drop_down()
function show/hide option menu instead of "adding and then hide a new option menu"
here is the edited 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 drop down menu options list drop_down_lst_options = ['Discard Duplicate data', 'Keep Duplicate data'] drop_down_menu = OptionMenu(new_window_radio_btn_frame, menu_choice, *drop_down_lst_options) # 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(drop_down_menu, 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(drop_down, choice): # show or hide the drop-down list if choice == 'append': drop_down.grid(row=2, column=0, padx=200) else: drop_down.grid_forget() 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()