Grey Wolnick
Grey Wolnick

Reputation: 17

Calling for a command in a button not working

So I am making Asteroids and it is in its early stages. I have a button but the command function doesn't call for the function in the same class. Here is the code:

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)

When I run this(and the rest) I am greeted with "Play is not defined". Thanks for the help!

Upvotes: 1

Views: 19

Answers (1)

Novel
Novel

Reputation: 13729

You need to use the full name:

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

That alone will give you a new error, since 'play' expects a "master" argument. You need to change the play function to only need the single argument:

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

Upvotes: 1

Related Questions