Skip to content Skip to sidebar Skip to footer

Python 3.x - Using Text String As Variable Name

I'm trying to avoid to multiply functions in code by using def Return_Label(self,number) with a parameter. Any Idea how to use string in order to define variable name usable

Solution 1:

I prefer a list approach to managing multiple entry fields and updating values. By using list you can use the index value to manage the labels as well :D.

See the below example of how you can use list to deal with all the values and updates.

import tkinter as tk
from tkinter import *

classWindow(tk.Tk):
    def__init__(self):
        super().__init__()
        self.minsize(200, 200)
        self.entry_list = []
        self.label_list = []
        entry_count = 2for i inrange(entry_count):
            self.entry_list.append(Entry(self))
            self.entry_list[i].pack()

        for i inrange(entry_count):
            self.label_list.append(Label(self,bg='yellow'))
            self.label_list[i].pack(expand='yes', fill='x')

        Button(self, text='SETUP', command=self.Return_Label).pack()

    defReturn_Label(self):
        for ndex, lbl inenumerate(self.label_list):
            lbl.config(text=self.entry_list[ndex].get())

if __name__ == '__main__':
    Window().mainloop()

Solution 2:

Create lists of objects rather than individual attributes for each object. For example,

import tkinter as tk
from tkinter import *

classWindow:
    def__init__(self):
        self.settings_window()

    defSettings_Window(self):
        self.settings_window = tk.Tk()
        self.settings_window.minsize(200,200)

        self.entries = [
            Entry(self.settings_window), 
            Entry(self.settings_window)
        ]

        for e in self.entries:
            e.pack()

        self.labelinputs = [
            StringVar(),
            StringVar()
        ]

        self.labels = [
            Label(self.settings_window, textvariable=label, bg='yellow')
            for label in self.labelinputs
        ]

        for l in self.labels:
            l.pack(expand='yes', fill='x')

        self.buttons = [
            Button(self.settings_window,text='SETUP1',command=lambda: self.return_label(0))
            Button(self.settings_window,text='SETUP2',command=lambda: self.return_label(1))
        ]

        for b in self.buttons:
            b.pack()

        self.settings_window.mainloop()

    defreturn_label(self,number):
        entry_field_value = self.entry.get()
        self.labelsinput[number].set(entry_field_value)

window=WINDOW()

Solution 3:

Dynamicly computing variable names should be avoided at all costs. They are difficult to do correctly, and it makes your code hard to understand, hard to maintain, and hard to debug.

Instead, store the widgets in a dictionary or list. For example:

def__init___(self):
    ...
    self.vars = {}
    ...
    self.vars[1] = StringVar()
    self.vars[2] = StringVar()
    ...

defReturn_Label(self,number):
    self.entry_field_value = self.entry.get()
    var = self.vars[number]
    var.set(self.entry_field_value)

Though, you really don't need to use StringVar at all -- they usually just add extra overhead without providing any extra value. You can save the labels instead of the variables, and call configure on the labels

self.labels[1] = Label(...)
...
self.labels[number].configure(text=self.entry_field_value)

Post a Comment for "Python 3.x - Using Text String As Variable Name"