[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky] [Facebook]
[Build Your Own Python Action Arcade!]

[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: Add buttons to a row container made with tkinter in Python

[This example lets you add and remove buttons from rows with tkinter in Python]

My previous post Make a row container with tkinter in Python creates a group of rows each containing a sequence of buttons. If you click on a button, it is destroyed and the row container automatically updates the arrangement of any remaining buttons.

This example gives a relatively straightforward addition by allowing you to add buttons to a row. Just click the + button on the row's left to add a new button to the row.

Creating Controls

The following code shows how the program creates its widgets.

def build_ui(self): '''Build the user interface.''' margin = 5 # Save references to the row frames. self.row_frames = [] # Initially the largest value in each row is zero. self.row_max = [0 for row_number in range(10)] # Create the rows. for row_number in range(10): # Create the row frame. frame = tk.Frame(self.window) frame.pack(side=tk.TOP, padx=margin, pady=margin, expand=True, fill=tk.X) self.row_frames.append(frame) # Create a + button. button = tk.Button(frame, text='+', width=2, bg='white') button.pack(side=tk.LEFT, padx=(2,5)) button.configure(command=lambda row_number=row_number: self.add_button(row_number)) # Create other buttons. for i in range(10): self.add_button(row_number)

This code creates a row_frames list to hold references to the row container widgets. It also creates a row_max list to keep track of the largest button number in each row.

The program then enters a loop where it builds each row. For each row, the code creates a Frame to act as the row container. Next, it adds a + button, setting its command property to the add_button method and passing that method the button's row number. It then uses a loop that uses add_button to create the initial 10 buttons that you can click to delete. Adding Buttons The program uses the following add_button method to create its initial buttons. This method also executes when you click a row's + button.

def add_button(self, row_number): '''Add a button to its row.''' self.row_max[row_number] += 1 button = tk.Button(self.row_frames[row_number], text=f'{self.row_max[row_number]}', width=2, bg='white') button.pack(side=tk.LEFT, padx=(2)) button.configure(command= lambda btn=button: self.delete_button(btn))

This code increments the row's row_max value to get the new button's number. It creates the new button, adding it to the correct row container and setting its caption to the row's new row_max value. It packs the button and configures its command property to invoke the delete_button method when it is clicked.

The code that deletes buttons is the same as in the previous example. See that post for details.

Conclusion

That's all there is to it! Now when you add or remove buttons from a row, its row container automatically rearranges the remaining buttons as needed.

Download the example to experiment with it and to see additional details.

© 2025 Rocky Mountain Computer Consulting, Inc. All rights reserved.