2016-06-29 46 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