TomSchimansky / CustomTkinter

A modern and customizable python UI-library based on Tkinter
MIT License
11.33k stars 1.07k forks source link

Invoking .geometry(), .winfo_height() or .winfo_width() on a window always returns 200 #2375

Closed Kamran496 closed 5 months ago

Kamran496 commented 5 months ago

Code:

import customtkinter

root = customtkinter.CTk()

print(f".geometry() -> {root.geometry()}")
print(f".width() -> {root.winfo_width()}")
print(f".height() -> {root.winfo_height()}\n")

width = 500
height = 500
root.geometry(f"{width}x{height}")

print(f".geometry() -> {root.geometry()}")
print(f".width() -> {root.winfo_width()}")
print(f".height() -> {root.winfo_height()}")

Output

.geometry() -> 200x200+208+208
.width() -> 200
.height() -> 200

.geometry() -> 200x200+208+208
.width() -> 200
.height() -> 200
Akascape commented 5 months ago

@Kamran496 Have you tried winfo_reqwidth() and winfo_reqheight()?

Kamran496 commented 5 months ago

@Kamran496 Have you tried winfo_reqwidth() and winfo_reqheight()?

@Akascape Same result, at least for me.

Akascape commented 5 months ago

@Kamran496 Remove this line:

print(f".geometry() -> {root.geometry()}")

Kamran496 commented 5 months ago

@Akascape

Still same result.

Code

import customtkinter

root = customtkinter.CTk()

print(f".width() -> {root.winfo_reqwidth()}")
print(f".height() -> {root.winfo_reqheight()}\n")

width = 500
height = 500
root.geometry(f"{width}x{height}")

print(f".width() -> {root.winfo_reqwidth()}")
print(f".height() -> {root.winfo_reqheight()}")

Output

.width() -> 200
.height() -> 200

.width() -> 200
.height() -> 200
Akascape commented 5 months ago

@Kamran496 Maybe you should call them after initialising the app. Like using .after method :

root.after(500, lambda: print(f".width() -> {root.winfo_width()}"))

Kamran496 commented 5 months ago

@Akascape

Thank you for the solution. Using .after as you've described above does indeed produce the correct output.

Another way to get correct output is .update() before calling .geometry(), .winfo_height() or .winfo_width().

See below Comment.

Kamran496 commented 5 months ago

Code

import customtkinter

root = customtkinter.CTk()

print(f".geometry() -> {root.geometry()}")
print(f".width() -> {root.winfo_reqwidth()}")
print(f".height() -> {root.winfo_reqheight()}\n")

width = 500
height = 500
root.geometry(f"{width}x{height}")
root.update()

print(f".geometry() -> {root.geometry()}")
print(f".width() -> {root.winfo_width()}")
print(f".height() -> {root.winfo_height()}")

Output

.geometry() -> 200x200+208+208
.width() -> 200
.height() -> 200

.geometry() -> 500x500+208+208
.width() -> 500
.height() -> 500