我找到了答案!
显然,您可以直接在 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()
如您所见,通过单击窗口上的任意位置,即使窗口被按钮覆盖,您也会在窗口中获得正确的鼠标位置!