如何使用循环减少python中的代码 [英] how to use loop to reduce the the code in python

查看:34
本文介绍了如何使用循环减少python中的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 tkinter 中创建很多小部件.我目前为此使用了很多代码.有没有办法总结代码?但是,我仍然希望能够捕获每个小部件的价值.编号增加到 200...非常感谢!

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)

'''

推荐答案

从变量中删除前缀,然后以前缀为键创建字典.

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

例如,self.A1_choice_rubbing_marks 变成 self.choice_rubbing_marks[A1"]

对每个变量都这样做.在你弄清楚如何用简单的循环来做之前,不要尝试使用字典推导式.

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

例如:

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

这篇关于如何使用循环减少python中的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