在按钮中调用命令不起作用

时间:2017-04-18 21:27:50

标签: 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 = "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)

当我运行这个(以及其余的)时,我会看到“Play is not defined”。谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

您需要使用全名:

button1 = Button(text = "Play", command=self.play)

仅此一项就会给你一个新的错误,因为' play'期待一个"大师"论点。您需要将播放功能更改为仅需要单个参数:

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