窗口标题栏的高度

窗口标题栏的高度

本文介绍了如何获取 tkinter 窗口标题栏的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何获得 tkInter 窗口标题栏的高度,但似乎找不到任何关于它是如何完成的信息.

我尝试使用 root.geometry() 并且似乎 root.geometry() 只返回窗口内容的大小而不是窗口的总大小带有标题栏和边框大小的窗口.我看到其他人说你需要向操作系统询问这些事情.我希望避免这种情况,因为它会使代码平台独立变得更加困难.必须有一种方法可以做到这一点,而无需为此前往操作系统.有谁知道我必须做什么才能获得这些信息?

我的系统:

操作系统:Linux

KDE Plasma: 5.16.4

KDE 框架: 5.61.0

导入 tkinter根 = tkinter.Tk()root.geometry("250x250+100+100")root.update_idletasks()打印('root.winfo_x() = ', root.winfo_x())打印('root.winfo_y() = ', root.winfo_y())打印('root.geometry() = ', root.geometry())root.mainloop()

测试代码结果:

 root.winfo_x() = 100root.wininfo_y() = 100root.geometry() = 250x250+100+100

使用屏幕标尺应用测量时窗口的高度为:

x=102, y=286
解决方案

我终于想通了.为了获得标题栏的高度,我首先向窗口添加了一个框架并设置了窗口几何形状('1x1').我还必须使用 update_idletasks() 来更新 tkinter 内部数据.这样做后,我得到了正确的高度.不得不这样做似乎很奇怪,但它奏效了.下面是我用来获取高度的代码.在 Windows 和 Linux 中测试和工作.如果有人知道更正确的方法来做到这一点,我肯定想知道.另外,如果有人使用苹果,请告诉我下面的代码是否有效.

更新:我已经更新了代码,并且已经在(Linux、Windows 和 MacOS)中进行了测试,以提供正确的标题栏高度.我认为这是最好的方法,因为您不需要进行依赖于操作系统的系统调用.但我不知道它是否适用于任何缩放级别.

更新:现在适用于任何缩放级别.(测试在 Windows 10、Windows 7 中通过)

导入 tkinter 作为 tk从系统导入平台类应用程序(tk.Tk):def __init__(self):super().__init__()tk.Frame(self).update_idletasks()self.geometry('350x200+100+100')self.update_idletasks()偏移_y = 0如果平台在 ('win32', 'darwin'):导入 ctypes尝试:# >= 赢得 8.1ctypes.windll.shcore.SetProcessDpiAwareness(2)除了:# win 8.0 或更低ctypes.windll.user32.SetProcessDPIAware()offset_y = int(self.geometry().rsplit('+', 1)[-1])bar_height = self.winfo_rooty() - offset_y打印(f'高度:{bar_height}\n平台:{平台}')# self.destroy()定义主():应用程序 = 应用程序()app.mainloop()如果 __name__ == '__main__':主要的()

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
解决方案

I have figured this out finally. In order to get the height of the title bar I first added a frame to the window and set the window geometry('1x1'). I also had to use update_idletasks() to update tkinter internal data. After doing this I got the correct height. Seems strange to have to do it this way but it worked. Below is the code I used to get the height. Tested and works in Windows and Linux. If anyone knows of a more correct way to do this I would sure like to know. Also if anyone is using apple please let me know if the code below works.

UPDATE: I have updated the code and it's been tested in (Linux, Windows and MacOS) to give the correct title bar height. I think this is the best way to do this because you don't need to make OS dependent system calls.But I don't know whether it could work for any zoom level.


Update:now could work for any zoom level.(Test passed in windows 10,windows 7)

import tkinter as tk
from sys import platform


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        tk.Frame(self).update_idletasks()
        self.geometry('350x200+100+100')
        self.update_idletasks()

        offset_y = 0
        if platform in ('win32', 'darwin'):
            import ctypes
            try: # >= win 8.1
                ctypes.windll.shcore.SetProcessDpiAwareness(2)
            except: # win 8.0 or less
                ctypes.windll.user32.SetProcessDPIAware()
            offset_y = int(self.geometry().rsplit('+', 1)[-1])

        bar_height = self.winfo_rooty() - offset_y
        print(f'Height: {bar_height}\nPlatform: {platform}')
        #  self.destroy()


def main():
    app = App()
    app.mainloop()


if __name__ == '__main__':
    main()

这篇关于如何获取 tkinter 窗口标题栏的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 13:10