This is part of the code I’m using – problem is detailed below. I am creating a simple savings calculator using Python 3.2 and tkinter on Mint Linux Nadia. Advice welcome!
Thanks
Tom
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()
When I run the program the text prints fine, however it contains curly braces around the text:
{Your savings total is £} 10 {and the A shares are worth £} 25 {Your total savings is £} 35
I can’t understand why there are curly braces, but I want them gone. Does anyone know how I can do this? BTW I’m just an enthusiast learning python and loving it so far. I’ve only included the section of the code that I think is relevant.
With
you are creating a 2-element tuple (“Your savings total is £”, a).
Then you add new elements to the tuple with += operator.
result_txt.insertexpects a string as a second argument, not a tuple (docs), so you want to use string formatting instead:(see Python docs explaining
format)