Skip to content

Commit

Permalink
impl tkinter gui 10x10
Browse files Browse the repository at this point in the history
  • Loading branch information
nitsanavni committed Nov 10, 2023
1 parent 961463a commit 80859a2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
approvaltests
pytest
pytest
42 changes: 42 additions & 0 deletions tests/test_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,48 @@

from game_of_life import GameOfLife

import tkinter as tk

class TkinterGui:

def __init__(self, game_of_life,width=10,height=10):
self.width = width
self.height = height
self.game = game_of_life
self.root = tk.Tk()
self.root.title("Game of Life")
self.cell_size = 10
self.cells = {}
self.create_widgets()

self.root.mainloop()

def create_widgets(self):
for x in range(self.width):
for y in range(self.height):
cell_frame = tk.Frame(self.root, width=self.cell_size, height=self.cell_size,
borderwidth=1, relief='raised')
cell_frame.grid(row=y, column=x)
cell = tk.Label(cell_frame, bg='white', width=2, height=1)
cell.pack(padx=1, pady=1)
self.cells[(x, y)] = cell
self.update_cells()

def update_cells(self):
for x in range(self.width):
for y in range(self.height):
if self.game.is_alive(x, y):
self.cells[(x, y)].config(bg='black')
else:
self.cells[(x, y)].config(bg='white')

def test_tkinter_gui():
game = GameOfLife()
create_glider_at(game, 0, 0)
gui = TkinterGui(game)
gui.root.update() # Update the GUI frame to show latest cell configurations
gui.root.after(2000, lambda: gui.root.destroy()) # Keep the window open for 2 seconds and then close it
gui.root.mainloop() # Start the GUI event loop

def create_glider_at(game,x,y):
game.set_alive(x+0,y+1)
Expand Down

0 comments on commit 80859a2

Please sign in to comment.