繁体   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