1

I'm creating a popup window in tkinter and want to hide it until it is created, labeled and centered on the screen to prevent the window to briefly flash empty at the wrong position.

But if i implemente these the resulting window acts if its contents were never distributed by pack() and the size of the window is set to a default 200 x 200 px.

I also tried after() and update_idletasks() since i guessed the issue in the sequence of drawing but with no success.

To hide it i use the withdraw() and deiconify() functions as stated in many other Posts including this one: Tkinter - Preload window?

My resulting code looks like this:

Frame creation

class NotificationPopup(Tk.Toplevel):
    def __init__(self, root, text, title):
        Tk.Toplevel.__init__(self, root)

        # Hide window until created
        self.withdraw()

        # Slaved to parent. Shown over parent window
        self.transient(root)

        # Stops interaction with parent until child is solved
        self.grab_set()

        self.label = Tk.Label(self, text=text, justify=Tk.LEFT, padx=10)
        self.label.pack()
        self.button = Tk.Button(self, text='Ok', command=self.destroy)
        self.button.pack()
        self.button.focus_set()

        self.resizable(False, False)
        self.relief = Tk.GROOVE

        # Bind return to the button to close the window
        self.button.bind('<Return>', (lambda event: self.destroy()))

        Toolbox.center_toplevel(self)

        # Show window
        self.deiconify()

Center method

# Center a window based on screen dimensions and window size
def center_toplevel(toplevel):
    toplevel.update_idletasks()

    # Toplevel window dimensions
    w = toplevel.winfo_width()
    h = toplevel.winfo_height()

    # get screen width and height
    ws = toplevel.winfo_screenwidth()  # width of screen
    hs = toplevel.winfo_screenheight()  # height of screen

    # calculate x and y coordinates for the Tk root window
    x = (ws / 2) - (w / 2)
    y = (hs / 2) - (h / 2)

    # Set dimension of window and placement on screen
    toplevel.geometry('%dx%d+%d+%d' % (w, h, x, y)) 

Running the code without withdraw() on my application Shows the width and heigt at 450 and 400 px while including withdraw() shrinks it to 200 x 200 px and don't refit it to it's content.

Kamakiri
  • 63
  • 2
  • 7
  • there is not enough here to work on your issue. What are your imports and where is your root window? – Mike - SMT Dec 21 '18 at 13:22
  • `Tk.Toplevel` should result in errors to begin with so your code should not even run. Please provide a [Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve). – Mike - SMT Dec 21 '18 at 13:38
  • The function is called out of another class. Sadly I'm not at work anymore so i can't edit in all the imports and a clearer example right now. Will provide it the earliest possible. But im importing `Tkinter as Tk`. – Kamakiri Dec 21 '18 at 15:07
  • You should import tkinter as `tk` in all lower case because tkinter contains a class called `Tk`. This will likely cause problems. – Mike - SMT Dec 21 '18 at 15:10

0 Answers0