[Python] How to get a subwindow to open mid function to allow for user input in Python tkinter

Discussão em 'Python' iniciado por Stack, Julho 23, 2025.

  1. Stack

    Stack Membro Participativo

    An image of my GUI is shown below. Things are blacked out for work purposes but this will give you the gist of what it looks like.

    [​IMG]

    In each frame, the user makes some selections and then hits "Run". The general idea of the code is below

    import tkinter as tk
    class mainGUI:
    def __init__(self, parent, *args, **kwargs):
    self.parent = parent
    self.window_width = 1325
    self.window_height = 720
    self.frame1 = tk.Frame(...)
    self.frame1.grid(row=0, column=0)
    self.run_button = tk.Button(self.parent, text='Run', font=('Helvetica', 18), command=lambda: self.run_cmd(<inputs>)
    self.run_button.grid(row=1, column=0)

    def run_cmd(self, parent):
    <un-important code>
    case = 2
    if case == 2:
    userInput(self.parent)

    userSelection = userInput.val

    <rest of code>

    root = tk.Tk()
    mainGUI(root)
    root.mainloop()


    In the Run button command, when var "case" equals 2, it calls the class userInput. The general code shown below

    class userInput:
    var = ''
    def __init__(self, parent):
    self.subwindow = tk.Toplevel(parent)
    self.subwindow.transient(parent)
    self.subwindow.title('User Input')
    self.window_width = 360
    self.window_height = 325

    # listbox frame
    self.listbox_frame = tk.Frame(self.subwindow, width=340, height=160, bg='white')
    self.listbox_frame.grid(row=1, column=0)
    self.listbox_frame.grid_propagate(False)
    # button frame
    self.button_frame = tk.Frame(self.subwindow, width=70, height=25, bg='white')
    self.button_frame.grid(row=2, column=0, padx=100, pady=0)
    self.button_frame.grid_propagate(False)

    self.listbox_done_button = tk.Button(self.button_frame, text="Done", command=lambda: self.user_select_done(self.listbox_selections, self.subwindow))
    self.listbox_done_button.grid(row=0, column=1, padx=5)

    listbox = tk.Listbox(frame,selectmode = "browse", exportselection=tk.FALSE, relief="groove")

    self.content = ['Selection1','Selection2','Selection3']
    for item in range(len(self.content)):
    listbox.insert(tk.END, self.content[item])

    def user_select_done(self, listbox, subwindow):
    # print('file select')
    self.selectedItem = tf.getListboxSelects(listbox)
    if len(self.selectedItem) > 0:
    if self.selectedItem == 'Selection1':
    userInput.var = 'Selection1'
    elif self.selectedItem == 'Selection2':
    userInput.var = 'Selection2'
    elif self.selectedItem == 'Selection3':
    userInput.var = 'Selection3'
    subwindow.destroy()
    else:
    userInput.var = 'None'
    subwindow.destroy()
    print('No selection made')


    What I want to happen: The user hits Run --> the if statement gets satisfied and launches the subwindow --> the user makes their selection and hits the "Done" button --> the rest of the function code executes, the value of the user selection is ouput and used later in the function

    What is currently happening: The user hits Run --> the if statement is satisfied --> the rest of the code in the function executes --> subwindow pops up asking for user input

    Continue reading...

Compartilhe esta Página