简体   繁体   English

Python循环变量名称

[英]Python loop variable names

I am not sure what I am trying to do would be called so I am finding it difficult to find the answer to the following... 我不确定我要做什么,所以我发现很难找到以下答案...

I am deleting the contents of a number of tkinter entry widgets. 我正在删除许多tkinter条目小部件的内容。 I have four lines that look like I could replace with one line using a loop. 我有四行看起来可以用循环替换为一行。 I tried ... 我试过了 ...

for x in range(1, 5):
    self.entryplot_valx.delete(0, tk.END)

but I do not know how to feed the value in... Below is my code - I apologise for stupidity of this question 但我不知道该如何输入...下面是我的代码-对于这个问题的愚蠢表示歉意

    self.entryplot_val1.delete(0, tk.END)
    self.entryplot_val2.delete(0, tk.END)
    self.entryplot_val3.delete(0, tk.END)
    self.entryplot_val4.delete(0, tk.END)

You can make this work by changing your loop iterable to a tuple of these four entries: 您可以通过将循环迭代更改为以下四个条目的元组来完成此工作:

entries = (self.entryplot_val1,
           self.entryplot_val2,
           self.entryplot_val3,
           self.entryplot_val4)
for x in entries:
    x.delete(0, tk.END)

Since you don't want to create this tuple every time, let's move it into object initialization: 由于您不想每次都创建此元组,因此将其移至对象初始化中:

def __init__(self):
    # assuming you have a setup like the following
    master = Tk()
    self.entryplot_val1 = Entry(master)
    self.entryplot_val2 = Entry(master)
    self.entryplot_val3 = Entry(master)
    self.entryplot_val4 = Entry(master)
    # initialize your tuple to use in your for loop
    self.entries = (self.entryplot_val1,
                    self.entryplot_val2,
                    self.entryplot_val3,
                    self.entryplot_val4)
    # some further setup for entries
    self.entryplot_val1.pack()
    self.entryplot_val2.pack()
    self.entryplot_val3.pack()
    self.entryplot_val4.pack()

And then you can simplify this a bit further into: 然后,您可以进一步简化为:

def __init__(self):
    # assuming you have a setup like the following
    master = Tk()
    self.entries = (Entry(master), Entry(master), Entry(master), Entry(master))
    # some further setup for entries
    for x in self.entries:
        x.pack()

You can then use loops in the form of last example elsewhere in your class code. 然后,您可以在类代码中其他地方使用最后一个示例形式的循环。

Since you removed the previous identifiers for the entries, you would need to change your code to use the new tuple wherever they were used. 由于您删除了条目的先前标识符,因此需要更改代码以在使用新元组的任何地方使用它们。 That is, change references of self.entryplot_val1 to self.entries[0] , self.entryplot_val2 to self.entries[1] and so on. 即,改变的引用self.entryplot_val1self.entries[0] self.entryplot_val2self.entries[1]等。

You could solve this creating the attribute name as a string and using getattr(obj, attrname) , ie: 可以解决此问题,将属性名称创建为字符串并使用getattr(obj, attrname) ,即:

for x in range(1, 5):
    attrname = "entryplot_val{}".format(x)
    getattr(self, attrname).delete(0, tk.END)

But really when you find yourself using a "var1", "var2" (...) "varX" naming scheme you probably really want a list instead. 但是实际上,当您发现自己使用“ var1”,“ var2”(...)“ varX”命名方案时,您可能真的想要一个列表。

You can use get_attr() like that: 您可以像这样使用get_attr()

for x in range(1, 5):
    get_attr(self, "entryplot_val{}".format(x)).delete(0, tk.END)

See this part of the doc. 请参阅文档的这一部分

You can get the variable names using a string. 您可以使用字符串获取变量名称。

for x in range(1, 5):
    attr_name = f'entryplot_val{x}'
    getattr(self, attr).delete(0, tk.END)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM