2016-06-29 41 views
1

基本上,我正在尝试创建疯狂的Lobs。我的麻烦是,当我从我的函数输入中创建一个变量时,我的语法说我没有定义MN1(我的第一个变量),这对我的其他人来说意味着相同。我在输入框上输入变量时遇到问题

from tkinter import * 

root = Tk() 
root.title("Mad Libs") 

root.geometry("500x600") 

x = 0 

inp = None 

def mLib(): 
global x 
if x == 0: 
    lbl1["text"]="Man's Name"#1 
    MN1 = raw_input(v.get()) 
elif x == 1: 
    lbl1["text"]="Occupation"#2 
    O1 = raw_input(v.get()) 
elif x == 2: 
    lbl1["text"]="Noun"#3 
    N1 = raw_input(v.get()) 
elif x == 3: 
    lbl1["text"]="Noun"#4 
    N2 = raw_input(v.get()) 
elif x == 4: 
    lbl1["text"]="Noun"#5 
    N3 = raw_input(v.get()) 
elif x == 5: 
    lbl1["text"]="Shape"#6 
    S1 = raw_input(v.get()) 
elif x == 6: 
    lbl1["text"]="Man's Name"#7 
    MN2 = raw_input(v.get()) 
elif x == 7: 
    lbl1["text"]="Verb"#8 
    V1 = raw_input(v.get()) 
elif x == 8: 
    lbl1["text"]="Woman's Name"#9 
    WN1 = raw_input(v.get()) 
elif x == 9: 
    lbl1["text"]="Body Part"#10 
    BP1 = raw_input(v.get()) 
elif x == 10: 
    lbl1["text"]="Verb"#11 
    V2 = raw_input(v.get()) 
elif x == 11: 
    lbl1["text"]="Noun"#12 
    N4 = raw_input(v.get()) 
elif x == 12: 
    lbl1["text"]="Noun"#13 
    N5 = raw_input(v.get()) 
elif x == 13: 
    lbl1["text"]="Restaurant Name"#14 
    RN1 = raw_input(v.get()) 
elif x == 14: 
    lbl1["text"]="Historic Monument"#15 
    HM1 = raw_input(v.get()) 
elif x == 15: 
    lbl1["text"]="Verb Ending In ED"#16 
    V3 = raw_input(v.get()) 
elif x == 16: 
    lbl1["text"]="Noun"#17 
    N6 = raw_input(v.get()) 
elif x == 17: 
    lbl1["text"]="Noun"#18 
    N7 = raw_input(v.get()) 
elif x == 18: 
    lbl1["text"]="Noun"#19 
    N8 = raw_input(v.get()) 
elif x == 19: 
    lbl1["text"]="Verb"#20 
    V4 = raw_input(v.get()) 
elif x == 20: 
    lbl1["text"]="Noun"#21 
    N9 = raw_input(v.get()) 
elif x == 21: 
    lbl1["text"]="Adjective"#22 
    A1 = raw_input(v.get()) 
elif x == 22: 
    lbl1["text"]="Adjective"#23 
    A2 = raw_input(v.get()) 
elif x == 23: 
    lbl1["text"]="Emotion"#24 
    E1 = raw_input(v.get()) 
elif x == 24: 
    lbl1["text"]="Verb Ending In Ing"#25 
    V5 = raw_input(v.get()) 
elif x == 25: 
    lbl1["text"]="Noun"#26 
    N10 = raw_input(v.get()) 
elif x == 26: 
    lbl1["text"]="Noun"#27 
    N11 = raw_input(v.get()) 
elif x == 27: 
    lbl1["text"]="Verb"#28 
    V6 = raw_input(v.get()) 
else: 
    print(Para % (MN1, O1, N1, N2, N3, S1, MN2, V1, WN1, BP1, V2, N4, N5, RN1, HM1, V3, N6, N7, N8, V4, N9, A1, A2, E1, V5, N10, N11, V6)) 

inp = MN1, O1, N1, N2, N3, S1, MN2, V1, WN1, BP1, V2, N4, N5, RN1, HM1, V3, N6, N7, N8, V4, N9, A1, A2, E1, V5, N10, N11, V6 



x = x+1 
return 

Para = '''%s is a normal %s. Then, one day, a %s explodes, causing a %s to blow up, and a nearby %s erupts into a %s of flames. 
%s realizes that he's being chased by the government, who's trying to %s him. While on the run, he teams up with an incredibly 
attractive woman named %s, who has an incredible %s. She may be from the streets, but she can %s like nobody's buisness. The 
duo decide to turn tables on their pursuers by blowing up a %s, which triggers a chain reaction, causing the local %s, %s, and 
%s to explode. Then, the bad guys' helicopter gets %s by a piece of %s from when the %s exploded, and the helicopter explodes 
and falls onto a %s, causing it to %s, which shoots a fireball straight into the heart of %s and destroys the bad guy leader. 
Everything is %s and the two decide that such a %s ordeal has caused them to fall in %s with each other. They decide to celebrate 
by %s on the %s,and they even managed to use a %s from the beginning of the movie, to %s the whole story together.''' 

print(Para % (MN1, O1, N1, N2, N3, S1, MN2, V1, WN1, BP1, V2, N4, N5, RN1, HM1, V3, N6, N7, N8, V4, N9, A1, A2, E1, V5, N10, N11, V6)) 



btn1 = Button(root,text="Enter", command=mLib) 
btn1.pack() 

lbl1 = Label(root, text="Lab") 
lbl1.pack() 
v = Entry(root).pack() 

root.mainloop() 

而且,当我的语法出现同时也突出root.mainloop()

+0

'MN1'只在f x为零时才被定义。只有当x是1时才定义'O1'。等等。 –

+0

即使这样,这些变量也只在mLib()内部定义。外部程序不知道它们。 –

回答

1

声明变量像MN1您的功能MLIB并以外基于您的代码您没有从您的eBox Entry小部件中接收任何输入项,但是您正在从未知变量中获取输入项v

+0

我希望在按下按钮时声明的变量 – PLP123

+0

在函数mlib之前声明变量,如'MN1,O1,N1 .......,V6 =“0”'。 –

+0

谢谢你的问题是固定的我在我的函数之前定义了每个像这样的'MN1 ='“'' – PLP123