【问题标题】:Why does Python say that a function that I have defined isn't defined? [closed]为什么 Python 说我定义的函数没有定义? [关闭]
【发布时间】:2016-09-19 09:39:29
【问题描述】:

我使用 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'))

【问题讨论】:

  • 您需要在win.butt1.configure(win.root, text = 'Continue', font = ('MS Serif', 12, 'bold') )#<--add this 行中添加另一个右括号。
  • 这看起来不是有效的 Python 代码,因为在 class win(): 行之后没有缩进
  • 我尝试将 GameIntro 放在类上方,但它说 text1 和 butt1 等变量不起作用

标签: python class tkinter


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    相关资源
    最近更新 更多