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

yipeiwu_com7年前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字典代码实例

这篇文章主要介绍了python 哈希表实现简单python字典代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 class...

python创建关联数组(字典)的方法

本文实例讲述了python创建关联数组(字典)的方法。分享给大家供大家参考。具体分析如下: 关联数组在python中叫字典,非常有用,下面是定义字典的两种方法 # Dictionar...

python opencv如何实现图片绘制

python opencv如何实现图片绘制

这篇文章主要介绍了python opencv如何实现图片绘制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 点和圆 : circle...

python统计日志ip访问数的方法

本文实例讲述了python统计日志ip访问数的方法。分享给大家供大家参考。具体如下: import re f=open("/tmp/a.log","r") arr={} lines...

使用httplib模块来制作Python下HTTP客户端的方法

使用httplib模块来制作Python下HTTP客户端的方法

httplib 是 python中http 协议的客户端实现,可以使用该模块来与 HTTP 服务器进行交互。httplib的内容不是很多,也比较简单。以下是一个非常简单的例子,使用htt...