0

I'm having some trouble getting a (ttk, and no I cannot change it to tk) button to maintain its size when it reveals an image. I start with a grid with only black background and no images.

https://i.stack.imgur.com/7ZdOj.png

Clicking on one of the buttons correctly changes the background color and reveals the image, but the button height and width dramatically changes.

https://i.stack.imgur.com/h0gv5.png

Everything seems to snap back to the right size once at least 1 image is revealed in every row and every column as seen below:

https://i.stack.imgur.com/7t2S3.png

My suspicion is something having to do with the stickyness of the widgets but I cannot seem to get the buttons to stay the same size. Is there a way to make this happen? Note: The issue does not occur if all 25 buttons have images from the start.

The button is called self.button_dict[(row_index, col_index)] and the following is the code that gets called when the button is invoked.

# change button color
self.set_style(f"bclicked{row_index}{col_index}.TButton", background=new_color, bordercolor=current_border_color, highlightthickness=10, padding=0)
self.button_dict[(row_index, col_index)].configure(style=f"bclicked{row_index}{col_index}.TButton", command = lambda row_index=row_index, col_index=col_index:
                                                                        self.change_button_color(new_color, current_color, row_index, col_index, current_border_color, placing_ship))

# reset the annotation behavior
self.button_dict[(row_index, col_index)].bind('<Button-3>', lambda event, row_index=row_index, col_index=col_index:
                                                                        self.change_button_color(new_color, self.marking_colors["Annotating Color"], row_index, col_index, "#333333", False, right_clicked=True))

self.checks_found[row_index, col_index] = 1
self.button_dict[(row_index, col_index)].configure(image = self.images[row_index*self.col_size + col_index])
self.button_dict[(row_index, col_index)].image = self.images[row_index*self.col_size + col_index]
roromaniac
  • 26
  • 2
  • Have you tried setting a fixed `width` and `height` for your button widgets? – JRiggles Oct 21 '22 at 12:20
  • Unfortunately ttk.Button widgets don't take fixed width and height values without some text. I have tried to pass the width onto the style object of the button but still no dice. Changing the width of the style does make it so that the expanded cell is now square instead of rectangular, but it still expands. – roromaniac Oct 21 '22 at 13:44
  • I was actually able to fix the width, but not the height of a ttk.Button. – roromaniac Oct 21 '22 at 13:52
  • I ended up going with a little bit of a hacky solution but would still love an answer. The hacky solution was just to include an image of the same size that was just all black pixels. That way buttons with images don't resize any buttons without images since there are no buttons without images! – roromaniac Oct 21 '22 at 14:52

0 Answers0