原生海龟画图实时获取鼠标指针

原生海龟画图实时获取鼠标指针

"""
   原生海龟画图实时获取鼠标指针
"""

import turtle
import tkinter as tk
root = tk.Tk()
root.title("原生海龟画图")
cv = tk.Canvas(root, width=500,height=500)
cv.focus_set()
cv.pack(side = tk.LEFT)
pen = turtle.RawTurtle(cv)   # 在画布上新建原生海龟
pen.pencolor("red")
window = pen.getscreen()     # 画笔所在屏幕窗口
window.delay(0)

def get_mouse_pos(event):
   """获取鼠标指针"""
   cv.unbind("")
   x = root.winfo_pointerx()-root.winfo_rootx()
   y = root.winfo_pointery()-root.winfo_rooty()
   root.title(str(x) + "," + str(y))
   pen.goto(x,y)
   cv.bind("", get_mouse_pos)
       
def main():
    """主要执行函数"""
    window.setworldcoordinates(0,500,500,0)
    window.bgcolor("white")
    frame = tk.Frame(root)
    frame.pack(side = tk.RIGHT,fill=tk.BOTH)
    pointLabel = tk.Label(frame,text="这里是宽度")
    pointLabel.pack()
    cv.bind("", get_mouse_pos)
    cv.pack
    tk.mainloop()
    
if __name__ == "__main__":
  main()
  

李兴球

李兴球的博客是Python创意编程原创博客

评论已关闭。