How to get the widget's current x and y coordinates?

前端 未结 3 599
感情败类
感情败类 2020-12-21 00:17

I\'m currently writing a game project as for the game \"4 in a row\". To make the animation where the picture widget disk falls in the column, I\'ve been thinking about crea

相关标签:
3条回答
  • 2020-12-21 00:22

    Yes there is, it is a method of a widget named coords,

    coords(tagOrId, x0, y0, x1, y1, ..., xn, yn) Queries or modifies the coordinates that define an item. If no coordinates are specified, this method returns a tuple whose elements are the coordinates of the item named by tagOrId. If coordinates are specified, then they replace the current coordinates for the named item. If tagOrIdrefers to multiple items, then the first one in the display list is used - From "Python and Tkinter Programming" by John E. Grayson

    Example:

    root = Tk()
    calibCanvas = Canvas(root, width=800,height=800,bg='WHITE')     
    canvasRect = calibCanvas.create_rectangle(400,400,410,410, outline='BLUE')  
    print calibCanvas.coords(canvasRect)
    
    0 讨论(0)
  • 2020-12-21 00:26

    You can try winfo_rootx() and winfo_rooty().

    From effbot.com:

    winfo_rootx()

    Get the pixel coordinate for the widget’s left edge, relative to the screen’s upper left corner.

    Returns: The root coordinate.

    winfo_rooty()

    Get the pixel coordinates for the widget’s upper edge, relative to the screen’s upper left corner.

    Returns: The root coordinate.

    An example from nullege.com:

    # get self position & height
    lv_x = self.winfo_rootx()
    lv_y = self.winfo_rooty()
    
    0 讨论(0)
  • 2020-12-21 00:44

    winfo_rootx() and winfo_rooty() return the coordinates relative to the screen's upper left corner. winfo_x and winfo_y return the coordinates of a window relative to its parent.

    0 讨论(0)
提交回复
热议问题