Search code examples
python-3.xtkintertkinter-entry

Python Tkinter: Can i do a calculation with the input of several Entries and display the result as text?


Okay so - I have under 24 hours of experience with programming - this is my second day - so please don't slaughter me. Yesterday I spend almost all day trying to figure this out, but all the concepts are still a bit slippery to me - I hope somebody is willing to help.

I want to make a calculation based on the inputs of several Entry widgets and then make it display the result as an output in a Label (or just text).

Here's a picture

This is a piece of the code


    from tkinter import *
from tkinter.messagebox import *

root = Tk()
root.title("StoGram 1.0")

# Entry
symentry = Entry(root, width=10) #Symbol
enentry = Entry(root, width=10) #Entry
slentry = Entry(root, width=10) #Stop Loss
tpentry = Entry(root, width=10) #Take Profit
kbentry = Entry(root, width=10) #Bank Balance
rentry = Entry(root, width=10) #Max Risk


symentry.grid(row=1, column=3) #Symbol
enentry.grid(row=2, column=1) #Entry
slentry.grid(row=3, column=1) #Stop Loss
tpentry.grid(row=4, column=1) #Take Profit
kbentry.grid(row=5, column=1) #Bank Balance
kbentry.insert(0, "100000") 
rentry.grid(row=6, column=1) #Max Risk
rentry.insert(0, "1")

# Calculate button to grid
calculate_Button = Button(root, text='Calculate', command=answer) 

# Calculate button to grid
calculate_button.grid(row="4", column="3")

I have tried to figure out what to do with # Calculate button to grid calculate_Button = Button(root, text='Calculate', command=answer)

and in trying that I have played around with def answer():

as well as int and str .get() and a couple of other things.

But really, I am just a monkey with a wrench hammer trying to make stuff work and I have no idea what I am doing.

Any advice is much appreciated.


Solution

  • I think this may help you.

    import tkinter as tk
    root = tk.Tk()
    root.title("StoGram 1.0")
    
    bank_balance = tk.StringVar()
    max_risk =tk.StringVar()
    entry = tk.StringVar()
    stop_loss= tk.StringVar()
    def answer():
        output = (int(bank_balance.get())*int(max_risk.get()))/(int(entry.get())-int(stop_loss.get()))
        print(output)
        Output_label.config(text = str(output))
    
    # Entry
    symentry = tk.Entry(root, width=10) #Symbol
    enentry = tk.Entry(root, width=10, textvariable = entry) #Entry
    slentry = tk.Entry(root, width=10, textvariable = stop_loss) #Stop Loss
    tpentry = tk.Entry(root, width=10) #Take Profit
    kbentry = tk.Entry(root, width=10,textvariable = bank_balance) #Bank Balance
    rentry = tk.Entry(root, width=10, textvariable = max_risk) #Max Risk
    symentry_label = tk.Label(root, text = 'Symbol', font=('calibre',10))
    enentry_label = tk.Label(root, text = 'Entry:', font=('calibre',10))
    slentry_label = tk.Label(root, text = 'Stop Loss:', font=('calibre',10))
    tpentry_label = tk.Label(root, text = 'Take Profit:', font=('calibre',10))
    kbentry_label = tk.Label(root, text = 'Bank Balance:', font=('calibre',10))
    rentry_label = tk.Label(root, text = 'Max Risk%', font=('calibre',10))
    Buy_shares_label = tk.Label(root, text = 'Buy Shares:', font=('calibre',10))
    Output_label = tk.Label(root, text = '', font=('calibre',10))
    Rps_Label = tk.Label(root, text = 'Rps:', font=('calibre',10))
    R_Label = tk.Label(root, text = 'R:', font=('calibre',10))
    symentry_label.grid(row=1, column=3)
    enentry_label.grid(row=3, column=1)
    slentry_label.grid(row=4, column=1)
    tpentry_label.grid(row=5, column=1)
    kbentry_label.grid(row=6, column=1)
    rentry_label.grid(row=7, column=1)
    
    Rps_Label.grid(row=4,column=4)
    Buy_shares_label.grid(row=5,column=4)
    Output_label.grid(row=5,column=6)
    R_Label.grid(row=6,column=4)
    
    symentry.grid(row=2, column=3) #Symbol
    enentry.grid(row=3, column=2) #Entry
    slentry.grid(row=4, column=2) #Stop Loss
    tpentry.grid(row=5, column=2) #Take Profit
    kbentry.grid(row=6, column=2) #Bank Balance
    kbentry.insert(0, "100000")
    rentry.grid(row=7, column=2) #Max Risk
    rentry.insert(0, "1")
    
    # Calculate button to grid
    calculate_Button = tk.Button(root, text='Calculate', command=answer)
    
    # Calculate button to grid
    calculate_Button.grid(row="5", column="3")
    root.mainloop()
    

    To make calculation you need to firstly create a function which as you said is named as answer. def answer()then inside that you can do all the calculations.

    Then the most important job is to extract the values from the text inputs so we need to create variables initializing them as tk.StringVar() this will make the variable a kind of global variable which could be used through the whole program. Then we need to connect the variable with the tk.Entry() for which we need to use textvariable = Variable Name as one of its parameters.

    Also we need to cast the variables to integer with int() because the values we get from the Text Input are in the form of strings.