Python-函数中的变量未定义

时间:2019-06-02 19:44:22

标签: function tkinter undefined python-3.7

我正在尝试模拟彩票,但我的功能有一些问题。这是我想要做的:

  1. 运行window1()
  2. window1()-> destroy_window1()-> window2()
  3. window2()->销毁window2或retry()-> window1()

当我到达destroy_window1()时出现错误,我得到以下消息:“ NameError:未定义名称'e1'”。我该如何解决这个问题?

我读到您应该在函数之外预定义变量。因此,我尝试仅将e1 = 1等,但随后出现消息:“ AttributeError:'int'对象没有属性'get'”。由于它是一个条目,所以我不知道如何对其进行预定义。

from tkinter import*
import random

Part1 = list(range(1,51))
Part2 = list(range(1,11))

Numbers = [0]*7

for n in range (5):
    Number = random.choice(Part1)

    Position = Part1.index(Number)
    del Part1[Position]

    Numbers[n] = Number


for i in range (2):
    Number = random.choice(Part2)

    Position = Part2.index(Number)
    del Part2[Position]

    Numbers[5+i] = Number

print (Numbers)


def destroy_window1():
    global Guess
    Guess = [e1.get(), e2.get(), e3.get(), e4.get(), e5.get(), e6.get(), e7.get()]
    master1.destroy()
    window2()

def retry():
    master2.destroy()
    window1()

def window1():

    master1 = Tk()
    master1.title('Lottery')
    Label(master1, text="Guess numbers:").grid(row=0)

    e1 = Entry(master1, width=2)
    e2 = Entry(master1, width=2)
    e3 = Entry(master1, width=2)
    e4 = Entry(master1, width=2)
    e5 = Entry(master1, width=2)
    e6 = Entry(master1, width=2)
    e7 = Entry(master1, width=2)


    e1.grid(row=0, column=1, padx=5)
    e2.grid(row=0, column=2, padx=5)
    e3.grid(row=0, column=3, padx=5)
    e4.grid(row=0, column=4, padx=5)
    e5.grid(row=0, column=5, padx=5)
    e6.grid(row=0, column=7, padx=5)
    e7.grid(row=0, column=8, padx=5)

    master1.grid_columnconfigure(6, minsize=20) # Creates an empty column (nr. 6) with width 20

    Button(master1, text='OK', command=destroy_window1).grid(row=3, column=3, sticky=W, pady=5)



    master1.mainloop()


def window2():

    master2 = Tk()
    master2.title('Check results')
    Label(master2, text="Drawn numbers:").grid(row=0, column=0, sticky=W)
    Label(master2, text="Your numbers:").grid(row=1, column=0, sticky=W)

    for n in range (7):
        Label(master2, text=Numbers[n]).grid(row=0, column=n+1, sticky=W, padx=5)

        if str(Numbers[n]) == Guess[n]:
            Label(master2, text=Guess[n], bg="green").grid(row=1, column=n+1, sticky=W, padx=5)
        else:
            Label(master2, text=Guess[n], bg="red").grid(row=1, column=n+1, sticky=W, padx=5)

    Button(master2, text='Quit', command=master2.destroy).grid(row=3, column=3, sticky=W, pady=5)
    Button(master2, text='Retry', command=retry).grid(row=3, column=4, sticky=W, pady=5)

    master2.mainloop ()

window1()

我不能投票,谢谢!

2 个答案:

答案 0 :(得分:1)

  

当我到达destroy_window1()时出现错误,我得到以下消息:“ NameError:未定义名称'e1'”。我该如何解决这个问题?

问题在于destroy_window1()函数不知道e1变量,因为e1是在window1()函数中定义的(并且不是全局变量)。

一个简单的解决方法是将所有e变量放入list并将该list作为参数传递给destroy_window1()函数。使用简单的for循环创建列表,这不仅可以解决您的问题,而且还可以使您的代码更简洁,更易于阅读,并且将来更容易更改其功能。

像这样:

def destroy_window1(e_list):
    global Guess
    Guess = []

    for e_item in e_list:
        Guess.append(e_item.get())

    master1.destroy()
    window2()

def window1():

    master1 = Tk()
    master1.title('Lottery')
    Label(master1, text="Guess numbers:").grid(row=0)

    e_list = []

    for i in range(7):
        temp_e = e1 = Entry(master1, width=2)
        temp_e.grid(row=0, column=i, padx=5)
        e_list.append(temp_e)

    master1.grid_columnconfigure(6, minsize=20) # Creates an empty column (nr. 6) with width 20

    Button(master1, text='OK', command=lambda :destroy_window1(e_list)).grid(row=3, column=3, sticky=W, pady=5)

    master1.mainloop()

此解决方案的一部分涉及lambda函数。这是因为(您可能已经注意到),命令选项通常不能接受函数的参数。使用lambda函数可以实现这一点。 (在Lambda Functions Here上阅读)

答案 1 :(得分:-1)

设置e1=1时,会将变量e1设置为1,但是.get()函数不适用于整数,因为尚未定义该函数。