【问题标题】:How do you specify or read the position of the main app window with guizero?如何使用 guizero 指定或读取主应用程序窗口的位置?
【发布时间】:2019-09-26 17:18:47
【问题描述】:

我需要在 guizero 中指定或读取主应用程序窗口的位置(左上角的 x 和 y 坐标)。 显然 App 对象没有我可以设置或读取的 x 和 y 位置属性。 我在这个论坛上找到了 TkInter 但不是 guizero 的解决方案。

我的目标实际上是能够告诉鼠标在应用程序窗口中的位置。我可以通过事件数据 display_xdisplay_y 告诉鼠标相对于整个屏幕的位置,如果我移动窗口,这些会发生变化,但如果我可以使用这些值可以解释窗口在屏幕上的位置,那么我该如何找出呢?

如果窗口的特定区域被诸如文本或一个按钮。

我使用的是 Windows 10 和 Python 3

event_data.display_x = 鼠标在整个显示器上的 x 位置

event_data.x = 鼠标在小部件上的 x 位置。 如果该位置被小部件“覆盖”,则无法用于应用窗口

【问题讨论】:

标签: python user-interface position window


【解决方案1】:

我找到了答案!

显然,您可以直接在 guizero 对象上使用 tkinter 方法和属性。 碰巧,geometry 属性返回一个包含窗口坐标的字符串。

这是一个例子:

from guizero import App, PushButton

#___________________________________
def appos(event_data):
    """ Print the coordinates of the Window position,
        of the mouse position on the screen
        and of the mouse position within the window"""

    # Absolute mouse position on display
    mouse_x, mouse_y = (event_data.display_x), (event_data.display_y)

    # tk.geometry() returns the size and coordinates of a window in a string
    wincord = (app.tk.geometry()).split("+")[1:] # Discards size 

    # Coordinates of (top left corner) of window
    wincord_x, wincord_y = int(wincord[0]), int (wincord[1])

    # Relative mouse position within the window
    mouse_rel_x = mouse_x - wincord_x
    mouse_rel_y = mouse_y - wincord_y

    print ("Window position on display: ", wincord_x, wincord_y)
    print ("Mouse position on entire display: ", mouse_x, mouse_y)
    print ("Mouse position in window: ", mouse_rel_x, mouse_rel_y)
    print()
#_________________________________

app = App(layout="grid")

app.when_clicked = appos # Call the function that prints the positions

# Creates a list of lists (kinda a 2-dimension array) with 
# placeholders to be filled with buttons
bt = [[0,1,2,3,4,5,6,7],
[0,1,2,3,4,5,6,7],
[0,1,2,3,4,5,6,7],
[0,1,2,3,4,5,6,7],
[0,1,2,3,4,5,6,7],
[0,1,2,3,4,5,6,7],
[0,1,2,3,4,5,6,7],
[0,1,2,3,4,5,6,7]]

# creates an array of 8x8 buttons and arrange them on the window
for x in range (8):
    for y in range (8):
        bt[x][y] = PushButton(app, text= str(x)+str(y), grid= [x, y])
        # bt[x][y].when_clicked = clicked # For future use

app.display()

如您所见,通过单击窗口上的任意位置,即使窗口被按钮覆盖,您也会在窗口中获得正确的鼠标位置!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多