Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

tkinter - how to use loop to reduce the the code in python

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)

'''


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...