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

yipeiwu_com5年前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得到windows自启动列表的方法

如下所示: #!/usr/bin/python #-*-coding:utf-8-*- import _winreg as wr #导入内置的windows注册表操作库 import...

Python中operator模块的操作符使用示例总结

operator模块是python中内置的操作符函数接口,它定义了一些算术和比较内置操作的函数。operator模块是用c实现的,所以执行速度比python代码快。 逻辑操作 fr...

python中异常捕获方法详解

在Python中处理异常使用的是try-except代码块,try-except代码块放入让python执行的操作,同时告诉python程序如果发生了异常该怎么办,try-except这...

python开发之anaconda以及win7下安装gensim的方法

一、推荐安装Anaconda 官方介绍:Anaconda is a completely free Python distribution (including for commerci...

Python中使用Inotify监控文件实例

Inotify地址:访问 # -*- coding:utf-8 -*- import os import pyinotify from functions import * WA...