tkinter 窗口在没有窗口顶部的情况下获取 x、y、几何/坐标 [英] tkinter window get x, y, geometry/coordinates without top of window

查看:32
本文介绍了tkinter 窗口在没有窗口顶部的情况下获取 x、y、几何/坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 python (3) 中使用 tk,但我认为这适用于任何语言.我希望在标题栏之外获取 tk 窗口的当前 x、y 坐标:

I am using tk in python (3), though I'd assume that this applies to any language. I am looking to get the current x, y coordinates of a tk window outside of the title bar:

import tkinter  
root = tkinter.Tk()

然而,使用 root.winfo_y() 给了我包括标题栏深度在内的坐标.对于位于屏幕左上角的窗口:

however, using root.winfo_y() gives me the coordinates including the depth of the titlebar. For a window that is in the upper left corner of my screen:

root.winfo_x(), root.winfo_y()          # returns (0, 22)

换句话说,运行:

root.geometry('+{}+{}'.format(root.winfo_x(), root.winfo_y()))

每次调用时都会将窗口向下移动 22 个像素(标题栏的高度).如何获取整个窗口的实际坐标?

will move down the window by 22 pixels (the height of the title bar) every time I call it. How can I get the actual coordinates of the entire window?

推荐答案

在 Tkinter 中,大多数配置函数在没有新值参数的情况下使用时返回当前值.因此 root.geometry()(加上一些字符串解析)可以满足您的目标.

In Tkinter, most configuration functions when used without new value arguments returns the current value(s). Thus root.geometry() (plus a few string parsing) can serve your goal.

这篇关于tkinter 窗口在没有窗口顶部的情况下获取 x、y、几何/坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