簡體   English   中英

在Python中以文本顯示的花括號

[英]Curly braces showing in text in Python

這是我正在使用的代碼的一部分-問題在下面詳述。 我正在Mint Linux Nadia上使用Python 3.2和tkinter創建一個簡單的儲蓄計算器。 歡迎指教! 謝謝湯姆

    Button(self,
           text = "Click for savings calculations",
           command = self.show_result
           ).grid(row = 6, column = 0, sticky = W)

    self.result_txt = Text(self, width = 75, height = 10, wrap = WORD)
    self.result_txt.grid(row = 7, column = 0, columnspan = 4)

def show_result(self):

    curacct = int(self.curacct_ent.get())

    isa = int(self.isa_ent.get())

    av = int(self.av_ent.get())
    avu = int(self.avu_ent.get())

    a = int(curacct + isa)
    b = int(av*avu)



    result = "Your savings total is £", (a)
    result += "and the A shares are worth £", (b)
    result += "Your total savings is £", (a+b)





    self.result_txt.delete(0.0, END)
    self.result_txt.insert(0.0, result)

# main
root = Tk()
root.title("Savings Calculator")
app = Application(root)
root.mainloop()

當我運行該程序時,文本可以很好地打印,但是在文本周圍包含花括號:{您的儲蓄總額為£} 10 {而A股的價值為£} 25 {您的儲蓄總額為£} 35

我不明白為什么會有花括號,但我希望它們消失。 有人知道我該怎么做嗎? 順便說一句,到目前為止,我只是一個學習python並熱愛它的愛好者。 我只包含了我認為相關的代碼部分。

result = "Your savings total is £", (a)

您正在創建一個2元素元組(“您的儲蓄總額為£”,a)。

然后,使用+ =運算符將新元素添加到元組。

result_txt.insert需要一個字符串作為第二個參數,而不是一個元組( docs ),因此您想改用字符串格式:

result = ("Your savings total is £{} "
          "and the A shares are worth £{} "
          "Your total savings is £{}").format(a, b, a+b)

(請參閱解釋format Python文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM