简体   繁体   English

使用python中的循环从列表创建变量名

[英]create variable name from list using a loop in python

I am trying to generate a bunch of variables from a list, which will be called from another function. 我试图从列表中生成一堆变量,该列表将从另一个函数中调用。

If I create them separately(as in the commented part of the code), everything is working fine(by fine, I mean, I can access the date using get_data function listed below). 如果我分别创建它们(如代码的注释部分所示),则一切正常(很好,我的意思是,我可以使用下面列出的get_data函数访问日期)。 But, when I am using the loop, the get_data is giving error: 但是,当我使用循环时,get_data给出了错误:

File "main.py", line 97, in get_data
    dAu = self.Author.get_text()
AttributeError: 'MyWindow' object has no attribute 'Author'

So, it is obvious that inside the loop, EDIT I am expecting, for field[0], 所以,很明显,在循环中, 编辑我期待,现场[0]

self.field = self.field[0]=self.Author

but this is not the case. 但这种情况并非如此。 EDIT COMPLETE 编辑完成

self.field != self.Author

as I wished. 如我所愿。 How can I get that? 我该怎么办?

The code in question is: 有问题的代码是:

    #  Generate the Entry fields
    self.notebook = Gtk.Notebook()
    # self.LAuthor = Gtk.Label("Author")
    # self.EAuthor = Gtk.Entry()
    # self.page = Gtk.Grid()
    # self.page.attach(self.LAuthor, 0, 0, 2, 1)
    # self.page.attach_next_to(self.EAuthor, self.LAuthor, Gtk.PositionType.RIGHT, 1, 1)
    # self.notebook.append_page(self.page, Gtk.Label("Trial"))

    xpos = 0
    minf = 0
    fields = ["Author",  "Year",  "Journal", "Title", "Publisher", "Page",
              "Address", "Annote", " Booktitle", "Chapter", "Crossred",
              "Edition", "Editor", "HowPublished", "Institution", "Month",
              "Note", "Number", "Organization", "Pages", "Publishers",
              "School", "Series", "Type"]
    Tabs = ["Essential", "Publishers", "Extra I", "Extra II"]
    for note in range(int(len(fields)/6)):
        ypos = 0
        self.npage = "page"+str(note)
        self.npage = Gtk.Grid()
        self.npage.set_border_width(10)
        maxf = minf+6
        for field in fields[minf:maxf]:
            print(field)
            self.lfield = "L" + field
            self.lfield = Gtk.Label(field)
            self.field = Gtk.Entry()
            self.field.set_placeholder_text(field)
            self.npage.attach(self.lfield, xpos, ypos, 2, 1)
            self.npage.attach_next_to(self.field, self.lfield,
                                      Gtk.PositionType.RIGHT, 1, 1)
            ypos += 1

        self.notebook.append_page(self.npage, Gtk.Label(Tabs[note]))
        minf = maxf

And the get_data function is just: get_data函数就是:

def get_data(self, widget):
    dAu = self.Author.get_text()
    print(dAu)

Kindly help. 请帮助。

Use dictionary - for example: 使用字典-例如:

self.all_fields = dict()

field = "Author"
self.all_fields[field] = ...
# self.all_fields["Author"] = ...

and then you can use 然后你可以使用

def get_data(self, widget):
    dAu = self.all_fields["Author"].get_text()
    print(dAu)

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

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