def _redraw_all_spots(self) -> None: self._canvas.delete(tkinter.ALL) canvas_width = self._canvas.winfo_width() canvas_height = self._canvas.winfo_height() #print information on tk window self._weights_white['text'] = 'WHITE: {}'.format( self._state._othello._weight[1]) self._weights_black['text'] = 'BLACK: {}'.format( self._state._othello._weight[0]) self._turn_label['text'] = 'TURN: {}'.format( self._state._othello.turn_show()[-1]) self._rule_label['text'] = 'RULE: {}'.format(self._rule) #draw pieces for spot in self._state.all_spots(): center_x, center_y = spot.center().pixel(canvas_width, canvas_height) radius_x = spot.radius_frac()[0] * canvas_width radius_y = spot.radius_frac()[1] * canvas_height if spot._color is WHITE: self._canvas.create_oval(center_x - radius_x, center_y - radius_y, center_x + radius_x, center_y + radius_y, fill='snow', outline='#000000') elif spot._color is BLACK: self._canvas.create_oval(center_x - radius_x, center_y - radius_y, center_x + radius_x, center_y + radius_y, fill='#000000', outline='#000000') #draw grid on board diax = 2 * radius_x diay = 2 * radius_y for i in range(self._rows): self._canvas.create_line(0, diay * i, canvas_width, diay * i) for j in range(self._cols): self._canvas.create_line(diax * j, 0, diax * j, canvas_height) #winner window and get information about how to process afterwards if self._state.winner(): winner_window = Winner_Dialog(self._state.winner(), self._root_window) winner_window.show() afterwards = winner_window.afterwards() if afterwards is 1: SpotsApplication( spots_model.SpotsState(self._rows, self._cols, self._first_player, self._topleft, self._rule)).run()
def __init__(self): self._state = spots_model.SpotsState() self._root_window = tkinter.Tk() self._canvas = tkinter.Canvas(master=self._root_window, width=600, height=500, background='purple') self._canvas.bind('<Button-1>', self._on_canvas_clicked) self._canvas.bind('<Configure>', self._on_canvas_resized) self._canvas.grid(row=0, column=0, padx=10, pady=10, sticky=tkinter.N + tkinter.S + tkinter.W + tkinter.E) self._root_window.rowconfigure(0, weight=1) self._root_window.columnconfigure(0, weight=1)
def _on_greet(self) -> None: dialog = NameDialog() dialog.show() #default setup fp = dialog.get_fp() tl = dialog.get_tl() rows = int(dialog.get_rows()) cols = int(dialog.get_cols()) rule = ('>' if dialog.get_rule() is 1 else '<') if dialog.was_ok_clicked(): #get setup parameter from dialog fp = dialog.get_fp() tl = dialog.get_tl() rows = int(dialog.get_rows()) cols = int(dialog.get_cols()) rule = ('>' if dialog.get_rule() is 1 else '<') first_player_str = ('White' if fp == 1 else 'Black') topleft_str = ('White' if tl == 1 else 'Black') self._greeting_text.set( 'Setup: Rows:{} Cols:{} First player: {} Topleft: {} rule: {}'. format(rows, cols, first_player_str, topleft_str, rule)) #run othello spots_gui.SpotsApplication( spots_model.SpotsState(rows, cols, fp, tl, rule)).run()
# "what it looks like" (i.e., a "view" problem, not a "model" # problem), that code belongs here in the user interface. self._canvas.delete(tkinter.ALL) canvas_width = self._canvas.winfo_width() canvas_height = self._canvas.winfo_height() for spot in self._state.all_spots(): center_x, center_y = spot.center().pixel(canvas_width, canvas_height) radius_x = spot.radius_frac() * canvas_width radius_y = spot.radius_frac() * canvas_height self._canvas.create_oval(center_x - radius_x, center_y - radius_y, center_x + radius_x, center_y + radius_y, fill='#ffff00', outline='#000000') if __name__ == '__main__': # Create a SpotsState object that will store the current state of the # Spots application as it runs, then pass it to a newly-created # SpotsApplication, and finally start the application. Note how # we can nest expressions like this when we don't need to reuse their # results more than once. SpotsApplication(spots_model.SpotsState()).start()
# "what it looks like" (i.e., a "view" problem, not a "model" # problem), that code belongs here in the user interface. self._canvas.delete(tkinter.ALL) canvas_width = self._canvas.winfo_width() canvas_height = self._canvas.winfo_height() for spot in self._state.all_spots(): center_x, center_y = spot.center().pixel(canvas_width, canvas_height) radius_x = spot.radius_frac() * canvas_width radius_y = spot.radius_frac() * canvas_height self._canvas.create_oval(center_x - radius_x, center_y - radius_y, center_x + radius_x, center_y + radius_y, fill='#ffff00', outline='#000000') if __name__ == '__main__': # Create a SpotsState object that will store the current state of the # Spots application as it runs, then pass it to a newly-created # SpotsApplication, and finally start the application. Note how # we can nest expressions like this when we don't need to reuse their # results more than once. SpotsApplication(spots_model.SpotsState()).run()
def __init__(self): self._state = spots_model.SpotsState() self.board = [] self._root_window = tkinter.Tk() self.row_size = 4 self.col_size = 6 self.box_size = 0 if self.row_size > self.col_size: self.box_size = (16 / self.row_size) * 40 else: self.box_size = (16 / self.col_size) * 40 self.current_move = [] self.piece_list = [] self.B_score = 0 self.W_score = 0 self.B_points = tkinter.StringVar() self.W_points = tkinter.StringVar() self.turn_and_winner_var = tkinter.StringVar() self.B_points.set('') self.W_points.set('') self.turn_and_winner_var.set("") self.canvas = tkinter.Canvas( master = self._root_window, width = 640, \ height = 640, background = '#006000') self.canvas.grid(row=0, column=0, padx=10, pady=10, sticky=tkinter.N + tkinter.S + tkinter.E + tkinter.W) self.canvas.bind('<Button-1>', self._on_canvas_clicked) self.canvas.bind('<Configure>', self._on_canvas_resized) self._root_window.rowconfigure(0, weight=1) self._root_window.columnconfigure(0, weight=1) self.winner_and_turn = tkinter.Label( master=self._root_window, textvariable=self.turn_and_winner_var) self.winner_and_turn.grid( row=1, column=0, padx=10, pady=10, ) self.padding = tkinter.Label(master=self._root_window) self.padding.grid(row=2, column=0, padx=10, pady=10, sticky=tkinter.W) self.B_score = tkinter.Label(master=self._root_window, textvariable=self.B_points) self.B_score.grid(row=1, column=0, padx=10, pady=10, sticky=tkinter.W) self.W_score = tkinter.Label(master=self._root_window, textvariable=self.W_points) self.W_score.grid(row=1, column=0, padx=10, pady=10, sticky=tkinter.E) self.whose_turn = False self._root_window.rowconfigure(0, weight=1) self._root_window.columnconfigure(0, weight=1) self.make_board() self._bob = tkinter.Button(master = self._root_window, text = 'X',\ width = 20, height = 10, command = self.make_square) self._bob.grid(row = 0, column = 1, padx = 2, pady = 2,\ sticky = tkinter.N + tkinter.S + tkinter.E + tkinter.W)