如何使用循环减少python中的代码

编程入门 行业动态 更新时间:2024-10-17 04:40:13
本文介绍了如何使用循环减少python中的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想在 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 06:09:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390670.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   代码   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!