为了账号安全,请及时绑定邮箱和手机立即绑定

清除多个标签值

清除多个标签值

小怪兽爱吃肉 2022-10-18 15:17:51
我在这里丢了花生。我正在尝试清除两个标签值,但出现错误AttributeError: 'Label' object has no attribute 'delete'基本上,如果我要单击计算小计按钮,然后单击除总按钮。我得到了我的预期值。现在,如果我要单击清除值按钮,我会收到错误消息。当我打字时,我真的在摇头。有人愿意解释为什么会这样吗?try:  import Tkinter as tk except:     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.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 resultdef 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()
查看完整描述

1 回答

?
慕斯709654

TA贡献1840条经验 获得超5个赞

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

您可能混淆了tkinter.Text和的方法tkinter.Label。您调用的方法是tkinter.label.delete,未定义(不存在),但是对于tkinter.Text. 因此,“重置”的唯一方法是text将 s 的属性更改tkinter.Label回“默认”字符串。改用另一个小部件可能更合适。


查看完整回答
反对 回复 2022-10-18
  • 1 回答
  • 0 关注
  • 119 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信