-17
在一本書中發現了這個練習來學習使用Python 3編程...我必須創建一個派生自Frame的新App類。它必須顯示一張臉和2個按鈕,一個用來繪製一個張開的嘴,另一個用來繪製一條線(打開和關閉嘴 - 小操作)。刪除方法意外的結果
下面是我做的,它幾乎像它應該工作:打開按鈕工作正常,如果有一條線(閉嘴),它刪除它,但封閉的嘴巴按鈕繪製的行,而不刪除打開嘴,雖然它看起來像我使用完全相同的刪除方法來照顧... 我的問題:爲什麼它是爲一個按鈕而不是另一個按鈕?你會得到相同的結果嗎?
class Application(Frame):
"main canvas and buttons"
def __init__(self, boss =None):
Frame.__init__(self)
self.can = Canvas(self, width=400, height =400, bg ='ivory')
self.can.pack(side =TOP, padx =5, pady =5)
self.face=Visage(self.can, 50, 50)
self.bouche=2
Button(self, text ="Ouvrir", command =self.ouvrirBouche).pack(side =LEFT)
Button(self, text ="Fermer", command =self.fermerBouche).pack(side =LEFT)
def ouvrirBouche(self):
"draws the open mouth and delete the closed one if any"
if (self.bouche != 0):
self.ouvre=cercle(self.can, 200, 260, 35)
if (self.bouche ==1):
print(self.bouche)
self.can.delete(self.ferme)
self.bouche=0
def fermerBouche(self):
"draws the closed mouth and delete the open one if any"
if (self.bouche != 1):
self.ferme= self.can.create_line(170, 260, 230, 260)
if (self.bouche ==0):
print(self.bouche)
self.can.delete(self.ouvre)
self.bouche=1
class Visage(object):
"drawing a face in canvas canv"
def __init__(self, canv, x, y):
self.canv, self.x, self.y = canv, x, y
cercle(canv, x+150, y+150, 130)
cercle(canv, x+100, y+100, 20)
cercle(canv, x+200, y+100, 20)
if __name__ == '__main__':
root=Tk()
app=Application(root)
app.pack(side=TOP)
root.mainloop()