如何销毁使用Tkinter从类创建的按钮?

2024-09-27 22:17:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Tkinter制作一个虚拟键盘。目前,键盘可以工作,并根据用户输入的内容改变颜色。我的大多数按钮都是使用类来缩短代码的。但是,我想在按下某个按钮时销毁整个键盘(该按钮称为gone)。然而,当按下按钮时,我得到一个AttributeError

问题似乎是我不能销毁从类创建的按钮。我如何在不关闭Tkinter窗口的情况下销毁键盘

代码如下:

import tkinter as tk

txt=""
buttontext=""
messagesinbox=["What colour do you want","What text colour do you want"]
message=messagesinbox[0]
colour="white"
textcolour="black"
count=0
messagecount=0

letters=[["q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"],["Q","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M"]]


root=tk.Tk()
class key:
    def __init__(self,rowe,columne,text,com):
        if count%2==0:
            self.text=text
        else:
            self.text=text.upper()
        self.rower=rowe
        self.columner=columne
        self.colour=colour
        self.txtcol=textcolour
        self=tk.Button(root, height=1,width=2,fg=self.txtcol,bg=self.colour,text=self.text, command=com)
        self.grid(row=rowe,column=columne)


class doublecolumn:
    def __init__(self,rowe,columne,text,com):
        self.text=text
        self.rower=rowe
        self.columner=columne
        self.colour=colour
        self.txtcol = textcolour
        if com==0:
            self = tk.Button(root, height=2,fg=self.txtcol, width=2,bg=self.colour, text=self.text)
        else:
            self=tk.Button(root,height=2,fg=self.txtcol,width=2,bg=self.colour, text=self.text,command=com)
        self.grid(row=rowe,column=columne, rowspan=2)


class textkey:
    def __init__(self,rowe,columne,text,com):
        self.text=text
        self.rower=rowe
        self.columner=columne
        self.colour=colour
        self.txtcol = textcolour
        if com==0:
            self = tk.Button(root,fg=self.txtcol, height=1, width=30,bg=self.colour, text=self.text)
        else:
            self=tk.Button(root,fg=self.txtcol,height=1,width=30,bg=self.colour, text=self.text,command=com)
        self.grid(row=rowe,column=columne, columnspan=250)

class fin:
    def __init__(self,rowe,columne,text,com):
        self.text=text
        self.rower=rowe
        self.columner=columne
        self.colour=colour
        self.txtcol = textcolour
        if com==0:
            self = tk.Button(root,fg=self.txtcol, height=1, width=5,bg=self.colour, text=self.text)
        else:
            self=tk.Button(root,fg=self.txtcol,height=1,width=5, text=self.text,bg=self.colour,command=com)
        self.grid(row=rowe,column=columne, columnspan=2)


def swap():
    global messages,messagecount,message,comment
    messagecount=messagecount+1
    message=messagesinbox[messagecount%2]
    comment = textkey(0, 0, message, 0)

def spacebar():
    global enter, txt
    txt = txt + " "
    enter = textkey(1, 0, txt,0)

def backspace():
    global enter, txt
    length=len(txt)
    length=length-1
    parta=txt[0:length]
    partb=""
    txt=parta+partb
    enter = textkey(1, 0, txt,0)

def one():
    global enter,txt
    txt=txt+"1"
    enter=textkey(1,0,txt,0)

def two():
    global enter,txt
    txt=txt+"2"
    enter=textkey(1,0,txt,0)

def three():
    global enter,txt
    txt=txt+"3"
    enter=textkey(1,0,txt,0)

def four():
    global enter,txt
    txt=txt+"4"
    enter=textkey(1,0,txt,0)

def five():
    global enter,txt
    txt=txt+"5"
    enter=textkey(1,0,txt,0)

def six():
    global enter,txt
    txt=txt+"6"
    enter=textkey(1,0,txt,0)

def seven():
    global enter,txt
    txt=txt+"7"
    enter=textkey(1,0,txt,0)

def eight():
    global enter,txt
    txt=txt+"8"
    enter=textkey(1,0,txt,0)

def nine():
    global enter,txt
    txt=txt+"9"
    enter=textkey(1,0,txt,0)

def zero():
    global enter,txt
    txt=txt+"0"
    enter=textkey(1,0,txt,0)



