当前位置: 代码迷 >> python >> Choice1未定义错误
  详细解决方案

Choice1未定义错误

热度:79   发布时间:2023-06-21 11:01:47.0

有关功能有错误。 我正在尝试将在其他函数中定义然后返回的变量写入文本文件。 它给出了错误:

Traceback (most recent call last): 
    File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/__init__.??py", line 1475, in __call__
        return self.func(*args) 
    File "/Users/MaxBookPro/Desktop/Cake Order.py", line 20, in text 
        ck.write(choice1) 
NameError: global name 'choice1' is not defined

任何帮助将不胜感激,因为我是python的新手。

from tkinter import *
import random
import time

tk = Tk()
tk.title("Cakes of Destiny") # Title of the window
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
canvas1 = Canvas(tk, width=305, height=370, bd=0, highlightthickness=0)
canvas1.configure(background='#9EE6FF')
canvas1.pack()

def order():

    def text():
       ck = open('orders.txt', 'a')
       ck.write('=NEW ORDER=')
       ck.write('\n')
       ck.write(choice1)
       ck.write('\n')
       ck.write(name)
       ck.write('\n')
       ck.write(detail)
       ck.write('\n')
       ck.close()

    def next2():
        entry2 = Entry(tk, highlightbackground ='#9EE6FF')
        entry2.pack()
        entry2.place(x=70, y=255)
        button8 = Button(tk, text = "Enter Customer Details", command = text, highlightbackground='#9EE6FF')
        button8.pack()
        button8.place(x=70, y = 315)
        detail = entry2.get()
        return detail

    def next1():
        entry1 = Entry(tk, highlightbackground ='#9EE6FF')
        entry1.pack()
        entry1.place(x=70, y=255)
        button7 = Button(tk, text = "Enter Customer Name", command = next2, highlightbackground='#9EE6FF')
        button7.pack()
        button7.place(x=75, y = 315)
        name = entry1.get()
        return name

    def choice():
        woweeeee = 0
        if entry.get() == 'Chocolate Cake':
            next1()
            woweeeee = 1
            choice1 = 'Chocolate Cake'
            return choice1
        if entry.get() == 'Wedding Cake':
            next1()
            woweeeee = 2
            choice1 = 'Wedding Cake'
            return choice1
        if entry.get() == 'Carrot Cake':
            next1()
            woweeeee = 3
            choice1 = 'Carrot Cake'
            return choice1
        if entry.get() == 'Christening Cake':
            next1()
            woweeeee = 4
            choice1 = 'Christening Cake'
            return choice1
        if woweeeee == 0:
            result = messagebox.showwarning("Error!","There is no cake by that name!")

    button4.destroy()
    button5.destroy()
    entry = Entry(tk, highlightbackground ='#9EE6FF')
    entry.pack()
    entry.place(x=70, y=255)
    button6 = Button(tk, text = "Enter Cake Choice", command = choice, highlightbackground='#9EE6FF')
    button6.pack()
    button6.place(x=85, y = 315)

def quityay():
    tk.destroy()

wowee = Label(tk, text='Chelsea', fg="white", bg="#9EE6FF", font=("Dense",70))
wowee.pack()
wowee.place(x=80, y = 5)
wowee2 = Label(tk, text='Bun', fg="white", bg="#9EE6FF", font=("Dense",60))
wowee2.pack()
wowee2.place(x=120, y = 74)
wowee3 = canvas1.create_rectangle(150, 5, 2, 2, fill='white', outline='white')
canvas1.move(wowee3, 77, 153)
wowee4 = Label(tk, text='Quality Cakes', fg="white", bg="#9EE6FF", font=("Cylburn",40))
wowee4.pack()
wowee4.place(x=70, y = 170)
button4 = Button(tk, text = "New Order", command = order, highlightbackground='#9EE6FF')
button4.pack()
button4.place(x=105, y = 255)
button5 = Button(tk, text = "Close", command = quityay, highlightbackground='#9EE6FF')
button5.pack()
button5.place(x=120, y = 315)

在函数中分配变量之前,应将其设置为全局变量:

def choice():
    global choice1
    woweeeee = 0
    if entry.get() == 'Chocolate Cake':
        next1()
        woweeeee = 1
        choice1 = 'Chocolate Cake'
        return choice1
    if entry.get() == 'Wedding Cake':
        next1()
        woweeeee = 2
        choice1 = 'Wedding Cake'
        return choice1
    if entry.get() == 'Carrot Cake':
        next1()
        woweeeee = 3
        choice1 = 'Carrot Cake'
        return choice1
    if entry.get() == 'Christening Cake':
        next1()
        woweeeee = 4
        choice1 = 'Christening Cake'
        return choice1
    if woweeeee == 0:
        result = messagebox.showwarning("Error!","There is no cake by that name!")

text()您尝试:

ck.write(choice1)

但是, choice1没有在该函数中定义。 最好的解决方案是将其(以及所需的其他变量)作为参数传递:

def text(choice1, name, detail):
    with open('orders.txt', 'a') as ck:
        ck.write('=NEW ORDER=')
        ck.write('\n')
        ck.write(choice1)
        ck.write('\n')
        ck.write(name)
        ck.write('\n')
        ck.write(detail)
        ck.write('\n')
  相关解决方案