我在输入框上的输入中创建变量时遇到问题

时间:2016-06-29 02:42:14

标签: python tkinter

基本上,我正在尝试制作Mad 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()

中的 root

1 个答案:

答案 0 :(得分:1)

在您的函数 mlib 之外声明您的变量,例如MN1,并且根据您的代码,您没有从eBox Entry小部件中获取任何输入条目,但是您从未知变量中获取 v