Chambers
-- -- --

How do I add more than one image in one cell in tkinter?

Anonymous in /c/coding_help

620
Here’s my code. I want to add 2 images in one cell and another 2 images in another cell. <br><br>```<br>class CreateBoard(ttk.Frame):<br> def __init__(self, parent, controller, width=600, height=200, **kwargs):<br> super().__init__(parent, width=width, height=height, **kwargs)<br> self.parent = parent<br> self.controller = controller<br> self.pack_propagate(0)<br> self.grid_propagate(0)<br><br> self._create_widgets()<br> self._place_widgets()<br><br> def _create_widgets(self):<br> self._player1_circle = tk.PhotoImage(file="1-yellow.png")<br> self._player2_circle = tk.PhotoImage(file="2-red.png")<br> self._board_frame = tk.Frame(self)<br> self._board_canvas = tk.Canvas(self._board_frame)<br> self._board_frame.pack()<br> self._board_canvas.pack()<br> self._drop_label = tk.Label(self._board_canvas, text="DROP HERE", font=("Arial", 8))<br> self._drop_label.place(relx=0.5, rely=0.5)<br><br> def _place_widgets(self):<br> controller.board_view(self._board_canvas, 0.01, 0.75)<br> self._board_canvas.create_image(65, 65, image=self._player1_circle)<br> self._board_canvas.create_image(75, 65, image=self._player2_circle)<br>```<br><br>This is how I envisioned it<br>```<br>C1 | C2 | C3 | C4 | C5 | C6 | C7<br>---------<br>img | img | | | | | <br>---------<br>img | img | | | | | <br>---------<br>img | img | | | | | <br>---------<br>img | img | | | | | <br>---------<br>img | img | | | | | <br>---------<br>img | img | | | | | <br>```<br><br>This is the function that draws the board cells<br><br>```<br>def board_view(canvas, prop_x, prop_y, r=7, light_color="#ff9436", dark_color="#ebdb89"):<br> # Draw squares<br> canvas.delete("square_lines")<br> game_cell_width = 200<br> game_cell_height = 200<br> num_columns = 7<br> num_rows = 6<br> canvas_width = game_cell_width * num_columns<br> canvas_height = game_cell_height * num_rows<br><br> if canvas.winfo_reqwidth() != canvas_width or canvas.winfo_reqheight() != canvas_height:<br> canvas.config(width=canvas_width, height=canvas_height, bg="#121212")<br><br> square_line_width = (game_cell_width + game_cell_height) / 100<br> padding_x = (canvas.winfo_reqwidth() - game_cell_width * num_columns) / 2<br> padding_y = 0<br> canvas.delete("square_lines")<br> for row in range(num_rows):<br> for column in range(num_columns):<br> x = column * game_cell_width + padding_x<br> y = row * game_cell_height + padding_y<br> x1 = x<br> y1 = y<br> x2 = x1 + game_cell_width<br> y2 = y1<br> x3 = x2<br> y3 = y2 + game_cell_height<br> x4 = x1<br> y4 = y3<br> canvas.create_line(x1, y1, x2, y2, width=square_line_width, fill=light_color, tags=("square_lines"))<br> canvas.create_line(x2, y2, x3, y3, width=square_line_width, fill=light_color, tags=("square_lines"))<br> canvas.create_line(x3, y3, x4, y4, width=square_line_width, fill=light_color, tags=("square_lines"))<br> canvas.create_line(x4, y4, x1, y1, width=square_line_width, fill=light_color, tags=("square_lines"))<br>```

Comments (13) 25874 👁️