亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

清除多個標簽值

清除多個標簽值

小怪獸愛吃肉 2022-10-18 15:17:51
我在這里丟了花生。我正在嘗試清除兩個標簽值,但出現錯誤AttributeError: 'Label' object has no attribute 'delete'基本上,如果我要單擊計算小計按鈕,然后單擊除總按鈕。我得到了我的預期值?,F在,如果我要單擊清除值按鈕,我會收到錯誤消息。當我打字時,我真的在搖頭。有人愿意解釋為什么會這樣嗎?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 關注
  • 180 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號