Trying to add string to float variable in tkinter label/entry widgets
Question: I'm hoping to get some guidance on an issue I've been spending far too much
time trying to solve. Every time I find a solution another problem takes its
place.

My aim is to 'simply' do some basic maths on some figures and then add a '$'
at the start of the float variable.

At first I tried this:

    
    
    self.paid_main.set("$", self.paid_entry.get())
    

Error:

    
    
    TypeError: set() takes 2 positional arguments but 3 were given
    

I'm obviously missing something here as I thought I gave 2 arguments - "$" and
self.paid_entry.get()

Then I tried:

    
    
    paid = ("$", self.paid_entry.get())
    self.paid_main.set(paid)
    

This works but gives a horrible looking result on the GUI. Instead of just
placing a dollar sign there, it puts curly braces around it. e.g. {$}

However beggars can't be choosers so I stick with the horrible curly braces
option, until a little bit further down I run into this issue.

    
    
    total_main = ("$", "%.2f" % self.quantity_entry.get() *   self.paid_entry.get() + self.brokerage_entry.get())
    self.total_main.set(total_main)
    

Error:

    
    
    TypeError: can't multiply sequence by non-int of type 'float'
    

Again, I must be missing something since when can't you multiply a float?

I can solve all my issues if I choose not to add a dollar sign in front of
anything, but then I'll be settling for second best. I'm hoping someone can
help me with the following questions:

  1. What are the 3 positional arguments, as I can only count 2? 
  2. Is there a way to provision for another argument?
  3. Is there a way to add a dollar sign without curly braces?
  4. Why am I getting the error trying to multiply a float?

I should note, I have to set the IntVar and DoubleVar variables to an empty
string as leaving it will add 0 and 0.0 respectively to the label coordinates
before I even click on 'add.' Unless there's another way of avoiding this?

Thanks in advance to anyone who can help me.

