标签文本重命名后如何去除标签? [英] How to get rid of a label once its text has been renamed?

查看:102
本文介绍了标签文本重命名后如何去除标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个Label,在其中按下按钮时,它会更改文本名称,以在现有标签下方显示另一个标签.它几乎只是在下面的行上打印带有相同文本的相同标签.按下删除文本"按钮时,它将删除已创建的新标签,但是如何在新标签之前删除该标签?我也只能通过销毁删除标签,而不能忘记.

So I have a Label where when a button is pressed, it changes the name of the text to display another label below the existing label. It pretty much just prints the same label with a different text on the row below. When the Delete Text button is pressed, it will delete the new label that has been created but how do I delete the label before the new label? I can also only remove the label via destroy and not forget.

我尝试将变量的文本改回其在窗口上显示的内容,但似乎一旦更改名称,标签就不会在python上存在,而只会在窗口上存在.

I tried changing the text of the variable back to what it says on the window but it seems like once the name has been changed, the label doesn't exist on python but it exists on the window only.

from tkinter import *

main = Tk()

NameList = []
counter = 0


def PrintText():

    #Globalising first and last name so that they can be used in other subroutines
    global name, counter, NameLabel

    #Defining the .get() function as something else to make it easier
    name = name_entry.get()

    NameList.append(name)
    NameLabel = Label(main, text=(NameList[0+counter]))
    NameLabel.grid(row=(counter+3))
    counter += 1

def DeleteText():

    global NameLabel, counter

    NameLabel.destroy()
    counter -= 1
    del NameList[counter]

def Name():
    #Globalising the labels and buttons so that they can be deleted later on
    global first_name_label, first_quit, next_button
    #
    name_label = Label(main, text="Name:")
    name_label.grid(row=0, column=0)
    #
    PrintTextButton = Button(main, text="Print Text", width=8, command=PrintText, bg="light blue")
    PrintTextButton.grid(row=2, column=0)
    DeleteTextButton = Button(main, text="Delete Text", width=8, command=DeleteText, bg="light blue")
    DeleteTextButton.grid(row=2, column=1)


name_entry = Entry(main)
name_entry.grid(row=0, column=1, columnspan=4)

errorName = StringVar()
Name()

当我在输入框中输入所需内容并按打印文本"时,我希望它创建标签.每次按打印文本"时,我希望它在旧标签下创建一个新标签.当我按删除项目时,我希望它删除我创建的标签.当我按住删除项目时,我希望它继续删除标签.

When I enter what I want in the entry box and press Print Text, I want it to create a label. Each time I press Print Text, I want it to create a new Label beneath the old label. When I press remove item, I want it to delete the label I have created. When I keep pressing remove item, I would like it to keep removing the labels.

推荐答案

问题:标签的文本重命名后,如何摆脱该标签?

Question: How to get rid of a label once its text has been renamed?

  • 定义一个容纳Label的容器class.
    在这里,我们使用从tk.Frame继承的class LabelListbox.

    • Define a Container class which holds the Label.
      Here we use class LabelListbox which inherits from tk.Frame.

      class LabelListbox(tk.Frame):
          def __init__(self, parent, *args, **kwargs):
              super().__init__(parent, *args, **kwargs)
      

    • @property .labels在容器中按排序顺序返回全部 Label.

    • The @property .labels returns all Label in the Container in sorted order.

      @property
      def labels(self):
          return sorted(self.children)
      

    • 向容器中添加新的Label,布局到列表末尾

    • Add a new Label to the Container, layout to the end of the list

      def add_label(self, text):
          tk.Label(self, text=text).grid()
      

    • 更改Labeltext=(由其索引引用).

    • Changes the text= of a Label, referenced by its index.

      def configure_text(self, idx, text):
          labels = self.labels
          if idx in range(0, len(labels)):
              self.children[labels[idx]].configure(text=text)
      

    • 从容器中删除最后一个Label

      def remove_last(self):
          if self.labels:
              self.children[self.labels[-1]].destroy()
      

    • 删除给定索引处的Label. (从0开始)

    • Remove a Label at the given index. (0-based)

      def remove_by_index(self, idx):
          labels = self.labels
          if idx in range(0, len(labels)):
              self.children[labels[idx]].destroy()
      

    • 删除所有可以找到给定textLabel

      def remove_by_text(self, text):
          for label in self.labels:
              if text in self.children[label]['text']:
                  self.children[label].destroy()
      

    • 用法:一个接一个地单击菜单项!

      Usage: Click one after another of the menu items!

      import tkinter as tk
      
      
      class LabelListbox(tk.Frame):
          # Here define all of the above
      
      
      class App(tk.Tk):
          def __init__(self):
              super().__init__()
              self.menubar = tk.Menu(self)
              self.config(menu=self.menubar)
      
              lbl_listbox = LabelListbox(self)
              lbl_listbox.grid(row=0, column=0)
      
              for _ in range(4):
                  lbl_listbox.add_label('This is Label {}'.format(_))
      
              self.menubar.add_command(label='Remove last',
                                       command=lbl_listbox.remove_last)
      
              self.menubar.add_command(label='Remove by index 1',
                                       command=lambda:lbl_listbox.remove_by_index(1))
      
              self.menubar.add_command(label='Change Label text"',
                                       command=lambda:lbl_listbox.
                                       configure_text(0, 'The text changed to Label 1'))
      
              self.menubar.add_command(label='Remove by text "Label 1"',
                                       command=lambda:lbl_listbox.remove_by_text('Label 1'))
      
      if __name__ == '__main__':
          App().mainloop()
      

      使用Python测试:3.5

      Tested with Python: 3.5

      这篇关于标签文本重命名后如何去除标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