python tkinter 执行问题

python tkinter exec issue

总共 60 个名为 e0、e1....e59、60 标签 l0、l1....l59 的 tkinter 条目。该功能可以正常工作。 Version_one:

def CreateCharacter():
    root.destroy()
    chcreation = tk.Tk()
    global e0, e1,..... e59
    l0 = tk.Label(chcreation, text=char[0],  fg='black')
    l0.grid(row=0, column=0)
    e0 = tk.Entry(chcreation, fg='black')
    e0.grid(row=1, column=0)
    ...........
    e59 = tk.Entry(chcreation, fg='black')
    e59.grid(row=1, column=0)

但是如果我像下面那样切换到 "exec" 方法,其他函数将无法获取 global 变量 e0 e1....e59 来收集输入。 Version_two:

def CreateCharacter():
    root.destroy()
    chcreation = tk.Tk()
    chcreation.title('create new character')
    chcreation.geometry('1100x500+50+100')
    global e0, e1, ..... e59  
    for i in range(60):
        ro = (i // 11) * 2
        col = i % 11
        exec("l%s=tk.Label(chcreation,text=char[%d])" % (i,i))#char is a name list
        exec("l%s.grid(row=ro,column=col, padx=5, pady=2)" % i)
        exec("e%s=tk.Entry(chcreation, fg='black',width=12)" % i)
        exec("e%s.grid(row=ro+1,column=col, padx=5, pady=2)" % i)

标签和条目在'chcreation' window中显示良好,但当其他函数调用此函数时,错误为"NameError: name 'e0' is not defined"。但我在全球范围内定义了它们,在 Version_one 版本中它们工作正常。请指教

调用函数是这样的:

def Charater_creating(en):
    crole = []
    for i in range(60):
        exec("crole.append(e%s.get())"%i)
    raw_data = pd.DataFrame(crole, index=mul_index)
    raw_data.to_csv('char.csv',header=False,mode='a')
    for i in range(60):
        exec("e%s.delete(0,'end')"%i)

完整代码:

import pandas as pd
import tkinter as tk

array_index = [['Basic', 'Basic' ],['Name', 'Nickname']]
char = array_index[1]
mul_index = pd.MultiIndex.from_arrays(array_index)
role = pd.DataFrame([], index=mul_index)


def CreateCharacter():
    root.destroy()
    chcreation = tk.Tk()
    chcreation.geometry('1200x500+50+100')
    global e0, e1

    #error block
    for i in char:
        t = char.index(i)
        ro = (t // 11) * 2
        col = t % 11
        exec("l%s=tk.Label(chcreation,text=char[%d])" % (t, t))
        exec("l%s.grid(row=ro,column=col, padx=5, pady=2)" % t)
        exec("e%s=tk.Entry(chcreation, fg='black',width=12)" % t)
        exec("e%s.grid(row=ro+1,column=col, padx=5, pady=2)" % t)

    #below is the working block
    # l0 = tk.Label(chcreation, text=char[0], font=('Arial', 12), fg='black')
    # l0['height'] = 2
    # l0['width'] = 8
    # l0.grid(row=0, column=0)
    # e0 = tk.Entry(chcreation, font=('Arial', 10), fg='black')
    # e0['width'] = 8
    # e0.grid(row=1, column=0)
    # l1 = tk.Label(chcreation, text=char[1], font=('Arial', 12), fg='black')
    # l1['height'] = 2
    # l1['width'] = 8
    # l1.grid(row=0, column=1)
    # e1 = tk.Entry(chcreation, font=('Arial', 10), fg='black')
    # e1['width'] = 8
    # e1.grid(row=1, column=1)


    creat_button = tk.Button(chcreation, text='OK', font=('Arial', 8), fg='black')
    creat_button['width'] = 8
    creat_button['height'] = 2
    creat_button.grid(row=11, column=5)
    creat_button.bind('<Button-1>', Charater_creating)


def Charater_creating(en):
    crole = [e0.get(), e1.get()]
    raw_data = pd.DataFrame(crole, index=mul_index)
    raw_data.to_csv('character_data11.csv', header=False, mode='a')
    e0.delete(0, 'end')
    e1.delete(0, 'end')


root = tk.Tk()
root.geometry('900x400+200+50')
c_cbutn = tk.Button(root, text='new', font=('Arial', 20), fg='red', command=CreateCharacter)
c_cbutn['height'] = 2
c_cbutn['width'] = 15
c_cbutn.grid(row=3, column=1)
root.mainloop()

这可能是问题;在带有 exec 的代码中,您使用以下内容创建条目:

exec("e%s_Entry=tk.Entry(chcreation, fg='black',width=12)" % i)

分配名称:e0_Entry, e1_Entry, ... etc. 给小部件。所以名字 e0, e1, ... etc. 不会被定义。

补充想法

我发现了问题,但不知道为什么。考虑以下功能:

def go_a():
    global a
    a = 25
    print(a)

go_a() # This works just fine


def go_b():
    global b
    exec("b = 25")
    print(b)    

go_b() # Generates NameError: name 'b' is not defined

出于某种原因,使用 exec 函数的函数不会在全局命名空间中设置名称 "b",除非它已经存在于那里。见下文:

b = 9   # Setting the name "b"
go_b()  # As if by magic, this now works

所以这是 exec 和范围的问题,但我真的不明白为什么。

我的建议是按照 Bryan 的建议,将条目存储在列表或字典中。

您永远不应使用 exec 创建变量。它使代码几乎无法理解和维护。

如果您想创建大量小部件(或任何类型的对象),请将它们存储在列表或字典中。

widgets = []
for i in range(60):
    ro = (i // 11) * 2
    col = i % 11
    widget = tk.Label(chcreation,text=i)
    widgets.append(widget)

您现在可以使用 widgets[0]widgets[1] 等内容来引用每个标签。如果您的索引是一个整数并且从零开始,这很有效。

-或-

widgets = {}
...
widgets[i] = widget

当您的索引不是整数时,以上方法效果更好。与列表一样,您仍然以相同的方式引用小部件(例如:widgets[0],等)