0

I want to make a widget appear in the center of the screen without using grid or pack.

Here is some example code:

import tkinter
Win=tkinter.Tk()
Welcome=tkinter.Label(Win,text="Welcome.")
Welcome.place(x=Win.winfo_width()//2-Welcome.winfo_width()//2,y=16)
Win.update()

When I ran the code, the widget appeared on the far left of the screen instead of the center. I was told about reqwidth and update_idletasks, which fixed the code, but I was wondering if someone could explain why the code doesn't work without reqwidth and update_idletasks, as I like to fully understand programming concepts.

Programmer S
  • 429
  • 7
  • 21
  • 2
    Possible duplicate of [winfo\_width() returns 1 even after using pack()](https://stackoverflow.com/questions/34373533/winfo-width-returns-1-even-after-using-pack) – fhdrsdg May 08 '18 at 14:34
  • 1
    Aslo see: https://stackoverflow.com/questions/4220295/get-tkinter-window-size/4221002 – fhdrsdg May 08 '18 at 14:43
  • 1
    Did you try winfo_reqheight() and winfo_reqwidth() ? – progmatico May 08 '18 at 20:51
  • @progmatico I have now, but how are they different to `winfo_height()` and `winfo_width()?` – Programmer S May 10 '18 at 10:28
  • 2
    Suppose you configure a window size. If the window is not shown yet in the screen, or tkinter hasn't finished all its UI update tasks, and you ask for the window size, it may not reflect the size you configured, because the window or widget is not yet at its requested size. The req methods will give you the requested values for the widget size, no matter what the real size was at the moment. – progmatico May 10 '18 at 15:27
  • The req methods may not give you the actual size you configured, but the size the geometry manager computed as needed for the window, based on its contained child widgets. But the normal methods will give you the real size. – progmatico May 10 '18 at 15:59

1 Answers1

0
import tkinter
Win=tkinter.Tk()
Welcome=tkinter.Label(Win,text="Welcome.")
Welcome.place(x=Win.winfo_width()//2-Welcome.winfo_reqwidth()//2,y=16)
Win.update()
Welcome.place(x=Win.winfo_width()//2-Welcome.winfo_reqwidth()//2,y=16)
print(str(Win.winfo_width()//2)+"-"+str(Welcome.winfo_reqwidth()//2)+"="+str(Win.winfo_width()//2-Welcome.winfo_reqwidth()//2))
Win.bind("<Configure>",func=lambda x:Welcome.place(x=Win.winfo_width()//2-Welcome.winfo_reqwidth()//2,y=16))

and

import tkinter
Win=tkinter.Tk()
Welcome=tkinter.Label(Win,text="Welcome.")
Win.update_idletasks()
Welcome.place(x=Win.winfo_width()//2-Welcome.winfo_width()//2,y=16)
Win.update()
Welcome.place(x=Win.winfo_width()//2-Welcome.winfo_width()//2,y=16)
print(str(Win.winfo_width()//2)+"-"+str(Welcome.winfo_width()//2)+"="+str(Win.winfo_width()//2-Welcome.winfo_width()//2))
Win.bind("<Configure>",func=lambda x:Welcome.place(x=Win.winfo_width()//2-Welcome.winfo_width()//2,y=16))

Seem to work just fine, however it would be helpful to know the actual difference between winfo_width and winfo_reqwidth.

Programmer S
  • 429
  • 7
  • 21