首页 > 解决方案 > PYTHON FUNCTIONS UnboundLocalError:分配前引用的局部变量“值”

问题描述

我想携带一个价值,无论我做什么都不会。这只是代码片段,但我希望这里有足够的,谢谢

def create_window2(value):
    print(value)
    window2 = Tk()
    window2.title("Choose Quantity")
    L1 = Label(window2, text="How many ml of"+value+"would you like?")
    S1 = Spinbox(window2, from_=0, to=10000000)
    button1 = Button(window2, text='continue', command= lambda: create_window3(value,S1.get()))
    #listbox.insert(END, S1,"bottles will be scheduled to be made once equipment has been found")
    L1.pack()
    S1.pack()
    button1.pack()


def nextup(up):
    if up == '1':
        value = "Pilsner"
    elif up == '2':
        value = "Dunkel"
    elif up == '3':
        value = "Red Helles"
    print(value)
    create_window2(value)

def sel(var):
   selection = str(var.get())
   nextup(selection)
def chooser():
    ch_beer = Tk()
    var = IntVar()
    R1 = Radiobutton(ch_beer, text="Pilsner", variable=var, value=1,
                      command=lambda:sel(var))
    R1.pack( anchor = W )

    R2 = Radiobutton(ch_beer, text="Dunkel", variable=var, value=2,
                      command=lambda:sel(var))
    R2.pack( anchor = W )

    R3 = Radiobutton(ch_beer, text="Red Helles", variable=var, value=3,
                      command=lambda:sel(var))
    R3.pack( anchor = W)
    ch_beer.mainloop()

此处给出错误 print(value) UnboundLocalError: local variable 'value' referenced before assignment

标签: pythonfunctiontkinter

解决方案


推荐阅读