2016-04-23 95 views
1

我的代碼創建一個Tk窗口,其中包含3個輸入框和3個按鈕。當點擊一個按鈕時,相鄰的輸入框和按鈕本身應該消失。我想用摧毀,因爲我不再使用它。問題在於它在任何按鈕/入口組件上都是第一次工作,但是當我在其中一個銷燬後單擊其他任何按鈕時,其他按鈕都不會銷燬。destroy()不會破壞重複

class BlackjackTable: 
    def __init__(self,numPlayers): 
     self.numPlayers=numPlayers 
     self.tableWindow=Tk() 
     self.bettedE=False 
     self.bettedS=False 
     self.bettedW=False 
     self.betCount=0 
     self.suitWindow() 
    def suitWindow(self): 
     self.tableWindow.title("Blackjack Table") 
     self.tableWindow.config(bg="dark green") 
     if int(self.betCount)!=int(self.numPlayers) and (not self.bettedE): 
      self.EHandEntry=Entry(self.tableWindow,fg="white",bg="dark green",width=10) 
      self.EHandEntry.grid(row=5,column=9) 

      self.EHandSubmit=Button(self.tableWindow,text="Bet",fg="white",padx=15,pady=5,command=self.betE,bg="dark green",activebackground="dark green",activeforeground="white") 
      self.EHandSubmit.grid(row=5,column=10) 
     if self.numPlayers>=2: 
      if (self.betCount!=self.numPlayers) and (not self.bettedS): 
       self.SHandEntry=Entry(self.tableWindow,fg="white",bg="dark green",width=10) 
       self.SHandEntry.grid(row=8,column=6) 

       self.SHandSubmit=Button(self.tableWindow,text="Bet",fg="white",padx=15,pady=5,command=self.betST,bg="dark green",activebackground="dark green",activeforeground="white") 
       self.SHandSubmit.grid(row=8,column=7) 
     if self.numPlayers==3: 
      if (self.betCount!=self.numPlayers) and (not self.bettedW): 
       self.WHandEntry=Entry(self.tableWindow,fg="white",bg="dark green",width=10) 
       self.WHandEntry.grid(row=5,column=3) 

       self.WHandSubmit=Button(self.tableWindow,text="Bet",fg="white",padx=15,pady=5,command=self.betW,bg="dark green",activebackground="dark green",activeforeground="white") 
       self.WHandSubmit.grid(row=5,column=4) 
    def betE(self): 
     print("betE") 
     self.bettedE=True 
     self.EHandSubmit.destroy() 
     self.EHandEntry.destroy() 
     self.suitWindow() 
     self.betCount+=1 
    def betST(self): 
     print("betST") 
     self.bettedS=True 
     self.SHandSubmit.destroy() 
     self.SHandEntry.destroy() 
     self.suitWindow() 
     self.betCount+=1 
    def betW(self): 
     print("betW") 
     self.bettedW=True 
     self.WHandSubmit.destroy() 
     self.WHandEntry.destroy() 
     self.suitWindow() 
     self.betCount+=1 
gui=BlackjackTable(3) 
mainloop() 

回答

1

您遇到這個問題,因爲這些方法裏面:betE()betST()betW()你叫白白suitWindow()其重繪基於複雜if條件componenents。

的解決方案是,你通過擺脫的基礎上,self.numPlayersself.bettedEself.bettedSself.bettedWself.betCount標誌的價值觀和國家所有的if條件繪圖使用suitWindow()您的小部件開始:

def suitWindow(self): 
     self.tableWindow.title("Blackjack Table") 
     self.tableWindow.config(bg="dark green") 

     self.EHandEntry=Entry(self.tableWindow,fg="white",bg="dark green",width=10) 
     self.EHandEntry.grid(row=5,column=9) 

     self.EHandSubmit=Button(self.tableWindow,text="Bet",fg="white",padx=15,pady=5,command=self.betE,bg="dark green",activebackground="dark green",activeforeground="white") 
     self.EHandSubmit.grid(row=5,column=10) 


     self.SHandEntry=Entry(self.tableWindow,fg="white",bg="dark green",width=10) 
     self.SHandEntry.grid(row=8,column=6) 

     self.SHandSubmit=Button(self.tableWindow,text="Bet",fg="white",padx=15,pady=5,command=self.betST,bg="dark green",activebackground="dark green",activeforeground="white") 
     self.SHandSubmit.grid(row=8,column=7) 
     self.WHandEntry=Entry(self.tableWindow,fg="white",bg="dark green",width=10) 
     self.WHandEntry.grid(row=5,column=3) 

     self.WHandSubmit=Button(self.tableWindow,text="Bet",fg="white",padx=15,pady=5,command=self.betW,bg="dark green",activebackground="dark green",activeforeground="white") 
     self.WHandSubmit.grid(row=5,column=4) 

一旦這些小部件被繪製出來,就像你一樣使用destroy(),並忘記了suitWindow()方法。因此,剩下的三個方法變成:

def betE(self): 
     print("betE") 
     self.bettedE=True 
     self.EHandSubmit.destroy() 
     self.EHandEntry.destroy() 

    def betST(self): 
     print("betST") 
     self.bettedS=True 
     self.SHandSubmit.destroy() 
     self.SHandEntry.destroy() 

    def betW(self): 
     print("betW") 
     self.bettedW=True 
     self.WHandSubmit.destroy() 
     self.WHandEntry.destroy() 

全部程序

你的程序變得那麼複雜爲你的代碼少:

''' 
Created on Apr 23, 2016 

@author: billal begueradj 
''' 
from tkinter import * 
class BlackjackTable: 
    def __init__(self,numPlayers): 
     self.numPlayers=numPlayers 
     self.tableWindow=Tk() 
     self.bettedE=False 
     self.bettedS=False 
     self.bettedW=False 
     self.suitWindow() 
    def suitWindow(self): 
     self.tableWindow.title("Blackjack Table") 
     self.tableWindow.config(bg="dark green") 

     self.EHandEntry=Entry(self.tableWindow,fg="white",bg="dark green",width=10) 
     self.EHandEntry.grid(row=5,column=9) 

     self.EHandSubmit=Button(self.tableWindow,text="Bet",fg="white",padx=15,pady=5,command=self.betE,bg="dark green",activebackground="dark green",activeforeground="white") 
     self.EHandSubmit.grid(row=5,column=10) 


     self.SHandEntry=Entry(self.tableWindow,fg="white",bg="dark green",width=10) 
     self.SHandEntry.grid(row=8,column=6) 

     self.SHandSubmit=Button(self.tableWindow,text="Bet",fg="white",padx=15,pady=5,command=self.betST,bg="dark green",activebackground="dark green",activeforeground="white") 
     self.SHandSubmit.grid(row=8,column=7) 
     self.WHandEntry=Entry(self.tableWindow,fg="white",bg="dark green",width=10) 
     self.WHandEntry.grid(row=5,column=3) 

     self.WHandSubmit=Button(self.tableWindow,text="Bet",fg="white",padx=15,pady=5,command=self.betW,bg="dark green",activebackground="dark green",activeforeground="white") 
     self.WHandSubmit.grid(row=5,column=4) 
    def betE(self): 
     print("betE") 
     self.bettedE=True 
     self.EHandSubmit.destroy() 
     self.EHandEntry.destroy() 

    def betST(self): 
     print("betST") 
     self.bettedS=True 
     self.SHandSubmit.destroy() 
     self.SHandEntry.destroy() 

    def betW(self): 
     print("betW") 
     self.bettedW=True 
     self.WHandSubmit.destroy() 
     self.WHandEntry.destroy() 

gui=BlackjackTable(3) 
mainloop()