python - How to center a window on the screen in Tkinter? -


I am trying to focus a typewriter window. I know that I can get the window size and the size of the screen in terms of program and I can use it to set that geometry, but I am thinking that an easy way to center the window on the screen is.

You can use the winfo_screenwidth and winfo_screenheight You can try to use, which come back respectively your Tk the width and height of the example (window) and in pixels (in pixels), and with some basic math you can focus your window:

  TK Diff Center (Toplevel) as imported connector: toplevel.update_idletasks () w = toplevel.winfo_screenwidth () h = toplevel.winfo_screenheight () size = tuple (int (_) toplevel .geometry (for)) Split ('+ ') [0]. Partition ('x') x = w / 2 - size [0] / 2 y = h / 2 - size [1] / 2 toplevel.geometry ("% dx% d +% d +% d"% (size) + (X, y))) If __name__ == '__main__': root = tk.Tk () root.title ("not centered") win = tk.Toplevel (root) win.title ("centered!") (Win) root.mainloop ()  

I agree that before returning the values ​​to the width and the height of the window, before I get to the update_idletasks method The call is accurate


Comments

Popular posts from this blog

paypal - How to know the URL referrer in PHP? -

oauth - Facebook OAuth2 Logout does not remove fb_ cookie -

wpf - Line breaks and indenting for the XAML of a saved FlowDocument? -