Python 3.x 在按钮中调用命令不起作用

Python 3.x 在按钮中调用命令不起作用,python-3.x,tkinter,Python 3.x,Tkinter,所以我正在制造小行星,而且它还处于早期阶段。我有一个按钮,但命令函数不调用同一类中的函数。代码如下: class parentWindow(): def play(self,master): print("PARTY") def __init__(self,master): self.master=master self.master.title("Asteroids") self.background_image = PhotoImage(file = "

所以我正在制造小行星,而且它还处于早期阶段。我有一个按钮,但命令函数不调用同一类中的函数。代码如下:

class parentWindow():

def play(self,master):
    print("PARTY")

def __init__(self,master):
    self.master=master
    self.master.title("Asteroids")

    self.background_image = PhotoImage(file = "Stars.gif")
    self.canvas = Canvas(self.master, width = 1920, height = 1080, bg = "black")
    self.canvas.image = self.background_image
    self.canvas.pack(expand = YES, fill = "both")

    self.canvas.create_image(0,0, image= self.background_image, anchor = NW)

    label1 = Label(text="ASTEROIDS", fg="white", bg="black", height=3, width=10)
    label1.config(font=("arcadeclassic", 50))
    label1.place( x=775, y=200)

    button1 = Button(text = "Play", command= play)
    button1.place( x=900, y=600)

当我运行这个程序(以及其他程序)时,我会听到“游戏没有定义”。谢谢你的帮助

您需要使用全名:

button1 = Button(text = "Play", command=self.play)
这本身就会给你一个新的错误,因为“play”需要一个“master”参数。您需要将play函数更改为仅需要单个参数:

def play(self):
    print("PARTY")

你的凹痕好像破了<代码>播放和
初始化
需要缩进到
语句中。