python tkinter exec 问题

死的

总共 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”。但是我在全局定义了它们,并且在 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 建议的那样。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python Tkinter Checkbutton问题

来自分类Dev

Python - Tkinter CheckButton 问题

来自分类Dev

python tkinter问题运行命令

来自分类Dev

Tkinter Python的全局变量问题

来自分类Dev

Python Tkinter:调整框架大小的问题

来自分类Dev

来自Tkinter在Python中的绑定函数问题

来自分类Dev

(Python 3.4 Tkinter)按钮网格填充问题

来自分类Dev

Tkinter Python 3在MacOS上的缩放问题

来自分类Dev

python tkinter熊猫搜索功能的问题

来自分类Dev

Python Tkinter滚动条问题

来自分类Dev

Tkinter Python的全局变量问题

来自分类Dev

Python Tkinter:调整框架大小的问题

来自分类Dev

python中的Tkinter grid()对齐问题

来自分类Dev

(Python 3.4 Tkinter)IntVar / StringVar问题

来自分类Dev

Python Tkinter面向对象的编码问题

来自分类Dev

Python with tkinter,按钮之间的线条绘制问题

来自分类Dev

Matplotlib 和 Pyplot 的导入问题(Python / Tkinter)

来自分类Dev

Python/Tkinter Lambda 事件参数问题

来自分类Dev

Python Tkinter框架newby几何问题?

来自分类Dev

Python 3.“ exec”函数令人困惑的问题

来自分类Dev

找 。-exec -fgrep问题

来自分类Dev

grep和exec问题

来自分类Dev

Java exec()问题

来自分类Dev

在python 2.7中从tkinter导入ttk时出现问题

来自分类Dev

Python Tkinter-蛇头后面的蛇身问题

来自分类Dev

Python-在Tkinter中使用绑定功能的问题

来自分类Dev

Tkinter Python应用程序中的布局管理问题

来自分类Dev

在python中使用tkinter Photoimage / Tk使用Singleton模式的问题

来自分类Dev

类继承问题。Tkinter。Python 3.x