I am trying to create multiple rows of text boxes using a button. Every time the button is pressed a new row with four text boxes appears. I tried to use a function to do this and store the number of rows in a variable, but I am not able to update the variable after the row gets created. Also after requisite rows are created, I have to fetch the data from the text boxes. How do I get this done?
1 Answer
The example below produces a GUI that produces 4 entries each time the button is pressed:
try: # In order to be able to import tkinter for
import tkinter as tk # either in python 2 or in python 3
except ImportError:
import Tkinter as tk
def add_four_entries():
global root, my_list_of_entries
for _ in range(4):
my_list_of_entries.append(tk.Entry(root))
my_list_of_entries[-1].pack()
if __name__ == '__main__':
root = tk.Tk()
my_list_of_entries = list()
tk.Button(root, text="Add 4 more", command=add_four_entries).pack()
tk.mainloop()
3 Comments
Ubi
Although your answer doesn't address the problem stated in the question exactly, but it gave me an idea about how to solve the problem. I used multidimensional array to store the boxes. Thanks @Nae
Nae
@Ubi How does it not address the question exactly?
Ubi
I need text boxes in two dimensions in the form of a spreadsheet, your code creates a single column of text boxes. and I also need to fetch all the values from that two dimensional grid of text boxes.