The tkinter messagebox appears first and then the main dialog does not complete successfully.

Asked 2 years ago, Updated 2 years ago, 451 views

This is a self-resolved content, but I will post it for sharing.

Before starting the main GUI application, you may first check the startup conditions, such as checking configuration files, and use the messagebox to alert you.

At that time, I experienced a phenomenon in which characters could not be displayed properly in the main dialog and could not be finished successfully without getting out of mainloop.


import tkinter as tk
from tkinter import messagebox


def_message_box_(title:str, message:str) - > None:
    # root to be the parent of the messagebox
    root=tk.Tk()
    # Move to the front
    root.attributes('-topmost', True)
    # Hide Dialogs
    # Hide the dialog because only the messagebox is displayed to the user.
    root.withdraw()
    # Move to the top
    root.lift()
    # focus set
    root.focus_force()

    messagebox.showinfo(title, message)

    return


class act_dialog():
    def__init__(self):
        dialog = tk.Tk()
        dialog.title('main application')
        dialog.resizable(0,0)
        dialog.lift()
        dialog.focus_force()

        message=tk.StringVar()
        message.set('Hello!!!')

        label=tk.Label(dialog, textvariable=message)
        label.pack()

        dialog.mainloop()

        print('finish!!)')

        return


_message_box_('information',"Let's start!!")
act_dialog()

python tkinter

2022-11-22 09:02

1 Answers

In order for the messagebox to appear independently, a dialog with root is required.
However, if the root dialog is displayed every time the messagebox is displayed, it will not look good, so I will hide it with draw().
Even if the messagebox, the child of root, is closed, root is still alive behind the scenes.
Because it is hidden, it is difficult to recognize that the root is alive.

The main dialog (act_dialog) cannot display characters because of the rule that the first generation of the TK instance takes precedence.
The characters in StringVar() are assigned to the root generated for the messagebox, and the main dialog does not display the characters.

Conversely, if this happens, another TK instance will be quietly alive somewhere.

If you want to use two or more dialogs separately, use the method with the master argument.

message=tk.StringVar(master=dialog)

You must specify master in this way.

This issue occurred because we did not discard the root for the messagebox.
Let's understand and do destroy().

If you only use the messagebox, the root will be automatically discarded at the end of the script, so it will be difficult to notice.


import tkinter as tk
from tkinter import messagebox


def_message_box_(title:str, message:str) - > None:
    # root to be the parent of the messagebox
    root=tk.Tk()
    # Move to the front
    root.attributes('-topmost', True)
    # Hide Dialogs
    # Hide the dialog because only the messagebox is displayed to the user.
    root.withdraw()
    # Move to the top
    root.lift()
    # focus set
    root.focus_force()

    messagebox.showinfo(title, message)

    # discarding
    root.destroy()

    return


class act_dialog():
    def__init__(self):
        dialog = tk.Tk()
        dialog.title('main application')
        dialog.resizable(0,0)
        dialog.lift()
        dialog.focus_force()

        message=tk.StringVar(master=dialog)
        message.set('Hello!!!')

        label=tk.Label(dialog, textvariable=message)
        label.pack()

        dialog.mainloop()

        print('finish!!)')

        return


_message_box_('information',"Let's start!!")
act_dialog()


2022-11-22 09:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.