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实现log日志的示例代码

源代码: # coding=utf-8 import logging import os import time LEVELS={'debug':logging.DEBUG,\...

python递归函数绘制分形树的方法

python递归函数绘制分形树的方法

分形几何学的基本思想:客观事物具有自相似性的层次结构,局部和整体在形态,功能,信息,时间,空间等方面具有统计意义上的相似性,称为自相似性,自相似性是指局部是整体成比例缩小的性质。 我们先...

基于python plotly交互式图表大全

基于python plotly交互式图表大全

plotly可以制作交互式图表,直接上代码: import plotly.offline as py from plotly.graph_objs import Scatter, L...

Python 闭包的使用方法

Python 闭包的使用方法 嵌套函数中的非局部变量 在进入闭包之前,我们必须先了解一个嵌套函数和非局部变量。 在函数中定义另一个函数称为嵌套函数。嵌套函数可以访问包围范围内的变量。 在...

python实现控制电脑鼠标和键盘,登录QQ的方法示例

本文实例讲述了python实现控制电脑鼠标和键盘,登录QQ的方法。分享给大家供大家参考,具体如下: import os from pynput.mouse import Button...