简体   繁体   English

Python / tkinter - 如何在Windows上获得包含边框的窗口大小?

[英]Python/tkinter - How do I get the window size including borders on Windows?

I'm trying to position my window based on with width and height of the window. 我正试图根据窗口的宽度和高度来定位窗口。 On Windows, the window size reported by wm_geometry , winfo_width and winfo_height is the size of the client area , ie the window size without the borders. 在Windows上, wm_geometrywinfo_widthwinfo_height报告的窗口大小是客户区的大小,即没有边框的窗口大小。 The position of the window, as reported by wm_geometry , winfo_x and winfo_y , and the position set using wm_geometry , is the position of the top left point of the window including the border. wm_geometrywinfo_xwinfo_y报告的窗口位置以及使用wm_geometry设置的位置是窗口左上角的位置, 包括边框。

This means that when I try to center the window on the screen, the position is visibly too low on the screen. 这意味着当我尝试将窗口置于屏幕中央时,屏幕上的位置明显太低。

I don't want to hard-code the border thickness since it can vary. 我不想硬编码边框厚度,因为它可以变化。

Is it possible to get or infer the size of the window border on Windows using Python/tkinter? 是否可以使用Python / tkinter在Windows上获取或推断窗口边框的大小?

From http://wiki.tcl.tk/11291 : 来自http://wiki.tcl.tk/11291

wm geometry . returns contentsWidthxcontentsHeight+decorationTop+decorationLeftEdge. 返回contentsWidthxcontentsHeight + decorationTop + decorationLeftEdge。

and winfo rooty . winfo rooty . returns contentsTop and winfo rootx . 返回contentsTop和winfo rootx . returns contentsLeftEdge 返回contentsLeftEdge

From these you can calculate the title bar height and left border width (which generally matches the right and bottom border width). 从中可以计算标题栏高度和左边框宽度(通常与右边框宽度和下边框宽度匹配)。 This should work on windows, but will not necessarily work on all platforms. 这应该适用于Windows,但不一定适用于所有平台。 As the linked page also examines, there are also issues in determining the total height and width of the screen area available due to the windows taskbar. 由于链接页面也会检查,因此在确定由于Windows任务栏而可用的屏幕区域的总高度和宽度时也存在问题。

As we discussed in the comments, I doubt you can do it using pure tkinter. 正如我们在评论中所讨论的那样,我怀疑你是否可以使用纯tkinter来做到这一点。

I've tried to implement a solution for getting the window size including the border using win32gui. 我试图使用win32gui实现一个获取窗口大小的解决方案,包括边框。 From this information you should be able to implement the rest of the functionality. 根据这些信息,您应该能够实现其余功能。 Unfortunatly this is only for Windows platforms and requires an installation on pywin32. 不幸的是,这仅适用于Windows平台,需要在pywin32上安装。 You can get it here . 你可以在这里得到它。 I'm sure there's a Mac and Linux equivalent if that is a requirement. 如果这是一个要求,我肯定有一个Mac和Linux等价物。

This is my solution, tested in Python 3.4: 这是我在Python 3.4中测试的解决方案:

import tkinter as tk
import win32gui

class TestApp:
    def __init__(self, master):
        frame = tk.Frame(master)
        frame.pack()

        self.master = master

        label = tk.Label(text='Test Label')
        label.pack()
        b1 = tk.Button(text='test', command=self.pressed)
        b1.pack()

    def pressed(self):
        win32gui.EnumWindows(self.callback, None)
        x = root.winfo_x()
        y = root.winfo_y()
        w = self.master.winfo_width()
        h = self.master.winfo_height()
        print('tkinter location: ({},{})'.format(x, y))
        print('tkinter size: ({},{})'.format(w, h))


    def callback(self, hwnd, extra):
        if "Test title" in win32gui.GetWindowText(hwnd):
            rect = win32gui.GetWindowRect(hwnd)
            x = rect[0]
            y = rect[1]
            w = rect[2] - x
            h = rect[3] - y
            print('Window location: ({},{})'.format(x, y))
            print('Window size: ({},{})'.format(w, h))

root = tk.Tk()
root.title("Test title")
app = TestApp(root)
root.mainloop()

I'm win32gui.EnumWindows() to interate over all available windows to locate the one with the correct title. 我是win32gui.EnumWindows()来对所有可用的窗口进行交互以找到具有正确标题的窗口。 From this I can then get the size using win32gui.GetWindowRect(). 然后,我可以使用win32gui.GetWindowRect()获取大小。

I'll print the results from win32gui and tkinter to the console for comparison. 我将把win32gui和tkinter的结果打印到控制台进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM