如何访问动态变量?

时间:2020-09-16 14:34:04

标签: python oop tkinter

我已经在tkinter项目中创建了动态变量,但是我无法访问它,我该怎么做?

dict_ndfitemcostcategory = {}
for ndicc in range(2, 133):
    # self.neighborhood_details_item_cost_category_entry_2  - starting point
    dict_ndfitemcostcategory["self.neighborhood_details_item_cost_category_entry" + "_" + str(ndicc)] = tk.Text(self.frame, height=2, width=25, bg='alice blue', wrap=tk.WORD)
    dict_ndfitemcostcategory["self.neighborhood_details_item_cost_category_entry" + "_" + str(ndicc)].grid(row=ndicc, column=3, padx=5, pady = 4)

我试图跑步:

self.neighborhood_details_item_cost_category_entry_2.insert(tk.END, "asd")

但是似乎找不到它: AttributeError: 'Neighborhood_Details' object has no attribute 'dict_ndn'

1 个答案:

答案 0 :(得分:1)

您没有定义动态变量;您只定义了一个普通的self.dict_ndfitemcostcategory = {} for ndicc in range(2, 133): key = "neighborhood_details_item_cost_category_entry" + "_" + str(ndicc) self.dict_ndfitemcostcategory[key] = tk.Text(self.frame, height=2, width=25, bg='alice blue', wrap=tk.WORD) self.dict_ndfitemcostcategory[key].grid(row=ndicc, column=3, padx=5, pady = 4) ,当函数返回时,它不在范围内。字典 itself 应该是一个实例属性,并以所需的“动态”属性作为键。

self.dict_ndfitemcostcategory["neighborhood_details_item_cost_category_entry_2"].insert(tk.END, "asd")

然后

create table example (a TIMESTAMP_NTZ);
insert into example (a) values (current_timestamp);
select * from example;