0

I am new to tkinter and I am trying to create a program that function as following:

  1. on the main window the user can select any option from the presented options using radio-buttons.
  2. after the selection the user clicks the submit button which will prompt the user with a new window that contains 2 radio buttons.
  3. if the user selects the 1st option an option menu will be shown for the user to choose from
  4. if the user selects the 2nd option the option menu will be hidden (if it's present)
  5. 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:

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.

enter image description here

now if the user changes his/her mind and chose the 2nd option this is what I get. enter image description here

instead, what I want is for the option menu to be hidden like the following picture. enter image description here

Adel Moustafa
  • 150
  • 1
  • 9
  • 1
    Please try to replace the code with a [mcve]. If you're asking about how to hide or show an optionmenu based on a radiobutton, the example only needs the radiobuttons and the optionmenu, and enough code to make it run. We don't need all of the other code. All of that other code just clouds the issue. – Bryan Oakley Dec 27 '22 at 16:42
  • 1
    Ok, I will edit the code accordingly @BryanOakley – Adel Moustafa Dec 27 '22 at 17:02
  • You have three windows in one main window. The main window is blank. The second window is Toplevel at the start. And first option button is third Toplevel. – toyota Supra Dec 27 '22 at 19:34
  • I am sorry @toyotaSupra I did not understand your comment!, can you elaborate? – Adel Moustafa Dec 27 '22 at 20:29
  • 2
    It is because you create new drop down whenever `add_drop_down()` is executed and you just hide the new drop down, not the old one. – acw1668 Dec 28 '22 at 01:07

1 Answers1

0

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:

  1. create the option-menu inside the new window function and initially hide it.
  2. 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()
  
Adel Moustafa
  • 150
  • 1
  • 9