I'm trying to figure out how to get the height of a tkInter window title bar but can't seem to find any info on how it's done.
I have tried using root.geometry()
and it seems that root.geometry()
only returns the size of the window content and not the total size of the window with title bar and border sizes. I have seen other people say that you need to ask the OS for those things. I was hoping to avoid this because it will make it harder to make the code platform independent. There must be a way to do this without going to the OS for this. Anyone know what it is I must do to get this info?
My system:
OS: Linux
KDE Plasma: 5.16.4
KDE Frameworks: 5.61.0
import tkinter
root = tkinter.Tk()
root.geometry("250x250+100+100")
root.update_idletasks()
print('root.winfo_x() = ', root.winfo_x())
print('root.winfo_y() = ', root.winfo_y())
print('root.geometry() = ', root.geometry())
root.mainloop()
Test code results:
root.winfo_x() = 100
root.winfo_y() = 100
root.geometry() = 250x250+100+100
The height of the window when measured with a screen ruler app is:
x=102, y=286
Syntax – geometry() To set a specific size to the window when using Python tkinter, use geometry() function on the Tk() class variable. where width and height should be replaced with integers that represent the width and height of the window respectively.
The title in tkinter refers to a name assigned to the application window. It is mostly found on the top of the application. For this, we can use the title() function. We create a widget object using the Tk() function and use the title() function to add a title to this window.
In Tkinter, minsize() method is used to set the minimum size of the Tkinter window. Using this method user can set window's initialized size to its minimum size, and still be able to maximize and scale the window larger. Here, height and width are in pixels.
The right way of adding title to the frame is by using Label Frame.
Title bar(default) is a system setting.As far as I know,it depends on many factors.(System zoom ratio,different OS,DPI awareness and so on).
In windows,change the zoom ratio will get different value of height.
About the question: tkinter will be recognized a old software in windows,you need to set DPI awareness to make it have the system normal height(fit the system zoom ratio if your system zoom ratio is not 100%).
Normally,the height of system title bar are same:but some exception(I am not really know about winapi
),different DPI awareness will show you the different height of title bar:
The same:
To make them same and get the normal height of title bar:
import tkinter
import ctypes
ctypes.windll.shcore.SetProcessDpiAwareness(2)
print(ctypes.windll.user32.GetSystemMetrics(4))
root = tkinter.Tk()
root.mainloop()
Result:
Refer: MSDN doc:GetSystemMetrics,DPI awareness(value of DPI awareness).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With