首页 > 解决方案 > 使用 exec 定义变量并从变量中获取值会引发 NameError

问题描述

现在我正在做一个项目,该项目将询问跑步者的姓名、1 英里、2 英里和 5 英里的时间,并据此生成数据。要求是为 7 个跑步者提供足够的条目,但我想我会添加通过使用添加任意数量的跑步者的能力exec()。但是,当尝试从其中定义的变量中读取数据时,我得到 aNameError: name 'entry2' is not defined但不是 for entry0and entry1,所以我想知道是否有人可以帮助我解决这些问题。如果有帮助,我不一定需要使用exec()它来创建它们,但我知道它可以定义变量并稍后从它们中读取。

所有代码:

from UsefulFunctions import setup_screen, clear_window # just some of my functions, pretty obvious what they do
from tkinter import *

class Application(Frame):
    def __init__(self, master):
        Frame.__init__(self,master)
        self.grid()
        self.create_widgets()
        
    def create_widgets(self):
        Label(self,text='Enter how many runners you\' entering times for:').grid()
        numOfRunners=Entry(self);numOfRunners.grid()
        var = IntVar()
        test = Button(self, text = "Submit", command = lambda: var.set(1)); test.grid()
        while True:
            try:
                test.wait_variable(var)
                numOfRunners=int(numOfRunners.get())
                break
            except ValueError:
                Label(self,text='Make sure that you use a number like \'4\', not \'four\'!').grid(row=3)
        clear_window(self)
        
        Label(self,text='Input').grid(row=0,column=0,sticky=W)
        for i in range(numOfRunners):
            columnNum=0
            Label(self,text='Runner Name').grid(row=(i*2)+1,column=0,sticky=W)
            Label(self,text='1 Mile Mark Time').grid(row=(i*2)+1,column=1,sticky=W)
            Label(self,text='2 Mile Mark Time').grid(row=(i*2)+1,column=2,sticky=W)
            Label(self,text='5k Time').grid(row=(i*2)+1,column=3,sticky=W)
            for col in range(4):
                exec('entry%s=Entry(self);entry%s.grid(row=%s,column=%s)' % (str(i),str(i),str((i*2)+2),str(col)))
        runnerNames=[]
        oneMile=[]
        twoMile=[]
        fiveK=[]
        var = IntVar()
        submitButton = Button(self, text = "Submit", command = lambda: var.set(1)); submitButton.grid(sticky=W)
        submitButton.wait_variable(var)
        
        for i in range(numOfRunners*4):
            exec("print('entry%s.get() = ' + entry%s.get())" % (i,i))
        
        self.calculate(runnerNames,oneMile,twoMile,fiveK)
        
    def calculate(self,runnerNames,oneMile,twoMile,fiveK):
        print(runnerNames)
        print(oneMile)
        print(twoMile)
        print(fiveK)
        
root =Tk()
root.title("AT Pace Check Times")
setup_screen(root, 800, 400)
app = Application(root)
root.mainloop()

这是完整的错误日志:

Traceback (most recent call last):
  File "/Users/122224/Desktop/Computer Programming/Python 3.x/Assignments/Programming 2/8/9_15 Computing 5k Mile Splits/main.py", line 70, in <module>
    app = Application(root)
  File "/Users/122224/Desktop/Computer Programming/Python 3.x/Assignments/Programming 2/8/9_15 Computing 5k Mile Splits/main.py", line 12, in __init__
    self.create_widgets()
  File "/Users/122224/Desktop/Computer Programming/Python 3.x/Assignments/Programming 2/8/9_15 Computing 5k Mile Splits/main.py", line 48, in create_widgets
    exec("print('entry%s.get() = ' + entry%s.get())" % (i,i))
  File "/Users/122224/Desktop/Computer Programming/Python 3.x/Assignments/Programming 2/8/9_15 Computing 5k Mile Splits/main.py", line 1, in <module>
    try:
NameError: name 'entry2' is not defined

标签: pythonpython-3.xtkinterexectkinter-entry

解决方案


万一有人在乎,那是因为我使用的是较旧的变量i,并且没有将其设置为0或使用另一个变量。


推荐阅读