destroy()不会破坏重复

时间:2016-04-23 04:03:40

标签: python-3.x tkinter

我的代码创建了一个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 个答案:

答案 0 :(得分:1)

您遇到了这个问题,因为在这些方法中:betE()betST()betW()您无用地调用suitWindow(),它会根据复杂的if条件重新绘制组件

解决方案是首先使用suitWindow()根据ifself.numPlayers的值和状态删除所有self.bettedE条件来绘制小部件, self.bettedSself.bettedWself.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()