1
votes

I have a simple script that creates a window:

import Tkinter as tk

def center(win):
  win.update_idletasks()
  width = win.winfo_width()
  frm_width = win.winfo_rootx() - win.winfo_x()
  win_width = width + 2 * frm_width
  height = win.winfo_height()
  titlebar_height = win.winfo_rooty() - win.winfo_y()
  win_height = height + titlebar_height + frm_width
  x = win.winfo_screenwidth() // 2 - win_width // 2
  y = win.winfo_screenheight() // 2 - win_height // 2
  win.geometry('{}x{}+{}+{}'.format(width, height, x, y))

def showDialog():
  print "tkinter"
  root = tk.Tk()
  root.title("Say Hello")
  label = tk.Label(root, text="Hello World")
  label.pack(side="top", fill="both", expand=True, padx=20, pady=20)
  button = tk.Button(root, text="OK", command=lambda: root.destroy())
  button.pack(side="bottom", fill="none", expand=True, padx=10, pady=10)
  center(root)
  root.attributes("-topmost", True)
  root.mainloop()

showDialog()

When running this script, a first empty window is displayed on the top left part of the screen and then the complete window is displayed centered in the screen.

I would like to not see this first empty window (it appears only for few milliseconds, but this is not nice)

How can I do that ?

1
I'm trying your script and it doesn't seem to center the window at all, much less after some brief period. - TigerhawkT3
I'm not currently on a machine with Tkinter, so I can't play with your code, but you can hide a root or Toplevel window using the withdraw method, and use deiconify to re-display it. - PM 2Ring
I have already tried withdraw/deiconify but it just displays a kind of shadow instead of the window. I have no idea of what this shadow is. Note: I am running python on a MAC 10.6 - Laurent D.
how does it work without win.update_idletasks() ? - furas
@furas: removing win.update_idletasks() does not help (behavior not changed) - Laurent D.

1 Answers

1
votes

Use the following two methods to hide or show the root window.

def hide(root):
    root.withdraw()

def show(root):
    root.update()
    root.deiconify()

When you center the root window its size is (1, 1), you should give window size to center method.
lambda is not needed here, use command=root.destroy.

import Tkinter as tk

def center(win, width, height):
  win.update_idletasks()
  frm_width = win.winfo_rootx() - win.winfo_x()
  win_width = width + 2 * frm_width
  titlebar_height = win.winfo_rooty() - win.winfo_y()
  win_height = height + titlebar_height + frm_width
  x = win.winfo_screenwidth() // 2 - win_width // 2
  y = win.winfo_screenheight() // 2 - win_height // 2
  win.geometry('{}x{}+{}+{}'.format(width, height, x, y))

def show(root):
    root.update()
    root.deiconify()

def hide(root):
    root.withdraw()

def showDialog():
  print "tkinter"
  root = tk.Tk()
  hide(root)
  root.title("Say Hello")
  label = tk.Label(root, text="Hello World")
  label.pack(side="top", fill="both", expand=True, padx=20, pady=20)
  button = tk.Button(root, text="OK", command=root.destroy)
  button.pack(side="bottom", fill="none", expand=True, padx=10, pady=10)
  center(root, width=200, height=200)
  show(root)
  root.mainloop()

showDialog()