0
from Tkinter import *

top=Tk()

First Value A that user will input

A = Entry(top)
A.grid(row=1, column=1)

Second value B that user also inputs

B = Entry(top)
B.grid(row=1, column=2)

Calculation - Now I want to add those values (Preferably values with decimal points)

A1=float(A.get())
B1=float(B.get())
C1=A1+B1

Result - I want python to calculate result and show it to user when I input the first two values

C = Label(textvariable=C1)
C.grid(row=1, column=3)


top.mainloop()
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • What's your question? All that's missing is a button to kick off the calculation. – Bryan Oakley Dec 20 '16 at 15:35
  • I tried it with button too, but i get the same error (Could not convert string to float) – Toni Duspara Dec 20 '16 at 15:51
  • @BryanOakley Here is my code `from Tkinter import *` `top=Tk()` `A = Entry(top)` `A.grid(row=1, column=1)` `B = Entry(top)` `B.grid(row=1, column=2)` `C1=StringVar()` `def x():` ` A1=float(A.get())` ` B1=float(B.get())` `C1=A1+B1` `C = Label(textvariable=C1)` `C.grid(row=1, column=3)` `start=Button(command=x)` `start.grid(row=2, column=1, sticky=E+W)` `top.mainloop()` – Toni Duspara Dec 20 '16 at 15:57
  • please don't post code in the comments. You can edit your question by clicking the [edit](http://stackoverflow.com/posts/41245364/edit) link – Bryan Oakley Dec 20 '16 at 15:59

1 Answers1

1

First off, welcome to StackOverflow and nice job- your code does (mostly) everything you want it to do! The timing is just off- you create your objects and get the value, but the value hasn't been input by the user yet.

To solve that, you need to put your .get()'s in a function, and you should be using an actual text-variable that you set() after each one (if you just use C1=(float), you'll end up making new floats so the Label isn't pointing to the right one).

(setup... )
B.grid(...)
C1 = Tkinter.StringVar()
C = Label(textvariable=C1) # Using a StringVar will allow this to automagically update

def setC():
    A1=float(A.get())
    B1=float(B.get())
    C1.set(str(A1+B1))

Additionally, you need to set this function so it goes off more than just "immediately on running the program". The simple way to do this is to just have the function call itself .after() some time (in milliseconds).

def setC():
    # Body above
    top.after(1000, setC) # Call setC after 1 second, so it keeps getting called.

setC() # You have to call it manually once, and then it repeats.

The slightly more advanced and efficient way to update involves events and bindings (binding setC() to fire every time A1 or B1 is changed), but the writeup on that is long so I'll give you that tip and send you to some documentation on that. (Effbot is good tkinter documentation regardless)

Delioth
  • 1,564
  • 10
  • 16
  • Thanks for answering so quickly, I did what you said, but I get "ValueError: Could not convert string to float:" – Toni Duspara Dec 20 '16 at 15:35
  • Ah, given your original code I would've expected you to already be getting that (Python can't convert the empty string into a float). The solution is to `.insert()` a value into your entries before they are ever converted to floats (so before `setC()` is run). – Delioth Dec 20 '16 at 15:49
  • While you _can_ use a `StringVar`, it's not strictly necessary. You can get and set the value of an entry without it. – Bryan Oakley Dec 20 '16 at 15:59
  • Thank you very much, I finally did it :) – Toni Duspara Dec 20 '16 at 16:02