2

I am trying to learn about tkinter so i create small GUI python to input 2 numbers and use their tk.Entry() box to input number and find the sum of both. But I fail to update the float number and generate their sum. Can anyone help add some components so to make it work? When i generate the result , it only show 0 which is the initial value of variable.

import tkinter as tk
from tkinter import *

root = tk.Tk()
root.title("Shear and Moment Diagram Calculator")
operator=""

canvas = tk.Canvas(root, height=400,width=500,bg="White")
canvas.pack()
frame=tk.Frame(root, bg="Light Blue")
frame.place(relwidth=0.9,relheight=0.9, relx=0.05, rely=0.05)

num1 = float()
num2 = float()

tk.Label(frame, font =("Helvetica", 20),
        text="Number 1:" ).grid(row=0)

tk.Label(frame, font =("Helvetica", 20),
        text="Number 2:" ).grid(row=1,column=0)

num1= tk.Entry(frame, textvariable = num1 ).grid(row=0, column = 1)
num2= tk.Entry(frame, textvariable = num2 ).grid(row=1, column = 1)

def generate():
    num3= num1 + num2
    print(num3)


generate=tk.Button(frame, text="result", height="2", width="10",
                   fg="Black", bg="yellow", command = generate )
generate.grid(row=2, column=2)

mainloop()
Pozz Phut
  • 25
  • 1
  • 1
  • 4
  • don't you get error message? always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful information. – furas Jan 21 '20 at 09:49
  • `textvariable` has to be `tk.StringVar()`, `tk.IntVar()` or `tk.DoubleVar()` – furas Jan 21 '20 at 09:50
  • don't use the same name for function `def generate()` and button `generate`. – furas Jan 21 '20 at 09:54

2 Answers2

4

textvariable needs tk.StringVar(), tk.IntVar() or tk.DoubleVar()

Using StringVar() you can convert value using float(), calculate sum and put it back in other StringVar() assigned to Label() or Entry(). You can also display text "Error" if values will be incorrect.

enter image description here

import tkinter as tk

# --- functions ---

def generate():
    try:
        result = float(num1.get()) + float(num2.get())
    except Exception as ex:
        print(ex)
        result = 'error'

    num3.set(result)

# --- main ---

root = tk.Tk()

num1 = tk.StringVar()
num2 = tk.StringVar()
num3 = tk.StringVar()

tk.Label(root, text="Number 1:").grid(row=0, column=0)
tk.Label(root, text="Number 2:").grid(row=1, column=0)
tk.Label(root, text="Result:").grid(row=2, column=0)

tk.Entry(root, textvariable=num1).grid(row=0, column=1)
tk.Entry(root, textvariable=num2).grid(row=1, column=1)
tk.Entry(root, textvariable=num3).grid(row=2, column=1)

button = tk.Button(root, text="Calculate", command=generate)
button.grid(row=3, column=1)

root.mainloop()
furas
  • 134,197
  • 12
  • 106
  • 148
  • Thank you So much, last time i use ` StringVar() ` instead of `tk.StringVar()` which result with " can't covert string to float". – Pozz Phut Jan 21 '20 at 10:09
  • if you try to convert `float("hello")` then you get error `"can't convert string to float"` - the same if you don't use `.get()` to get string from `StringVar()` - and it doesn't matter if you use `StringVar()` or `tk.StringVar()` – furas Jan 21 '20 at 10:15
2

You can use a StringVar() for textvariable to accept a floating point numeric string and then convert them to float using float() inside the generate function.

import tkinter as tk

root = tk.Tk()
root.title("Shear and Moment Diagram Calculator")
operator=""

canvas = tk.Canvas(root, height=400, width=500, bg="White")
canvas.pack()
frame=tk.Frame(root, bg="Light Blue")
frame.place(relwidth=0.9, relheight=0.9, relx=0.05, rely=0.05)

tk.Label(frame, font=("Helvetica", 20), text="Number 1:").grid(row=0)

tk.Label(frame, font=("Helvetica", 20), text="Number 2:").grid(row=1,column=0)

# define entry variables
n1 = tk.StringVar()
n2 = tk.StringVar()

# assign the StringVar to the entry widget textvariables
num1= tk.Entry(frame, textvariable=n1)
num1.grid(row=0, column=1)
num2= tk.Entry(frame, textvariable=n2)
num2.grid(row=1, column=1)

def generate():
    # get the entered value from the entry field and convert it to float and then add
    num3 = float(n1.get()) + float(n2.get())
    print(num3)

generate=tk.Button(frame, text="result", height="2", width="10", fg="Black", bg="yellow", command=generate )
generate.grid(row=2, column=2)

root.mainloop()

Here is the GUI and the result of floating number addition:

GUI

You can also use DoubleVar() to accomplish this task.

Hope it helps!

Somraj Chowdhury
  • 983
  • 1
  • 6
  • 14
  • Last time i used ' StringVar' instead of ' tk.StringVar' which is not working. XD – Pozz Phut Jan 21 '20 at 10:15
  • If you import tkinter using an alias name like `import tkinter as tk` then you need to use `tk.StringVar()` else if you use `from tkinter import *` the you can just use `StringVar()`. So it actually depends on how you import `tkinter`. – Somraj Chowdhury Jan 21 '20 at 10:29
  • Noted , will be careful next time. – Pozz Phut Jan 22 '20 at 04:19