简体   繁体   English

清除多个标签值

[英]clearing multiple labels values

I am losing my peanuts here.我在这里丢了花生。 I am trying to clear two label values but i get an error我正在尝试清除两个 label 值,但出现错误

AttributeError: 'Label' object has no attribute 'delete'

在此处输入图像描述

basically if i were to click the calculate subtotal button then click the divide total button.基本上,如果我要单击计算小计按钮,然后单击除总按钮。 I get my intended values.我得到了我的预期值。 Now if I were to click on the clear values button i get an error.现在,如果我要单击清除值按钮,我会收到错误消息。 Literally shaking my head as I type this.当我打字时,我真的在摇头。 Anyone care to explain why this is the case?有人愿意解释为什么会这样吗?

try:
  import Tkinter as tk
 except:
     import tkinter as tk


class GetInterfaceValues():
def __init__(self):
    self.root = tk.Tk()
    self.totalValue = tk.StringVar()

    self.root.geometry('500x200')
    self.calculateButton = tk.Button(self.root,
                            text='Calculate Subtotal',
                            command=self.getSubtotals)

    self.divideTotalButton = tk.Button(self.root,
                                     text='Divide total',
                                     command=self.divide)
    self.textInputBox = tk.Text(self.root, relief=tk.RIDGE, height=1, width = 6, borderwidth=2)

    self.firstLabel = tk.Label(self.root, text="This is the subtotal:")
    self.secondLabel = tk.Label(self.root, text="This is the Divide Total:")

    self.clearTotalButton = tk.Button(self.root, text='clear the values',command = self.clear)

    self.firstLabel.pack(side="bottom")
    self.secondLabel.pack(side="bottom")

    self.textInputBox.pack()
    self.calculateButton.pack()
    self.divideTotalButton.pack()
    self.clearTotalButton.pack()
    self.root.mainloop()

def getTextInput(self):
    result = self.textInputBox.get("1.0", "end")
    return result

def getSubtotals(self):
    userValue = int(self.getTextInput())

    self.firstLabel["text"] = self.firstLabel["text"] + str(userValue * 5)

def divide(self):
    userValue = int(self.getTextInput())
    self.secondLabel["text"] = self.secondLabel["text"] + str(userValue / 10)

def clear(self):
    self.firstLabel["text"] = self.firstLabel.delete("1.0","end")


app = GetInterfaceValues()
try:
  import Tkinter as tk
except:
  import tkinter as tk


class GetInterfaceValues():
    def __init__(self):
        self.root = tk.Tk()
        self.totalValue = tk.StringVar()

        self.root.geometry('500x200')
        self.calculateButton = tk.Button(self.root,
                                text='Calculate Subtotal',
                                command=self.getSubtotals)

        self.divideTotalButton = tk.Button(self.root,
                                         text='Divide total',
                                         command=self.divide)
        self.textInputBox = tk.Text(self.root, relief=tk.RIDGE, height=1, width = 6, borderwidth=2)

        self.firstLabelDefault = "This is the subtotal:"
        self.secondLabelDefault = "This is the Divide Total:"

        self.firstLabel = tk.Label(self.root, text=self.firstLabelDefault)
        self.secondLabel = tk.Label(self.root, text=self.secondLabelDefault)

        self.clearTotalButton = tk.Button(self.root, text='clear the values',command = self.clear)

        self.firstLabel.pack(side="bottom")
        self.secondLabel.pack(side="bottom")

        self.textInputBox.pack()
        self.calculateButton.pack()
        self.divideTotalButton.pack()
        self.clearTotalButton.pack()
        self.root.mainloop()

    def getTextInput(self):
        result = self.textInputBox.get("1.0", "end")
        return result

    def getSubtotals(self):
        userValue = int(self.getTextInput())

        self.firstLabel["text"] = self.firstLabel["text"] + str(userValue * 5)

    def divide(self):
        userValue = int(self.getTextInput())
        self.secondLabel["text"] = self.secondLabel["text"] + str(userValue / 10)

    def clear(self):
        self.firstLabel["text"] = self.firstLabelDefault
        self.secondLabel["text"] = self.secondLabelDefault
        self.textInputBox.delete("1.0", "end")


app = GetInterfaceValues()

You may have confused the methods of tkinter.Text and tkinter.Label .您可能混淆了tkinter.Texttkinter.Label的方法。 The method you called was tkinter.label.delete , which is not defined (does not exist), however it does exist for the tkinter.Text .您调用的方法是tkinter.label.delete ,它没有定义(不存在),但是它确实存在于tkinter.Text Therefore, the only way to 'reset' would be to change the text attribute of the tkinter.Label s back to a 'default' string.因此,“重置”的唯一方法是将tkinter.Labeltext属性更改回“默认”字符串。 It would perhaps be more appropriate to use another widget instead.改用另一个小部件可能更合适。

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

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