<gh_stars>0
import tkinter as tk
import itertools as IT  # avoiding double loops
import ConnectFour  # the game API


class GameScreen(tk.Tk):
    def __init__(self):
        super().__init__()
        self.iconbitmap(default='logo.ico')
        self.title("Connect Four v1.0")
        self._rows = 6
        self._cols = 7

        # create the top canvas for the game board
        canvas = tk.Canvas(self, bg="#0055FF")
        self.create_board(canvas)
        canvas.pack(fill=tk.BOTH, expand=True)

        # create the bottom frame for the game state
        frame = tk.Frame(self)
        self.text1 = tk.Label(
            frame,
            bd=1,
            text="Round 1",
            relief=tk.RAISED,
            bg="lightgrey",
            fg="black")
        self.text2 = tk.Label(
            frame,
            bd=1,
            text="Red's turn",
            relief=tk.RAISED,
            bg="lightgrey",
            fg="black")
        self.text3 = tk.Label(
            frame,
            bd=1,
            text="Undecided",
            relief=tk.RAISED,
            bg="lightgrey",
            fg="black")
        frame.columnconfigure(0, weight=1)
        frame.columnconfigure(1, weight=1)
        frame.columnconfigure(2, weight=1)
        self.text1.grid(row=1, column=0, sticky=tk.E + tk.W)
        self.text2.grid(row=1, column=1, sticky=tk.E + tk.W)
        self.text3.grid(row=1, column=2, sticky=tk.E + tk.W)
        frame.pack(side=tk.BOTTOM, fill=tk.X)

        # create new game
        self.game = ConnectFour.Game()

    def create_board(self, canvas):
        """Create a new empty board with circle shapes for discs"""
        self.board = [[None] * self._cols for _ in range(self._rows)]
        self.buttons = [[None] * self._cols for _ in range(self._rows)]
        for i, j in IT.product(range(self._rows), range(self._cols)):
            self.board[self._rows - 1 - i][j] = L = tk.Canvas(
                canvas,
                bg="#0055FF",
                height=60,
                width=60,
                relief="raised",
                highlightthickness=0)
            padding = 2
            id = self.buttons[self._rows - 1 - i][j] = L.create_oval(
                (padding, padding, 50 + padding, 50 + padding),
                outline="black",
                fill="lightgrey")
            width = L.winfo_reqwidth()
            height = L.winfo_reqheight()
            L.configure(width=width, height=height)

            canvas.rowconfigure(i, weight=1)
            canvas.columnconfigure(j, weight=1)
            L.grid(
                row=i,
                column=j,
                padx=3,
                pady=3,
                sticky=tk.E +
                tk.W +
                tk.N +
                tk.S)
            L.bind('<Button-1>', lambda e, id=id, j=j: self.on_click(e, id, j))

    def on_click(self, event, id, j):
        """Column j was clicked. The method that actually preform the moves."""
        player = self.game.turn
        player_color = "Red" if self.game.turn == 'X' else "Yellow"
        next_player_color = "Yellow" if self.game.turn == 'X' else "Red"
        try:
            self.game.move(j)
            self.text3.config(text="Undecided".format(j))
        except ConnectFour.FullColumn as e:
            self.text3.config(text="Column {} is full".format(j))
            return

        i = self.game.col_size[j] - 1
        self.board[i][j].itemconfig(
            id, fill=player_color, tags="{}".format(player))
        self.text1.config(text="Round {}".format(self.game.round))
        self.text2.config(text="{}'s turn".format(next_player_color))
        if self.game.winner in ["X", "Y"]:
            self.text3.config(text="{} has won!".format(player_color))
            self.text2.config(text="The End")
            for i, j in IT.product(range(self._rows), range(self._cols)):
                self.board[i][j].unbind("<Button-1>")
            self.flash_discs(
                self.game.get_winning_discs(),
                "red" if self.game.winner == 'X' else "yellow",
                "blue")

        if self.game.winner == 'D':
            self.text1.config(text="Round 42")
            self.text2.config(text="The End")
            self.text3.config(text="Draw")
            for i, j in IT.product(range(self._rows), range(self._cols)):
                self.board[i][j].unbind("<Button-1>")

    def flash_discs(self, discs, winner, altcolor):
        """Alternate color of given discs"""
        for disc in discs:
            row = disc[0]
            col = disc[1]
            self.board[row][col].itemconfig(
                self.buttons[row][col], fill=altcolor)

        self.after(250, self.flash_discs, discs, altcolor, winner)


root = GameScreen()
root.mainloop()

import tkinter as tk
import itertools as IT  # avoiding double loops
import ConnectFour  # the game API


class GameScreen(tk.Tk):
    def __init__(self):
        super().__init__()
        self.iconbitmap(default='logo.ico')
        self.title("Connect Four v1.0")
        self._rows = 6
        self._cols = 7

        # create the top canvas for the game board
        canvas = tk.Canvas(self, bg="#0055FF")
        self.create_board(canvas)
        canvas.pack(fill=tk.BOTH, expand=True)

        # create the bottom frame for the game state
        frame = tk.Frame(self)
        self.text1 = tk.Label(
            frame,
            bd=1,
            text="Round 1",
            relief=tk.RAISED,
            bg="lightgrey",
            fg="black")
        self.text2 = tk.Label(
            frame,
            bd=1,
            text="Red's turn",
            relief=tk.RAISED,
            bg="lightgrey",
            fg="black")
        self.text3 = tk.Label(
            frame,
            bd=1,
            text="Undecided",
            relief=tk.RAISED,
            bg="lightgrey",
            fg="black")
        frame.columnconfigure(0, weight=1)
        frame.columnconfigure(1, weight=1)
        frame.columnconfigure(2, weight=1)
        self.text1.grid(row=1, column=0, sticky=tk.E + tk.W)
        self.text2.grid(row=1, column=1, sticky=tk.E + tk.W)
        self.text3.grid(row=1, column=2, sticky=tk.E + tk.W)
        frame.pack(side=tk.BOTTOM, fill=tk.X)

        # create new game
        self.game = ConnectFour.Game()

    def create_board(self, canvas):
        """Create a new empty board with circle shapes for discs"""
        self.board = [[None] * self._cols for _ in range(self._rows)]
        self.buttons = [[None] * self._cols for _ in range(self._rows)]
        for i, j in IT.product(range(self._rows), range(self._cols)):
            self.board[self._rows - 1 - i][j] = L = tk.Canvas(
                canvas,
                bg="#0055FF",
                height=60,
                width=60,
                relief="raised",
                highlightthickness=0)
            padding = 2
            id = self.buttons[self._rows - 1 - i][j] = L.create_oval(
                (padding, padding, 50 + padding, 50 + padding),
                outline="black",
                fill="lightgrey")
            width = L.winfo_reqwidth()
            height = L.winfo_reqheight()
            L.configure(width=width, height=height)

            canvas.rowconfigure(i, weight=1)
            canvas.columnconfigure(j, weight=1)
            L.grid(
                row=i,
                column=j,
                padx=3,
                pady=3,
                sticky=tk.E +
                tk.W +
                tk.N +
                tk.S)
            L.bind('<Button-1>', lambda e, id=id, j=j: self.on_click(e, id, j))

    def on_click(self, event, id, j):
        """Column j was clicked. The method that actually preform the moves."""
        player = self.game.turn
        player_color = "Red" if self.game.turn == 'X' else "Yellow"
        next_player_color = "Yellow" if self.game.turn == 'X' else "Red"
        try:
            self.game.move(j)
            self.text3.config(text="Undecided".format(j))
        except ConnectFour.FullColumn as e:
            self.text3.config(text="Column {} is full".format(j))
            return

        i = self.game.col_size[j] - 1
        self.board[i][j].itemconfig(
            id, fill=player_color, tags="{}".format(player))
        self.text1.config(text="Round {}".format(self.game.round))
        self.text2.config(text="{}'s turn".format(next_player_color))
        if self.game.winner in ["X", "Y"]:
            self.text3.config(text="{} has won!".format(player_color))
            self.text2.config(text="The End")
            for i, j in IT.product(range(self._rows), range(self._cols)):
                self.board[i][j].unbind("<Button-1>")
            self.flash_discs(
                self.game.get_winning_discs(),
                "red" if self.game.winner == 'X' else "yellow",
                "blue")

        if self.game.winner == 'D':
            self.text1.config(text="Round 42")
            self.text2.config(text="The End")
            self.text3.config(text="Draw")
            for i, j in IT.product(range(self._rows), range(self._cols)):
                self.board[i][j].unbind("<Button-1>")

    def flash_discs(self, discs, winner, altcolor):
        """Alternate color of given discs"""
        for disc in discs:
            row = disc[0]
            col = disc[1]
            self.board[row][col].itemconfig(
                self.buttons[row][col], fill=altcolor)

        self.after(250, self.flash_discs, discs, altcolor, winner)


root = GameScreen()
root.mainloop()
# avoiding double loops # the game API # create the top canvas for the game board # create the bottom frame for the game state # create new game Create a new empty board with circle shapes for discs Column j was clicked. The method that actually preform the moves. Alternate color of given discs