Python 如何使网格中的瓷砖在按下时改变颜色?

Python 如何使网格中的瓷砖在按下时改变颜色?,python,tkinter,Python,Tkinter,所以我能够使用Tkinter在python中创建一个网格,但我不知道如何使每个单元格在按下时改变颜色 守则: import tkinter as tk def create_grid(event=None): w = c.winfo_width() # Get current width of canvas h = c.winfo_height() # Get current height of canvas c.delete('grid_line') # Will o

所以我能够使用Tkinter在python中创建一个网格,但我不知道如何使每个单元格在按下时改变颜色 守则:

import tkinter as tk

def create_grid(event=None):
    w = c.winfo_width() # Get current width of canvas
    h = c.winfo_height() # Get current height of canvas
    c.delete('grid_line') # Will only remove the grid_line

    # Creates all vertical lines at intevals of 100
    for i in range(0, w, 100):
        c.create_line([(i, 0), (i, h)], tag='grid_line')

    # Creates all horizontal lines at intevals of 100
    for i in range(0, h, 100):
        c.create_line([(0, i), (w, i)], tag='grid_line')

root = tk.Tk()

c = tk.Canvas(root, height=1000, width=1000, bg='white')
c.pack(fill=tk.BOTH, expand=True)

c.bind('<Configure>', create_grid)

root.mainloop()
将tkinter作为tk导入
def创建网格(事件=无):
w=c.winfo_width()#获取画布的当前宽度
h=c.winfo_height()#获取画布的当前高度
c、 删除(“网格线”)只会删除网格线
#以100为单位创建所有垂直线
对于范围(0,w,100)内的i:
c、 创建网格线([(i,0),(i,h)],tag='grid\u line')
#以100为单位创建所有水平线
对于范围(0,h,100)内的i:
c、 创建网格线([(0,i),(w,i)],tag='grid\u line')
root=tk.tk()
c=tk.Canvas(根,高度=1000,宽度=1000,bg='white')
c、 包装(填充=tk.BOTH,展开=True)
c、 绑定(“”,创建网格)
root.mainloop()

我不知道你所说的“按下时”是什么意思,所以我假设你的意思是“鼠标按下瓷砖时”

第一步是以更易于复制的格式放置行:

lineNumber = 10 # <-- CHANGED

def create_grid(event=None):
    w = c.winfo_width() # Get current width of canvas
    h = c.winfo_height() # Get current height of canvas
    c.delete('grid_line') # Will only remove the grid_line

    for i in range(0, w, w//lineNumber): # <-- CHANGED
        c.create_line([(i, 0), (i, h)], tag='grid_line')

    for i in range(0, h, h//lineNumber): # <-- CHANGED
        c.create_line([(0, i), (w, i)], tag='grid_line')

lineNumber=10#在代码中,请在创建网格函数中添加此for循环

for i in range (0, w, 100):
        for j in range (0, h, 100):
          
           x=c.create_rectangle(i, j,i+100,j+100,fill='white')
           c.tag_bind(x, '<ButtonRelease-1>', onObjectClick)
def onObjectClick(event):
        x=event.widget.find_closest(event.x, event.y)[0]
        c.itemconfig(x,fill="blue")
所以你的完整代码是

import tkinter as tk
###################Add this function also###################
def onObjectClick(event):
        x=event.widget.find_closest(event.x, event.y)[0]
        c.itemconfig(x,fill="blue")
############################################################
        
def create_grid(event=None):

    global areas
    w = c.winfo_width() # Get current width of canvas
    h = c.winfo_height() # Get current height of canvas
    c.delete('grid_line') # Will only remove the grid_line

    # Creates all vertical lines at intevals of 100
    for i in range(0, w, 100):
        c.create_line([(i, 0), (i, h)], tag='grid_line')

    # Creates all horizontal lines at intevals of 100
    for i in range(0, h, 100):
        c.create_line([(0, i), (w, i)], tag='grid_line')
        
#####Add this loop#############################################   
    for i in range (0, w, 100):
        for j in range (0, h, 100):
           x=c.create_rectangle(i, j,i+100,j+100,fill='white')
           c.tag_bind(x, '<ButtonRelease-1>', onObjectClick)
           
#####Add this for loop##########################################               
  

root = tk.Tk()

c = tk.Canvas(root, height=1000, width=1000, bg='white')
c.pack(fill=tk.BOTH, expand=True)

c.bind('<Configure>', create_grid)

    
root.mainloop()
将tkinter作为tk导入
###################同时添加此函数###################
定义对象单击(事件):
x=event.widget.find_closest(event.x,event.y)[0]
c、 itemconfig(x,fill=“蓝色”)
############################################################
def创建网格(事件=无):
全球范围
w=c.winfo_width()#获取画布的当前宽度
h=c.winfo_height()#获取画布的当前高度
c、 删除(“网格线”)只会删除网格线
#以100为单位创建所有垂直线
对于范围(0,w,100)内的i:
c、 创建网格线([(i,0),(i,h)],tag='grid\u line')
#以100为单位创建所有水平线
对于范围(0,h,100)内的i:
c、 创建网格线([(0,i),(w,i)],tag='grid\u line')
#####在本次现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场########
对于范围(0,w,100)内的i:
对于范围(0,h,100)内的j:
x=c.创建_矩形(i,j,i+100,j+100,fill='white')
c、 标记绑定(x',onObject单击)
#####本次为回路增加本次为回路的回路的再加本本次为回路的回路回路的现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支支#####
root=tk.tk()
c=tk.Canvas(根,高度=1000,宽度=1000,bg='white')
c、 包装(填充=tk.BOTH,展开=True)
c、 绑定(“”,创建网格)
root.mainloop()
检查
def onObjectClick(event):
        x=event.widget.find_closest(event.x, event.y)[0]
        c.itemconfig(x,fill="blue")
import tkinter as tk
###################Add this function also###################
def onObjectClick(event):
        x=event.widget.find_closest(event.x, event.y)[0]
        c.itemconfig(x,fill="blue")
############################################################
        
def create_grid(event=None):

    global areas
    w = c.winfo_width() # Get current width of canvas
    h = c.winfo_height() # Get current height of canvas
    c.delete('grid_line') # Will only remove the grid_line

    # Creates all vertical lines at intevals of 100
    for i in range(0, w, 100):
        c.create_line([(i, 0), (i, h)], tag='grid_line')

    # Creates all horizontal lines at intevals of 100
    for i in range(0, h, 100):
        c.create_line([(0, i), (w, i)], tag='grid_line')
        
#####Add this loop#############################################   
    for i in range (0, w, 100):
        for j in range (0, h, 100):
           x=c.create_rectangle(i, j,i+100,j+100,fill='white')
           c.tag_bind(x, '<ButtonRelease-1>', onObjectClick)
           
#####Add this for loop##########################################               
  

root = tk.Tk()

c = tk.Canvas(root, height=1000, width=1000, bg='white')
c.pack(fill=tk.BOTH, expand=True)

c.bind('<Configure>', create_grid)

    
root.mainloop()