how to use loop to reduce the the code in python

*爱你&永不变心* 提交于 2021-02-11 12:26:15

问题


I would like to create a lot of widgets in tkinter. I am currently using a lot of code for this. Is there a way to summarise the code? However, I would still like to be able to capture the value of each widgets. The numbering goes up to 200... Thanks a lot!

'''

    self.A1_rubbing_marks_Type = tk.StringVar(self.A_Frame_measure)
    # Dictionary with options
    self.A1_choice_rubbing_marks=  ['No', 'Yes']
    self.A1_rubbing_marks_Type.set('') # set the default option
    self.A1_Menu_rubbing_marks = tk.OptionMenu(self.A_Frame_measure, self.A1_rubbing_marks_Type, *self.A1_choice_rubbing_marks, command=self.show_rubbing_marks_borders)
    self.A1_Menu_rubbing_marks.config(width=4)    
    self.A1_Menu_rubbing_marks.grid_forget()

    self.A2_rubbing_marks_Type = tk.StringVar(self.A_Frame_measure)
    # Dictionary with options
    self.A2_choice_rubbing_marks=  ['No', 'Yes']
    self.A2_rubbing_marks_Type.set('') # set the default option
    self.A2_Menu_rubbing_marks = tk.OptionMenu(self.A_Frame_measure, self.A2_rubbing_marks_Type, *self.A2_choice_rubbing_marks, command=self.show_rubbing_marks_borders)
    self.A2_Menu_rubbing_marks.config(width=4)    
    self.A2_Menu_rubbing_marks.grid_forget()

    self.A3_rubbing_marks_Type = tk.StringVar(self.A_Frame_measure)
    # Dictionary with options
    self.A3_choice_rubbing_marks=  ['No', 'Yes']
    self.A3_rubbing_marks_Type.set('') # set the default option
    self.A3_Menu_rubbing_marks = tk.OptionMenu(self.A_Frame_measure, self.A3_rubbing_marks_Type, *self.A3_choice_rubbing_marks, command=self.show_rubbing_marks_borders)
    self.A3_Menu_rubbing_marks.config(width=4)    
    self.A3_Menu_rubbing_marks.grid_forget()

    self.A4_rubbing_marks_Type = tk.StringVar(self.A_Frame_measure)
    # Dictionary with options
    self.A4_choice_rubbing_marks=  ['No', 'Yes']
    self.A4_rubbing_marks_Type.set('') # set the default option
    self.A4_Menu_rubbing_marks = tk.OptionMenu(self.A_Frame_measure, self.A4_rubbing_marks_Type, *self.A4_choice_rubbing_marks, command=self.show_rubbing_marks_borders)
    self.A4_Menu_rubbing_marks.config(width=4)    
    self.A4_Menu_rubbing_marks.grid_forget()

'''

I defined a dict, but i get too much issues.

'''

    keys_blade_exchange_Type = [f"A{i}" for i in range(1, 200)]
    self.blade_exchange_Type = {k_2: tk.StringVar(self.A_Frame_measure) for k_2 in keys_blade_exchange_Type}
    self.choice_blade_exchange =  ['No', 'Yes']
    self.Menu_blade_exchange = {k_2: tk.OptionMenu(self.A_Frame_measure, *self.choice_blade_exchange ,  self.blade_exchange_Type[k_2]) for k_2 in keys_blade_exchange_Type}

    for r, (k_2, cb_2) in enumerate(self.Menu_blade_exchange.items(), start=2):
        cb_2.grid(row=r, column=1, sticky="W", padx=25, pady=4)

'''


回答1:


You can use a simple loop, setattr and getattr.

for i in range(1, 201):
    setattr(self, f'A{i}__rubbing_marks_Type',  tk.StringVar(self.A_Frame_measure))
    setattr(self, f'A{i}_choice_rubbing_marks',  ['No', 'Yes'])
    getattr(self, f'A{i}_rubbing_marks_Type').set('')
    setattr(self, f'A{i}_Menu_rubbing_marks',
            tk.OptionMenu(self.A_Frame_measure, 
                          getattr(self, f'A{i}_rubbing_marks_Type'), 
                          *getattr(self, f'A{i}_choice_rubbing_marks'), 
                          command=self.show_rubbing_marks_borders))
    getattr(self, f'A{i}_Menu_rubbing_marks').config(width=4)    
    getattr(self, f'A{i}_Menu_rubbing_marks').grid_forget()

If f-strings are not available in the version of Python you are using, simply replace them with .format(i).




回答2:


Remove the prefix from your variables, and then create dictionaries instead with the prefix as the key.

For example, self.A1_choice_rubbing_marks becomes self.choice_rubbing_marks["A1"], etc.

Do that for each variable. Don't try to use dictionary comprehensions until you figure out how to do it with simple loops.

For example:

self.choice_rubbing_marks = {}
self.rubbing_marks_Type = {}
self.Menu_rubbing_marks = {}
self.Frame_measure = {}

for i in range(200):
    key = f"A{i}"  # => A0, A1, ..., A199
    self.rubbing_marks_Type[key] = tk.StringVar(self.A_Frame_measurevalue='')
    self.Frame_measure[key] = tk.StringVar()
    self.choice_rubbing_marks[key] =  ['No', 'Yes']
    self.rubbing_marks_Type[key].set('') # set the default option
    self.Menu_rubbing_marks[key] = tk.OptionMenu(
        self.A_Frame_measure, 
        self.rubbing_marks_Type[key], *self.choice_rubbing_marks[key],
        command=self.show_rubbing_marks_borders
    )
    self.Menu_rubbing_marks[key].config(width=4)    
    self.Menu_rubbing_marks[key].grid_forget()


来源:https://stackoverflow.com/questions/65690937/how-to-use-loop-to-reduce-the-the-code-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!