def q():
    global enter,txt
    txt=txt+letters[count%2][0]
    enter=textkey(1,0,txt,0)

def w():
    global enter,txt
    txt=txt+letters[count%2][1]
    enter=textkey(1,0,txt,0)

def e():
    global enter,txt
    txt=txt+letters[count%2][2]
    enter=textkey(1,0,txt,0)

def r():
    global enter,txt
    txt=txt+letters[count%2][3]
    enter=textkey(1,0,txt,0)

def t():
    global enter,txt
    txt=txt+letters[count%2][4]
    enter=textkey(1,0,txt,0)

def y():
    global enter,txt
    txt=txt+letters[count%2][5]
    enter=textkey(1,0,txt,0)

def u():
    global enter,txt
    txt=txt+letters[count%2][6]
    enter=textkey(1,0,txt,0)

def i():
    global enter,txt
    txt=txt+letters[count%2][7]
    enter=textkey(1,0,txt,0)

def o():
    global enter,txt
    txt=txt+letters[count%2][8]
    enter=textkey(1,0,txt,0)

def p():
    global enter,txt
    txt=txt+letters[count%2][9]
    enter=textkey(1,0,txt,0)


def a():
    global enter, txt
    txt = txt + letters[count%2][10]
    enter = textkey(1, 0, txt,0)


def s():
    global enter, txt
    txt = txt + letters[count%2][11]
    enter = textkey(1, 0, txt,0)


def d():
    global enter, txt
    txt = txt + letters[count%2][12]
    enter = textkey(1, 0, txt,0)


def f():
    global enter, txt
    txt = txt + letters[count%2][13]
    enter = textkey(1, 0, txt,0)


def g():
    global enter, txt
    txt = txt + letters[count%2][14]
    enter = textkey(1, 0, txt,0)


def h():
    global enter, txt
    txt = txt + letters[count%2][15]
    enter = textkey(1, 0, txt,0)


def j():
    global enter, txt
    txt = txt + letters[count%2][16]
    enter = textkey(1, 0, txt,0)


def k():
    global enter, txt
    txt = txt + letters[count%2][17]
    enter = textkey(1, 0, txt,0)


def l():
    global enter, txt
    txt = txt + letters[count%2][18]
    enter = textkey(1, 0, txt,0)


def z():
    global enter, txt
    txt = txt + letters[count%2][19]
    enter = textkey(1, 0, txt,0)


def x():
    global enter, txt
    txt = txt + letters[count%2][20]
    enter = textkey(1, 0, txt,0)


def c():
    global enter, txt
    txt = txt + letters[count%2][21]
    enter = textkey(1, 0, txt,0)


def v():
    global enter, txt
    txt = txt + letters[count%2][22]
    enter = textkey(1, 0, txt,0)


def b():
    global enter, txt
    txt = txt + letters[count%2][23]
    enter = textkey(1, 0, txt,0)


def n():
    global enter, txt
    txt = txt + letters[count%2][24]
    enter = textkey(1, 0, txt,0)


def m():
    global enter, txt
    txt = txt + letters[count%2][25]
    enter = textkey(1, 0, txt,0)

def hashkey():
    global enter, txt
    txt = txt + "#"
    enter = textkey(1, 0, txt,0)

def capslock():
    global count,letts
    count=count+1
    letts=[key(4,0,"q",q),key(4,1,"w",w),key(4,2,"e",e),key(4,3,"r",r),key(4,4,"t",t),key(4,5,"y",y),key(4,6,"u",u),key(4,7,"i",i),key(4,8,"o",o),key(4,9,"p",p),key(5,0,"a",a),key(5,1,"s",s),key(5,2,"d",d),key(5,3,"f",f),key(5,4,"g",g),key(5,5,"h",h),key(5,6,"j",j),key(5,7,"k",k),key(5,8,"l",l),key(5,9,"#",hashkey),doublecolumn(5,10,"^",capslock),key(6,1,"z",z),key(6,2,"x",x),key(6,3,"c",c),key(6,4,"v",v),key(6,5,"b",b),key(6,6,"n",n),key(6,7,"m",m)]

def destruction():
    global letts
    for i in range(0,5):
        letts[i].destroy()


