python tkinter执行问题

2024-10-01 11:35:12 发布

您现在位置:Python中文网/ 问答频道 /正文

总共60个tkinter条目,命名为e0、e1….e59,60个标签l0、l1….l59。函数可以这样工作。 版本一:

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”方法,那么另一个函数就无法提取全局变量e0 e1….e59来收集输入。 第二版:

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”窗口中显示良好,但当其他函数调用此函数时,错误为“NameError:name'e0'is not defined”。但是我在全球范围内定义了它们,在一个版本中它们可以正常工作。请给我建议。你知道吗

调用函数如下:

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()

Tags: indexcolumnrootwidthtkgridrowexec
2条回答

您不应该使用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],等等)

这可能就是问题所在;在带有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和作用域的问题,但我真的不明白为什么。你知道吗

我的建议是将条目存储在一个列表或dict中,而不是像Bryan建议的那样。你知道吗

相关问题 更多 >