首页 > 编程 > Python > 正文

python 五子棋如何获得鼠标点击坐标

2019-11-25 11:29:18
字体:
来源:转载
供稿:网友

这篇文章主要介绍了python 五子棋如何获得鼠标点击坐标,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

点坐标的取自:

from tkinter import *root=Tk()#创建一个框架,在这个框架中响应事件frame=Frame(root,width=200,height=200)def callBackLeft(event):  print("相对于应用程序左上角的位置,左键点击的位置是",event.x,event.y)  print("相对于屏幕左上角的位置,左键点击的位置是",event.x_root,event.y_root)def callBackRight(event):  print("右键点击的位置是",event.x,event.y)  print("右键点击的位置是",event.x_root,event.y_root)frame.bind("<Button-1>",callBackLeft)frame.bind("<Button-3>",callBackRight)frame.pack()mainloop()

执行后 结果如图:

对坐标进行 处理和过滤得到 具体坐标

from tkinter import *root = Tk()size = 16def piant(event):  if event.x % 30 > 15:    event.x = event.x // 30 + 1  else:    event.x = event.x // 30  if event.y % 30 > 15:    event.y = event.y // 30 + 1  else:    event.y = event.y // 30  # 边缘检测  if event.x > size:    event.x = size  if event.y > size:    event.y = size  if event.x < 1:    event.x = 1  if event.y < 1:    event.y = 1  print("x坐标:%d,y坐标:%d"%(event.x,event.y))canvas = Canvas(root, width=500, height=500)canvas.pack(expand=YES, fill=BOTH)canvas.bind("<Button-1>",piant)canvas.pack()#画竖线for num in range(1, 17):  canvas.create_line(num * 30, 30,            num * 30, 480,            width=2)#画横线for num in range(1, 17):  canvas.create_line(30, num * 30,            480, num * 30,            width=2)root.mainloop()

执行后 结果如图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表