Note that there are some explanatory texts on larger screens.

plurals
  1. POButtons overriding set height/width (appearing too big) when creating them in a for loop
    primarykey
    data
    text
    <p>I have a method that creates an array of buttons (simulating a grid). The user can click the buttons and a "marker" (image) will appear on it. The problem I have is that even though I'm setting height and width within my method, when it is called, the buttons appear way too big. Important to note that when the user clicks the button, it "shrinks" to the size I specified... Weird. Here is my method: </p> <pre><code>def create_grid(self, leng) : try: global board board = grid.Grid(leng) frame = tkinter.Frame(self.main_window) buttons = [] for row_index in range(0,leng) : row = [] for column_index in range(0,leng) : button = tkinter.Button(frame, height=36, \ width = 36, text = " ", \ command=lambda row=row_index, \ column=column_index: \ self.button_clicked(row,column)) button.grid(row=row_index, column=column_index) row.append(button) buttons.append(row) self.gomuko_buttons = buttons return frame.grid(row=0, column=0) except TypeError: return </code></pre> <p><img src="https://i.stack.imgur.com/x8DLt.jpg" alt="Issue"></p> <p>Also, the function that executes when the button is pressed is the following:</p> <pre><code>def button_clicked(self,row,column) : global turns grid.Comp = False board.place_marker(row, column) button = self.gomuko_buttons[row][column] button['image'] = self.photo1 button['state'] = 'disabled' turns = turns + 1 </code></pre> <p>Any hint? :(</p> <p>EDIT:</p> <p>I solved it thanks to Bryan Oakley's response. Turns out the behaviour for width and height is different for images and text. I solved the problem by simply adding a blank image to the loop, like the following:</p> <pre><code>button = tkinter.Button(frame, height=0, \ width = 0, text = " ", image = self.blank, \ command=lambda row=row_index, \ column=column_index: \ self.button_clicked(row,column)) </code></pre> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload