python定义了函数却显示未定义_Python-函数中的变量未定义

1586010002-jmsa.png

I'm trying to simulate a lottery and have a little problem with my functions. Here's what I'm trying to do:

run window1()

window1() --> destroy_window1() --> window2()

window2() --> destroy window2 or retry() --> window1()

The error occurs when I reach destroy_window1(), where i get the following message: "NameError: name 'e1' is not defined". How can I solve this problem?

I've read that you should pre-define the variables outside the functions. So, I tried to just put e1 = 1 etc., but then i get the message: "AttributeError: 'int' object has no attribute 'get'". Since it's an entry, I don't know how to pre-define it.

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

I can't vote up yet, thanks in advance!

解决方案The error occurs when I reach destroy_window1(), where i get the following message: "NameError: name 'e1' is not defined". How can I solve this problem?

The problem is that the destroy_window1() function doesn't know about the e1 variable, because e1 is defined within the window1() function (and its not global).

A simple fix is to put all the e variables into a list and pass that list as an argument to the destroy_window1() function. Make the list with a simple for loop, this not only solves your problem, but it also makes your code cleaner, easier to read, and easier to change it's functionality in future.

Like so:

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

Part of this solution involves a lambda function. This is because (as you may have noticed) the command option normally can't take arguments for the functions. The use of a lambda functions makes this possible. (Read up on Lambda Functions Here)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值