Skip to content Skip to sidebar Skip to footer

Multiple Entry Widgets Using A Loop In Python Tkinter

I am making four Entry widgets in tkinter using a single loop. I got an error - could anyone help me resolve the error I got in this code? I need to track all four Entry widgets, s

Solution 1:

The key problem is that you index the arrays self.textEntryVar and self.e without ever having created them first, nor allocated any items. You need to create them as empty arrays and append onto them.

Another problem seems to be that you never pack the frame created by App() onto the root.

Not a problem, but since you're using the Python 3 'tkiner', we might as well use the simpler Python 3 super() initialization.

Below is my rework of your code with the above modifications and other fixes, see if it works better for you:

import tkinter as tk

class App(tk.Frame):
    def __init__(self):
        super().__init__()

        self.pack(fill=tk.BOTH, expand=1)

        self.stringVars = []
        self.entries = []

        for offset in range(4):
            stringVar = tk.StringVar()
            self.stringVars.append(stringVar)

            entry = tk.Entry(self, width=15, background='white', textvariable=stringVar, justify=tk.CENTER, font='-weight bold')
            entry.grid(padx=10, pady=5, row=17 + offset, column=1, sticky='W,E,N,S')
            self.entries.append(entry)

if __name__ == '__main__':
    root = tk.Tk()
    root.geometry("200x175")
    app = App()
    root.mainloop()

Post a Comment for "Multiple Entry Widgets Using A Loop In Python Tkinter"