Full code below:

    
    
    #!/usr/bin/env python3.4
    
    from tkinter import *
    import ystockquote
    import urllib.request
    from urllib.request import urlopen
    from bs4 import BeautifulSoup
    import decimal
    
    class Shares(Frame):
    
        def __init__(self, master):
        Frame.__init__(self, master)
        Frame(self ,width = 500, height = 300)
        self.configure()
        self.grid()
    
        Label(self, font = "Helvetica 14 bold", text = "      Name    ").grid(row = 8, column = 1, columnspan = 2, sticky = W, padx = 5, pady = 5)
        Label(self, font = "Helvetica 14 bold", text = "      Code    ").grid(row = 8, column = 3, padx = 5, pady = 5)
        Label(self, font = "Helvetica 14 bold", text = "    Buy Date  ").grid(row = 8, column = 4, padx = 5, pady = 5)
        Label(self, font = "Helvetica 14 bold", text = "    Quantity  ").grid(row = 8, column = 5, padx = 5, pady = 5)
        Label(self, font = "Helvetica 14 bold", text = "      Paid    ").grid(row = 8, column = 6, padx = 5, pady = 5)
        Label(self, font = "Helvetica 14 bold", text = "    Brokerage ").grid(row = 8, column = 7, padx = 5, pady = 5)
        Label(self, font = "Helvetica 14 bold", text = "      Total   ").grid(row = 8, column = 8, padx = 5, pady = 5)
        Label(self, font = "Helvetica 14 bold", text = "     Current  ").grid(row = 8, column = 9, padx = 5, pady = 5)
        Label(self, font = "Helvetica 14 bold", text = "      Total   ").grid(row = 8, column = 10,padx = 5, pady = 5)
        Label(self, font = "Helvetica 14 bold", text = "  % Loss/Gain ").grid(row = 8, column = 11, padx = 5, pady = 5)
    
        Button(self, text = "New Record", command = self.dialogue_box).grid(row = 9, column = 0)
    
    def dialogue_box(self):
        top = self.top = Toplevel()
    
        Label(top, text = "Code: ").grid(row = 0, column = 0, sticky = E, padx = 5, pady = 5)
        Label(top, text = "Buy Date: ").grid(row = 1, column = 0, sticky = E, padx = 5, pady = 5)
        Label(top, text = "Quantity: ").grid(row = 2, column = 0, sticky = E, padx = 5, pady = 5)
        Label(top, text = "Paid: ").grid(row = 3, column = 0, sticky = E, padx = 5, pady = 5)
        Label(top, text = "Brokerage: ").grid(row = 4, column = 0, sticky = E, padx = 5, pady = 5)
    
        self.code_main = StringVar()
        Label(self, textvariable = self.code_main).grid(row = 9, column = 3, padx = 5, pady = 5)
    
        self.code_entry = StringVar()
        Entry(top, textvariable = self.code_entry).grid(row = 0, column = 1, padx = 5, pady = 5)
        self.code_entry.set("")
    
        self.date_main = StringVar()
        Label(self, textvariable = self.date_main).grid(row = 9, column = 4, padx = 5, pady = 5)
        self.date_main.set("")
    
        self.date_entry = StringVar()
        Entry(top, textvariable = self.date_entry).grid(row = 1, column = 1, padx = 5, pady = 5)
        self.date_entry.set("")
    
        self.quantity_main = IntVar()
        Label(self, textvariable = self.quantity_main).grid(row = 9, column = 5, padx = 5, pady = 5)
        self.quantity_main.set("")
    
        self.quantity_entry = IntVar()
        Entry(top, textvariable = self.quantity_entry).grid(row = 2, column = 1, padx = 5, pady = 5)
        self.quantity_entry.set("")
    
        self.paid_main = DoubleVar()
        Label(self, textvariable = self.paid_main).grid(row = 9, column = 6, padx = 5, pady = 5)
        self.paid_main.set("")
    
        self.paid_entry = DoubleVar()
        Entry(top, textvariable = self.paid_entry).grid(row = 3, column = 1, padx = 5, pady = 5)
        self.paid_entry.set("")
    
        self.brokerage_main = DoubleVar()
        Label(self, textvariable = self.brokerage_main).grid(row = 9, column = 7, padx = 5, pady = 5)
        self.brokerage_main.set("")
    
        self.brokerage_entry = DoubleVar()
        Entry(top, textvariable = self.brokerage_entry).grid(row = 4, column = 1, padx = 5, pady = 5)
        self.brokerage_entry.set(29.95)
    
        self.total_main = DoubleVar()
        Label(self, textvariable = self.total_main).grid(row = 9, column = 8, padx = 5, pady = 5)
        self.total_main.set("")
    
        self.current_main = DoubleVar()
        Label(self, textvariable = self.current_main).grid(row = 9, column = 9, padx = 5, pady = 5)
        self.current_main.set("")
    
        self.total_two_main = DoubleVar()
        Label(self, textvariable = self.total_two_main).grid(row = 9, column = 10, padx = 5, pady = 5)
        self.total_two_main.set("")
    
        self.loss_gain_main = DoubleVar()
        Label(self, textvariable = self.loss_gain_main).grid(row = 9, column = 11, padx = 5, pady = 5)
        self.loss_gain_main.set("")
    
        Button(top, text = "Add", command = self.add).grid(row = 5, column = 0)
    
    def add(self):
        self.code_main.set(self.code_entry.get())
        self.date_main.set(self.date_entry.get())
        self.quantity_main.set(self.quantity_entry.get())
        paid = ("$", "%.2f" % self.paid_entry.get())
        self.paid_main.set(paid)
        brokerage = ("$", self.brokerage_entry.get())
        self.brokerage_main.set(brokerage)
        total_main = ("$", "%.2f" % self.quantity_entry.get() * self.paid_entry.get() + self.brokerage_entry.get())
        self.total_main.set(total_main)
        self.current_main.set(ystockquote.get_price(self.code_entry.get() + ".AX"))
        total_two = (self.current_main.get() * self.quantity_entry.get())
        self.total_two_main.set(total_two)
        rounded = ((self.total_two_main.get() / self.total_main.get() * 100) - 100)
        self.loss_gain_main.set("%.2f" % rounded)
        self.top.destroy()
    
    if __name__ == "__main__":
    
        master = Tk()
        master.title("Shares program")
        app = Shares(master)
        master.geometry("1280x550+0+0")
        master.mainloop()
    


Answer: You have to concatenate text using `+` and `str()` if element is not text.

    
    
    self.paid_main.set( "$" + str(self.paid_entry.get()) )
    

`self.paid_main` is treated as first argument for `set()`.

* * *
    
    
    paid = ("$", self.paid_entry.get())
    

This creates tuple `("$", some_value)` \- it doesn't concatenate elements.

`,` creates tuple or separates arguments in function like `print(args1, args2,
...)`

* * *

It can be one problem: `self.paid_main` is `DoubleVar()` and you can't set
text to it. You have to use `StringVar()` to set text with `$`

* * *

In this

    
    
    total_main = ("$", "%.2f" % self.quantity_entry.get() *   self.paid_entry.get() + self.brokerage_entry.get())
    

use bracket to calculate value before you use it with `%`

    
    
    total_main = "$%.2f" % (self.quantity_entry.get()*self.paid_entry.get() + self.brokerage_entry.get())
    

or

    
    
    value = self.quantity_entry.get()*self.paid_entry.get() + self.brokerage_entry.get()
    total_main = "$%.2f" % value
    

without brackets Python treads this as

    
    
    text = "$%.2f" % self.quantity_entry.get()
    total_main = text * self.paid_entry.get() + self.brokerage_entry.get()
    

