I've been having problems with tkinter in Python when I use it with complex methods. For example, when programming a minesweeper game, I have problem with a method that mark a cell as mined or put a "?" if there are doubts about it. If I abuse pressing the mouses's right Button, the method stops working wright. I don't know why? I used the time module to set that clicking at the same time on the two buttons will make reveal all the not marked cells around the clicked cell. from tkinter import Button, Tk, Frame, Label import time import random class Cell(Button): def __init__(self, board, row_number, column_number): self.board = board self.row_number = row_number self.column_number = column_number self.number_of_mines_around = 0 self.is_mined = False self.is_revealed = False self.is_marked = False self.time1 = 0 self.time2 = 0 self.list_of_adjacent_cells = [] self.number_of_right_click = 0 super().__init__(board.cells_container, text=" ", bg="blue", font=("arial", 14, "bold"), width=2, height=1) self.grid(row=row_number, column=column_number, sticky="nsew") self.bind("<Button-1>", lambda event: self.reveal()) self.bind("<Button-3>", lambda event: self.mark()) def reveal(self): self.time1 = time.time() self.reveal_adjacent_cells() if self.is_revealed: return if self.is_marked: return if self.is_mined: self.board.show_mined_cells() self.config(bg="red") return self.is_revealed = True self.board.cells_to_reveal -= 1 self.board.label_for_probability_values.configure(text=self.board.cells_to_reveal) self.config(text=self.number_of_mines_around, bg="white") self.board.number_of_revealed_cells += 1 if self.number_of_mines_around != 0: return for cell in self.list_of_adjacent_cells: cell.reveal() def mark(self): self.time2 = time.time() self.reveal_adjacent_cells() if self.is_revealed: return self.number_of_right_click += 1 if self.number_of_right_click % 3 == 0: self.configure(text=" ") return if self.number_of_right_click % 3 == 1: self.is_marked = not self.is_marked self.config(text="P", fg="white") return self.config(text="?", fg="white") def reveal_adjacent_cells(self): if not self.is_revealed: return _count = sum(1 for cell in self.list_of_adjacent_cells if cell.is_marked) if _count != self.number_of_mines_around: return if abs(self.time1 - self.time2) > 0.3: return for cell in self.list_of_adjacent_cells: cell.reveal() def check_if_it_is_adjacent(self, cell): if abs(self.row_number - cell.row_number) > 1: return if abs(self.column_number - cell.column_number) > 1: return self.list_of_adjacent_cells.append(cell) def count_the_mines_around(self): self.number_of_mines_around = sum(1 for cell in self.list_of_adjacent_cells if cell.is_mined) class Table(Tk): def __init__(self, number_of_rows, number_of_columns, number_of_mines): self.number_of_rows = number_of_rows self.number_of_columns = number_of_columns self.number_of_mines = number_of_mines self.list_of_cells = [] self.number_of_revealed_cells = 0 super().__init__() self.config(width=1000, height=1000) self.cells_container = Frame(self) self.cells_container.pack(expand=True, fill="both") for num in range(self.number_of_rows): self.cells_container.rowconfigure(num, weight=1) for num in range(self.number_of_columns): self.cells_container.columnconfigure(num, weight=1) self.create_cells() self.find_adjacent_cells_for_each_cell() self.scatter_the_mines() self.count_how_many_mines_is_around_each_cell() self.cells_to_reveal = len(self.list_of_cells) - self.number_of_mines self.label_for_probability_values = Label(self, text=self.cells_to_reveal, font=("arial", 14, "bold"), bg="#BFD641") self.label_for_probability_values.pack() self.mainloop() def create_cells(self): for row_number in range(self.number_of_rows): for column_number in range(self.number_of_columns): self.list_of_cells.append(Cell(self, row_number, column_number)) def find_adjacent_cells_for_each_cell(self): aa = self.number_of_columns adjacent_cell_position = (-1 - aa, -aa, -aa + 1, -1, 1, aa - 1, aa, aa + 1) for cell in self.list_of_cells: posit_cell = self.list_of_cells.index(cell) for num in adjacent_cell_position: try: cell.check_if_it_is_adjacent(self.list_of_cells[posit_cell + num]) except IndexError: pass def count_how_many_mines_is_around_each_cell(self): for cell in self.list_of_cells: cell.count_the_mines_around() def scatter_the_mines(self): for cell in random.sample(self.list_of_cells, self.number_of_mines): cell.is_mined = True def show_mined_cells(self): for cell in self.list_of_cells: if cell.is_mined: cell.config(text="X", bg="black", fg="white") Continue reading...