def finish():
    global txt,message,colourchange,textcolour,letts,colour,enter,space,back,done,comment,nums
    origincolour=colour
    origintext=textcolour
    try:
        if messagecount%2==0:
            letts[0].colour=txt
            colour=txt


        else:
            letts[0].textcolour=txt
            textcolour=txt



        message=messagesinbox[messagecount%2]


        letts=[key(4,0,"q",q),key(4,1,"w",w),key(4,2,"e",e),key(4,3,"r",r),key(4,4,"t",t),key(4,5,"y",y),key(4,6,"u",u),key(4,7,"i",i),key(4,8,"o",o),key(4,9,"p",p),key(5,0,"a",a),key(5,1,"s",s),key(5,2,"d",d),key(5,3,"f",f),key(5,4,"g",g),key(5,5,"h",h),key(5,6,"j",j),key(5,7,"k",k),key(5,8,"l",l),key(5,9,"#",hashkey),doublecolumn(5,10,"^",capslock),key(6,1,"z",z),key(6,2,"x",x),key(6,3,"c",c),key(6,4,"v",v),key(6,5,"b",b),key(6,6,"n",n),key(6,7,"m",m)]
        nums=[key(3,0,"1",one),key(3,1,"2",two),key(3,2,"3",three),key(3,3,"4",four),key(3,4,"5",five),key(3,5,"6",six),key(3,6,"7",seven),key(3,7,"8",eight),key(3,8,"9",nine),key(3,9,"0",zero)]
        txt=""
        enter = textkey(1, 0, txt, 0)
        space = textkey(7, 0, "Space", spacebar)
        comment=textkey(0,0,message,0)
        back=key(6,0,"<",backspace)
        done=fin(6,8,"Done",finish)
        colourchange = key(3, 10, "?", swap)


    except tk.TclError:
        message="That is not a valid colour"
        colour=origincolour
        textcolour=origintext
        txt=""
        comment = textkey(0, 0, message, 0)
        txt=""
        enter = textkey(1, 0, txt, 0)

enter=textkey(1,0,"",0)
comment=textkey(0,0,message,0)
letts=[key(4,0,"q",q),key(4,1,"w",w),key(4,2,"e",e),key(4,3,"r",r),key(4,4,"t",t),key(4,5,"y",y),key(4,6,"u",u),key(4,7,"i",i),key(4,8,"o",o),key(4,9,"p",p),key(5,0,"a",a),key(5,1,"s",s),key(5,2,"d",d),key(5,3,"f",f),key(5,4,"g",g),key(5,5,"h",h),key(5,6,"j",j),key(5,7,"k",k),key(5,8,"l",l),key(5,9,"#",hashkey),doublecolumn(5,10,"^",capslock),key(6,1,"z",z),key(6,2,"x",x),key(6,3,"c",c),key(6,4,"v",v),key(6,5,"b",b),key(6,6,"n",n),key(6,7,"m",m)]
nums=[key(3,0,"1",one),key(3,1,"2",two),key(3,2,"3",three),key(3,3,"4",four),key(3,4,"5",five),key(3,5,"6",six),key(3,6,"7",seven),key(3,7,"8",eight),key(3,8,"9",nine),key(3,9,"0",zero)]
space=textkey(7,0,"Space",spacebar)
back=key(6,0,"<",backspace)
done=fin(6,8,"Done",finish)
colourchange=key(3,10,"?",swap)
gone=key(0,0,"<",destruction)


root.mainloop()


Tags: keytextselftxtcomdefglobaltk
1条回答
网友
1楼 · 发布于 2024-09-27 22:17:39

正如错误所说,类Key没有destroy()函数

您可以在类中添加函数:

class key:
    def __init__(self,rowe,columne,text,com):
        if count%2==0:
            self.text=text
        else:
            self.text=text.upper()
        self.rower=rowe
        self.columner=columne
        self.colour=colour
        self.txtcol=textcolour
        # assign to self.button instead of self
        self.button=tk.Button(root, height=1,width=2,fg=self.txtcol,bg=self.colour,text=self.text, command=com)
        self.button.grid(row=rowe,column=columne)
    def destroy(self):
        self.button.destroy()

或者使Key成为tk.Button的派生类:

class key(tk.Button):
    def __init__(self, rowe, columne, text, com):
        if count%2 == 1:
            text = text.upper()
        super().__init__(root, height=1, width=2, fg=textcolour, bg=colour, text=text, command=com)
        self.grid(row=rowe, column=columne)

相关问题 更多 >

    热门问题