我目前正在使用Python 2.7在Tkinter制作扑克游戏的图形部分,但我现在无法重绘画布。出于某种原因,self.canvas.delete(ALL)
不会删除我的按钮或背景图片标签。我在我的pack_forget()
函数中尝试了redrawALL()
,但这似乎也没有用。
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
答案 0 :(得分:1)
Canvas没有self.canvas.create_*()
(http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/canvas.html)
可能你想删除按钮。
请勿同时使用.pack()
,.place()
,.grid()
。他们是三个不同的布局管理员。
使用.pack()
或.place()
或.grid()
您已使用.place()
元素,因此必须使用.place_forget()
工作代码:
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()