简体   繁体   中英

Using a Variable in the Instance Name of a Tkinter Entry Box

I'm attempting to use a variable as the instance name for a tkinter Entry box so that when I shuffle through a for-loop, each box has a unique instance name. Is there a way to do this so that later I can pull individual entry values?

    hoursList = [1, 2, 3, 4, 5]
    x = 0
    for item in hoursList:
        varName = "userEntry" + str(x)
        self.varName = tk.Entry(self.frame)
        self.varName.grid()
        x += 1
    self.getInput = tk.Button(self.frame, text="Submit", command= self.submitHours())
    self.getInput.grid()

def submitHours(self):
    if self.varName.get() is not None:
        print(self.varName.get())

Ideally this segment would create 5 Entry boxes named "userEntry1", "userEntry2", etc. and would print each submitted value.

Try to avoid dynamically creating variable names, if you can. It is possible, but the implementation is often convoluted and can lead to hard to understand code as your codebase gets more and more complicated. Instead, use a container like a list to hold all of your entries. There is no need for them to have a name, you just have to be able to reference them.

Additionally, your command = self.submit_hours() isn't doing what you think it's doing. As it is written, self.submit_hours will be run as you create your submit button. You want to pass a callable object to command (like a function). When the button is clicked, whatever you pass to command will be called . To fix this in your code, just remove the parenthesis on self.submit_hours() since self.submit_hours itself is callable.

A minimal example showing how to store your entries in a list, how to access them, and how they correspond to your hour_list is shown below.

import tkinter as tk

class MyApp:
    def __init__(self):
        self.root = tk.Tk()
        self.frame = tk.Frame(master = self.root)
        self.frame.grid()
        self.hour_list = [1, 2, 3, 4, 5]
        self.entries = []
        self.make_entries()
        self.root.mainloop()


    def submit_hours(self):
        for n, entry in enumerate(self.entries):
            print(f"Entry {n} (hour {self.hour_list[n]}) has the value '{entry.get()}'")
        return

    def make_entries(self):
        for hour in self.hour_list:
            temp_entry= tk.Entry(master = self.frame)
            temp_entry.grid()
            self.entries.append(temp_entry)
        self.submit_button = tk.Button(master = self.frame, text = "Submit",
                                       command = self.submit_hours)
        self.submit_button.grid()
        return

test = MyApp()

This is my console output when I type stuff into the entries and then hit the submit button:

Entry 0 (hour 1) has the value 'first box'
Entry 1 (hour 2) has the value 'this is the 2nd'
Entry 2 (hour 3) has the value 'and the third box'
Entry 3 (hour 4) has the value 'fourth'
Entry 4 (hour 5) has the value '5th'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM