How to dynamically add rows with tkinter?

391
December 15, 2019, at 6:20 PM

Imagine you have a tkinter program and want to add some widget without changing all the rows how can I accomplish that?

Here is the program:

from tkinter import *
window = Tk()
window.title("Hello Everyone.")
window.geometry("400x400")

window.grid_columnconfigure((0, 1, 2), weight=1)
window.grid_rowconfigure((0, 1, 2), weight=1)

coordinaten = [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0],
               [2, 1], [2, 2]]
for i in range(len(coordinaten)):
    button = Button(window)
    button.grid(row=coordinaten[i][0], column=coordinaten[i][1], sticky="ewns")

window.mainloop()

Imagine you want to add a label to the window. Is there way to this without changing all the values of the rows like this:

from tkinter import *
window = Tk()
window.title("Hello Everyone.")
window.geometry("400x400")

window.grid_columnconfigure((0, 1, 2), weight=1)
window.grid_rowconfigure((1, 2, 3), weight=2)
window.grid_rowconfigure(0, weight=1)

label = Label(window, text="Some text", bg="green")
label.grid(row=0, columnspan=3, sticky="ewns")
coordinaten = [[1, 0], [1, 1], [1, 2], [2, 0],
               [2, 1], [2, 2], [3, 0], [3, 1], [3, 2]]
for i in range(len(coordinaten)):
    button = Button(window)
    button.grid(row=coordinaten[i][0], column=coordinaten[i][1], sticky="ewns")

window.mainloop()
Answer 1

Question: dynamically add rows python tkinter?

Is there way to this without changing all the values of the rows?
Don't use predefined Row/Columns, here coordinaten = ..., at all.

class App(tk.Tk):
    def __init__(self):
        super().__init__()
        def command():
            pass
        buttons = (('button text 1.1', command), ('button text 1.2', command), ('button text 1,3', command),
                   ('button text 2.1', command), ('button text 2.2', command), ('button text 2.3', command),
                   ('button text 3.1', command), ('button text 3.2', command), ('button text 3.3', command)
                   )
        tk.Label(text='label text').grid(row=0, columnspan=3, sticky="ewns")
        # Button Layout X x 3
        row_offset = 1
        column_n = 3
        for row in range(row_offset, (len(buttons) // column_n) + row_offset):
            self.grid_rowconfigure(row, weight=2)
            for column in range(column_n):
                self.grid_columnconfigure(column, weight=1)
                # The left side has to be in sync with the `button` tuple  
                text, command = buttons[((row - row_offset) * column_n) + column]
                btn = tk.Button(self, text=text, command=command)
                btn.grid(row=row, column=column, sticky="ewns")

if __name__ == "__main__":
    App().mainloop()

To insert a new Row of Buttons, do:

        buttons = (('button text 1.1', command), ('button text 1.2', command), ('button text 1,3', command),
                   ('button text 2.1', command), ('button text 2.2', command), ('button text 2.3', command),
                   ('button new 2.1.1', command), ('button new 2.2.1', command), ('button new 2.3.1', command),
                   ('button text 3.1', command), ('button text 3.2', command), ('button text 3.3', command)
                   )
Rent Charter Buses Company
READ ALSO
using opencv to process an image in python

using opencv to process an image in python

I am using OpenCV to try and process an image for my python class

140
Cannot access an sqlite3 database file with python

Cannot access an sqlite3 database file with python

I'm trying to take recipe hyperlinks from a pre-existing file and add them to a table in a databaseI have already created the database and given the file name to set up a connection and insert the data into the table but whenever I do so I get the error sqlite3

328
Able to install Flask-bcrypt but not able to import flask bcrypt in my project [duplicate]

Able to install Flask-bcrypt but not able to import flask bcrypt in my project [duplicate]

Hi I am have flask bcrypt installed on my mac but i am not able to import the package in my project

144