Python 3.x - using text string as variable name

后端 未结 3 693
难免孤独
难免孤独 2021-01-28 19:32

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 st

相关标签:
3条回答
  • 2021-01-28 20:04

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

    import tkinter as tk
    from tkinter import *
    
    class Window:
        def __init__(self):
            self.settings_window()
    
        def Settings_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()
    
        def return_label(self,number):
            entry_field_value = self.entry.get()
            self.labelsinput[number].set(entry_field_value)
    
    window=WINDOW()
    
    0 讨论(0)
  • 2021-01-28 20:23

    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 *
    
    class Window(tk.Tk):
        def __init__(self):
            super().__init__()
            self.minsize(200, 200)
            self.entry_list = []
            self.label_list = []
            entry_count = 2
    
            for i in range(entry_count):
                self.entry_list.append(Entry(self))
                self.entry_list[i].pack()
    
            for i in range(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()
    
        def Return_Label(self):
            for ndex, lbl in enumerate(self.label_list):
                lbl.config(text=self.entry_list[ndex].get())
    
    if __name__ == '__main__':
        Window().mainloop()
    
    0 讨论(0)
  • 2021-01-28 20:31

    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()
        ...
    
    def Return_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)
    
    0 讨论(0)
提交回复
热议问题