为什么Python说我定义的函数没有定义?

时间:2016-05-22 13:50:21

标签: python class tkinter

我一直在使用Python(2.7)一段时间,但刚开始使用类。它告诉我GameIntro没有定义。我一直试图修复它,但它不想工作:

import random

from Tkinter import *
import time

class win():
root = Tk()

root.wm_title("HauntFall")#Title of window

text1 = Label(root, text = "HAUNTFALL", font = ('MS Serif', 50, 'bold'), fg = "white", bg = "black")
text1.pack()#This one and ^ that one make the big lettered HauntFall.

butt1 = Button(root, text = "PLAY", font = ('MS Serif', 15, 'bold'), fg = 'red', command = GameIntro)
butt1.pack(pady = 3)#These two make the PLAY button.



root.configure(bg='black')#This one sets the background to black.

root.mainloop()#Puts the window in a loop, so it doesn't immediately close.

def GameIntro():
    x = win()
    print "Gj"
    x.text1.configure( font = ('MS Serif', 12, 'bold'),text = "You approach a house abandoned many years ago - though you can hear faint creaking, and various other noises coming from inside it's shadowy depths. You approach the large wooden doors, and stare up at all 3 stories of the horrifing place. The house has always been a mystery, and although you are the curious type, you have been hesitant to go in. You promise yourself not to succumb to your fears, and you pull open the old wooden doors.", wraplength = 500)
    x.butt1.configure( text = 'Continue', font = ('MS Serif', 12, 'bold'))

1 个答案:

答案 0 :(得分:0)

直到mainloop退出后才定义该函数,这意味着在窗口存在时未定义该函数。在调用函数的代码执行之前,你必须在某处def GameIntro