Skip to content

Instantly share code, notes, and snippets.

@Nadohs
Last active February 10, 2022 15:51
Show Gist options
  • Select an option

  • Save Nadohs/a26ade55c658db2c3f696b456e395485 to your computer and use it in GitHub Desktop.

Select an option

Save Nadohs/a26ade55c658db2c3f696b456e395485 to your computer and use it in GitHub Desktop.
#todo sudoku ui
from sudoku_engine import Board
from guizero import App, PushButton
class SudView:
board_code = None
board_solution = None
app_ref = None
button_list = []
def create_board(self):
board = Board()
question_board_code = board.generateQuestionBoardCode(1) # generates a medium level sudoku
print('board:',question_board_code[0])
self.board_code = question_board_code[0]
self.board_solution = question_board_code[1]
def reset_board(self):
self.create_board()
self.update_buttons()
#self.app_ref.display()
def solve_board(self):
print('solve')
#self.solve_board()
self.update_buttons(custom_board_code=self.board_solution)
def update_buttons(self, custom_board_code=None):
board_code = list(self.board_code)
if custom_board_code:
board_code = list(custom_board_code)
for i in range(0, len(self.button_list)):
button = self.button_list[i]
b_val = board_code[i]
if b_val == '0':
b_val = ' '
button.set_text(b_val)
def layout_view(self, app):
if len(self.button_list) > 0:
self.update_buttons()
return
self.app_ref = app
button1 = PushButton(app, text="New", grid=[0,0], command=self.reset_board)
button2 = PushButton(app, text="Solve", grid=[0,2], command=self.solve_board)
button3 = PushButton(app, text="Print", grid=[0,4])
button4 = PushButton(app, text="Scan", grid=[0,6])
board_code = list(self.board_code)
print(len(board_code))
#TODO: should only build buttons here if needed and use update buttons to assign values only.
for i in range(0,len(board_code)):
cur_val = board_code[i]
if cur_val == '0':
cur_val = ' '
x_pos = int(i/9)+2
y_pos = int(i%9)
print(x_pos, y_pos)
button = PushButton(app, text=cur_val, grid=[x_pos,y_pos])
self.button_list.append(button)
if __name__ == '__main__':
print('prepare for gui')
sview = SudView()
app = App(title="Sudoku Rick", layout='grid')
sview.create_board()
sview.layout_view(app=app)
app.display()
#print cups client setup
#https://www.instructables.com/Enable-Raspberry-Pi-to-Print-to-Networked-Printers/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment