Christopher Liu
Christopher Liu

Reputation: 103

canvas.delete(ALL) not working

I'm currently making the graphical portion of a poker game in Tkinter with Python 2.7, but I'm having trouble with redrawing the canvas at the moment. For some reason self.canvas.delete(ALL) won't delete my buttons or background image label. I've tried pack_forget() in my redrawALL() function but that doesn't seem to be working either.

from Tkinter import *

class PokerGame(object):
    def __init__(self):
        self.run()

    def run(self):
        self.root = Tk()
        self.root.title("PyPoker")
        self.width = 1280
        self.height = 720
        self.canvas = Canvas(self.root, width = self.width, height = self.height)
        self.canvas.pack()
        self.drawStartScreen()
        self.root.mainloop()

    def drawStartScreen(self): 
        self.bgImage = PhotoImage(file = 'startbg.gif')
        self.bgLabel = Label(self.root, image = self.bgImage)
        self.bgLabel.pack()
        self.bgLabel.place(x = 0, y = 0, relwidth = 1, relheight = 1)
        self.btnPlay = Button(self.root, text = "Play", command = self.playGame)
        self.btnInstructions = Button(self.root, text = "How To Play", command = self.showInstructions)
        self.btnPlay.pack()
        self.btnInstructions.pack()
        self.btnPlay.place(relheight = 0.1, relwidth = 0.1, relx = 0.5, rely = 0.4, anchor = CENTER)
        self.btnInstructions.place(relheight = 0.1, relwidth = 0.1, relx = 0.5, rely = 0.5, anchor = CENTER)
        return

    def playGame(self):
        self.redrawAll()
        return

    def showInstructions(self):
        self.redrawAll()
        return


    def redrawAll(self):
        print "trying to delete all"
        self.canvas.delete(ALL)
        self.btnPlay.pack_forget()
        self.btnInstructions.pack_forget()
        return

Upvotes: 0

Views: 5416

Answers (1)

furas
furas

Reputation: 142651

Canvas has no element added by self.canvas.create_*() (http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/canvas.html)

Probably you want to remove buttons.

Do NOT use .pack(), .place(), .grid() at the same time. They are three different layout managers.

Use .pack() OR .place() OR .grid()

You have used .place() for your elements so you have to use .place_forget()

Working code:

from Tkinter import *

class PokerGame(object):
    def __init__(self):
        self.run()

    def run(self):
        self.root = Tk()
        self.root.title("PyPoker")
        self.root.geometry("1280x720")
        #self.width = 1280
        #self.height = 720
        #self.canvas = Canvas(self.root, width = self.width, height = self.height)
        #self.canvas.pack()

        self.drawStartScreen()
        self.root.mainloop()

    def drawStartScreen(self): 
        self.bgImage = PhotoImage(file = 'startbg.gif')

        self.bgLabel = Label(self.root, image = self.bgImage)
        self.bgLabel.place(x = 0, y = 0, relwidth = 1, relheight = 1)

        self.btnPlay = Button(self.root, text = "Play", command = self.playGame)
        self.btnPlay.place(relheight = 0.1, relwidth = 0.1, relx = 0.5, rely = 0.4, anchor = CENTER)

        self.btnInstructions = Button(self.root, text = "How To Play", command = self.showInstructions)
        self.btnInstructions.place(relheight = 0.1, relwidth = 0.1, relx = 0.5, rely = 0.5, anchor = CENTER)

    def playGame(self):
        self.redrawAll()

    def showInstructions(self):
        self.redrawAll()

    def redrawAll(self):
        print "trying to delete all"
        self.btnPlay.place_forget()
        self.btnInstructions.place_forget()
        self.bgLabel.place_forget()

PokerGame()

Upvotes: 1

Related Questions