清除多个标签值

我在这里丢了花生。我正在尝试清除两个标签值,但出现错误

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

http://img3.mukewang.com/634e53380001068409870445.jpg

基本上,如果我要单击计算小计按钮,然后单击除总按钮。我得到了我的预期值。现在,如果我要单击清除值按钮,我会收到错误消息。当我打字时,我真的在摇头。有人愿意解释为什么会这样吗?


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()


小怪兽爱吃肉
浏览 120回答 1
1回答

慕斯709654

try:  import Tkinter as tkexcept:  import tkinter as tkclass 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()您可能混淆了tkinter.Text和的方法tkinter.Label。您调用的方法是tkinter.label.delete,未定义(不存在),但是对于tkinter.Text. 因此,“重置”的唯一方法是text将 s 的属性更改tkinter.Label回“默认”字符串。改用另一个小部件可能更合适。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python