MCPcopy Index your code
hub / github.com/PySimpleGUI/PySimpleGUI / main

Function main

DemoPrograms/Demo_Sudoku.py:102–153  ·  view source on GitHub ↗

The Main GUI - It does it all. The "Board" is a grid that's 9 x 9. Even though the layout is a grid of 9 Frames, the addressing of the individual squares is via a key that's a tuple (0,0) to (8,8)

(mask_rate=0.7)

Source from the content-addressed store, hash-verified

100
101
102def main(mask_rate=0.7):
103 """"
104 The Main GUI - It does it all.
105
106 The "Board" is a grid that's 9 x 9. Even though the layout is a grid of 9 Frames, the
107 addressing of the individual squares is via a key that's a tuple (0,0) to (8,8)
108 """
109
110
111
112
113 # It's 1 line of code to make a Sudoku board. If you don't like it, then replace it.
114 # Dude (Dudette), it's 1-line of code. If you don't like the board, write a line of code.
115 # The keys for the inputs are tuples (0-8, 0-8) that reference each Input Element.
116 # Get an input element for a position using: window[row, col]
117 # To get a better understanding, take it apart. Spread it out. You'll learn in the process.
118 window = sg.Window('Sudoku',
119 [[sg.Frame('', [[sg.I(random.randint(1,9), justification='r', size=(3,1),enable_events=True, key=(fr*3+r,fc*3+c)) for c in range(3)] for r in range(3)]) for fc in range(3)] for fr in range(3)] +
120 [[sg.B('Solve'), sg.B('Check'), sg.B('Hint'), sg.B('New Game'), sg.T('Mask rate (0-1)'), sg.In(str(mask_rate), size=(3,1),key='-RATE-')],], finalize=True)
121
122 # create and display a puzzle by updating the Input elements
123
124 puzzle, solution = create_and_show_puzzle(window)
125 check_showing = False
126 while True: # The Event Loop
127 event, values = window.read()
128 if event == sg.WIN_CLOSED:
129 break
130
131 if event == 'Solve':
132 for r, row in enumerate(solution):
133 for c, col in enumerate(row):
134 window[r, c].update(solution[r][c], background_color=sg.theme_input_background_color())
135 elif event == 'Check':
136 check_showing = True
137 solved = check_progress(window, solution)
138 if solved:
139 sg.popup('Solved! You have solved the puzzle correctly.')
140 elif event == 'Hint':
141 elem = window.find_element_with_focus()
142 try:
143 elem.update(solution[elem.Key[0]][elem.Key[1]], background_color=sg.theme_input_background_color())
144 except:
145 pass # Likely because an input element didn't have focus
146 elif event == 'New Game':
147 puzzle, solution = create_and_show_puzzle(window)
148 elif check_showing: # an input was changed, so clear any background colors from prior hints
149 check_showing = False
150 for r, row in enumerate(solution):
151 for c, col in enumerate(row):
152 window[r, c].update(background_color=sg.theme_input_background_color())
153 window.close()
154
155if __name__ == "__main__":
156 DEFAULT_MASK_RATE = 0.7 # % Of cells to hide

Callers 1

Demo_Sudoku.pyFile · 0.70

Calls 6

readMethod · 0.95
closeMethod · 0.95
create_and_show_puzzleFunction · 0.85
check_progressFunction · 0.85
updateMethod · 0.45

Tested by

no test coverage detected