欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

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

时间:2021-04-23 09:30:57|栏目:Python代码|点击:

这篇文章主要介绍了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 = 16

def 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()

执行后 结果如图:

上一篇:Python语言的面相对象编程方式初步学习

栏    目:Python代码

下一篇:Python:type、object、class与内置类型实例

本文标题:python 五子棋如何获得鼠标点击坐标

本文地址:http://www.codeinn.net/misctech/107389.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有