from tkinter import * root = Tk() but = Button(root, text='Print') but.grid(row=0, column=0) def coordinates(event): print("""???""") but.bind('<Button-1>', coordinates) mainloop()
from tkinter import * root = Tk() but = Button(root, text='Print') but.grid(row=0, column=0) def coordinates(event): print("""???""") but.bind('<Button-1>', coordinates) mainloop()
justacoderЭто вы зря. Не надо забывать про волшебные свойства функции dir.
Облазив кучу сайтов
# -*-coding:utf-8-*- from Tkinter import * root = Tk() but = Button(root, text='Print') but.grid(row=10, column=0) def coordinates(event): print(but.grid_info()["row"]) but.bind('<Button-1>', coordinates) mainloop()
from tkinter import * root = Tk() def create_grid(x, y): def create_line(x1, y1): def print_x_y(event): x_y = [] row = (but.grid_info()['row']) column = (but.grid_info()['column']) x_y.append(row) x_y.append(column) print(x_y) for j in range(20): but = Button(root, bg='white') but.grid(row=x1, column=y1) but.bind('<Button-1>', print_x_y) y1 += 1 for i in range(20): create_line(x, y) x += 1 create_grid(0, 0) mainloop()
from tkinter import * root = Tk() def print_x_y(but): x_y = [] row = (but.grid_info()['row']) column = (but.grid_info()['column']) x_y.append(row) x_y.append(column) print(x_y) def create_grid(x, y): def create_line(x1, y1): for j in range(20): but = Button(root, bg='white') but.grid(row=x1, column=y1) but.bind('<Button-1>', print_x_y(but)) y1 += 1 for i in range(20): create_line(x, y) x += 1 create_grid(0, 0) mainloop()
from tkinter import Tk, Button import sys matrix = [ [0, 0, 0, 0, 1], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [1, 0, 0, 1, 1], [0, 0, 1, 1, 1], ] root = Tk() def but_click(e): if e.widget.value: print("BOOM") sys.exit() else: e.widget.destroy() def bomb_around(row, cell): i = 0 def inc(r, c): if r < 0 or c < 0: return 0 try: return matrix[r][c] except IndexError: return 0 # cross i += inc(row - 1, cell) i += inc(row, cell - 1) i += inc(row + 1, cell) i += inc(row, cell + 1) #diag i += inc(row - 1, cell - 1) i += inc(row + 1, cell - 1) i += inc(row - 1, cell + 1) i += inc(row + 1, cell + 1) return i for row_index, row in enumerate(matrix): for cell_index, cell in enumerate(row): button = Button(root, text=bomb_around(row_index, cell_index) or '') button.grid(row=row_index, column=cell_index) button.value = cell button.bind("<Button-1>", but_click) root.mainloop()
from tkinter import * root = Tk() def print_x_y(event): x_y = [] row = (event.widget.grid_info()['row']) column = (event.widget.grid_info()['column']) x_y.append(row) x_y.append(column) print(x_y) def create_grid(x, y): def create_line(x1, y1): for j in range(20): but = Button(root, bg='white') but.grid(row=x1, column=y1) but.bind('<Button-1>', print_x_y) y1 += 1 for i in range(20): create_line(x, y) x += 1 create_grid(0, 0) mainloop()