简体   繁体   English

如何删除数组中的所有 python tkinter 标签

[英]How do you remove all python tkinter labels in an array

I'm trying to write an app in Python Tkinter where you input text through a text field, then displays labels for each character from the input text.我正在尝试在 Python Tkinter 中编写一个应用程序,您可以在其中通过文本字段输入文本,然后显示输入文本中每个字符的标签。

from tkinter import *
array = []

root = Tk()
root.title('app interface')

inpframe = LabelFrame(root, text="input", padx=100, pady=20)
inpframe.pack()

outframe = LabelFrame(root, text="output", padx=100, pady=100)
outframe.pack()

c = " "

def on_enter(e):
    e.widget['background'] = 'green'
    c = e.widget['text']
    currentchar = Label(inpframe, text=c)
    currentchar.grid(row=1, column=1)

def on_leave(e):
    e.widget['background'] = 'SystemButtonFace'
    currentchar = Label(inpframe, text=" ")
    currentchar.grid(row=1, column=1)

inp = Entry(inpframe)
inp.grid(row=0, column=0)

def enterText():
    array.clear()
    inptxt = inp.get().lower()
    myLabel = Label(inpframe, text=inptxt)
    myLabel.grid(row=1, column=0)

    for i in inptxt:
        array.append(i)

    for i in range(0, len(array), 1):
        array[i] = Button(outframe, text=array[i], height=10, width=5)
        array[i].grid(row=2, column= i, padx=5, pady=10)
        array[i].bind("<Enter>", on_enter)
        array[i].bind("<Leave>", on_leave)

myButton = Button(inpframe, text="Enter", command=enterText)
myButton.grid(row=0, column=1)

root.mainloop()

Here's the problem.这就是问题所在。 When I input text shorter than the previous text, the shorter text gets displayed, but the remaining text from the previous text remains on the interface enter image description here .当我输入比前一个文本短的文本时,会显示较短的文本,但前一个文本中的剩余文本仍保留在界面上enter image description here For example, when I type "world", the app displays wo r l d.例如,当我输入“world”时,应用程序显示 wo r l d。 But when I type "hi", the app displays hi r ld但是当我输入“hi”时,应用程序显示 hi r ld

I see that the label holding the text is also left behind the newly entered text.我看到保存文本的 label 也留下了新输入的文本。 You can simply create the label in the global scope and then configure the text each time you enter a new text.您可以简单地在全局 scope 中创建 label,然后在每次输入新文本时配置文本。

As for the buttons;至于按钮; there is a simple way to destroy all children of a widget, as exemplified below.有一种简单的方法可以销毁小部件的所有子项,如下所示。

from tkinter import *
array = []

root = Tk()
root.title('app interface')

inpframe = LabelFrame(root, text="input", padx=100, pady=20)
inpframe.pack()
outframe = LabelFrame(root, text="output", padx=100, pady=100)
outframe.pack()
inp = Entry(inpframe)
inp.grid(row=0, column=0)
myLabel = Label(inpframe)   # Create label in the global scope
myLabel.grid(row=1, column=0)

c = " "

def on_enter(e):
    e.widget['background'] = 'green'
    c = e.widget['text']
    currentchar = Label(inpframe, text=c)
    currentchar.grid(row=1, column=1)

def on_leave(e):
    e.widget['background'] = 'SystemButtonFace'
    currentchar = Label(inpframe, text=" ")
    currentchar.grid(row=1, column=1)

def enterText():
    array.clear()
    inptxt = inp.get().lower()
    myLabel.config(text=inptxt) # Configure label for new text

    # Delete all current buttons
    for widget in outframe.winfo_children():
        widget.destroy()

    for i in inptxt:
        array.append(i)

    for i in range(0, len(array), 1):
        array[i] = Button(outframe, text=array[i], height=10, width=5)
        array[i].grid(row=2, column= i, padx=5, pady=10)
        array[i].bind("<Enter>", on_enter)
        array[i].bind("<Leave>", on_leave)

myButton = Button(inpframe, text="Enter", command=enterText)
myButton.grid(row=0, column=1)

root.mainloop()

Alteratly you can loop throug the array and delete each button before you clear array:或者,您可以循环遍历数组并在清除数组之前删除每个按钮:

# Delete all current buttons
for i in array:
    i.destroy()
array.clear()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM