Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the height of a tkinter window title bar

Tags:

python

tkinter

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
like image 557
Daniel Huckson Avatar asked Sep 01 '19 00:09

Daniel Huckson


People also ask

How do you adjust the size of a Tk window?

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.

How do you title a Tkinter window?

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.

Which function's is are used to set the Tkinter window size?

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.

What is the right way to set the title of the window in Python?

The right way of adding title to the frame is by using Label Frame.


1 Answers

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: enter image description here The same: enter image description here

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: enter image description here enter image description here

Refer: MSDN doc:GetSystemMetrics,DPI awareness(value of DPI awareness).

like image 128
jizhihaoSAMA Avatar answered Sep 18 '22 20:09

jizhihaoSAMA