[Python] How to show a dialog when using a spinbox in tkinter

Discussão em 'Python' iniciado por Stack, Dezembro 21, 2024.

  1. Stack

    Stack Membro Participativo

    I have a spinbox in my tkinter application and I want to show a warning in certain circumstances

    import tkinter as tk
    from tkinter import ttk, messagebox

    widgets = None
    root = None

    def main() -> None:
    global widgets
    global root

    root = tk.Tk()
    root.title('Spinbox')
    root.geometry('400x400')
    widgets = tk.IntVar()

    spin_box = ttk.Spinbox(
    root,
    from_=1,
    to=40,
    textvariable=widgets,
    command=_widgets_change,
    )
    spin_box.grid(row=0, column=0)

    root.mainloop()


    def _widgets_change() -> None:
    if widgets.get() > 2:
    root.update_idletasks()
    result = messagebox.showwarning(
    '',
    'Too big',
    parent=root
    )
    print(result)



    if __name__ == '__main__':
    main()


    This works if I increase the value in the entry, but if I use the up-down button I get into a loop with the error


    _tkinter.TclError: can't invoke "grab" command: application has been destroyed
    ​

    How can I get around this?

    (Linux - Manjaro)

    Continue reading...

Compartilhe esta Página