我的代码创建了一个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()
答案 0 :(得分:1)
您遇到了这个问题,因为在这些方法中:betE()
,betST()
和betW()
您无用地调用suitWindow()
,它会根据复杂的if
条件重新绘制组件
解决方案是首先使用suitWindow()
根据if
,self.numPlayers
的值和状态删除所有self.bettedE
条件来绘制小部件, self.bettedS
,self.bettedW
和self.betCount
标记:
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()