Search code examples
pythonuser-interfacetkintermeanmedian

How I change the result from int to double in tkinter GUI python? tried casting and didn't work


This's my code below and the result always integer without decimals, how can I change it to double or a number with decimals?

I tried casting but didn't work.


import tkinter as tk
from functools import partial

n_num = list(map(int, input('Enter mean numbers: ').split()))
n = len(n_num)

get_sum = sum(n_num)
mean = get_sum / n

print("Mean / Average is: " + str(mean))



def mean_result (label_mean_result,num):
    n_num = list(map(int,num.get()))
    n = len(n_num)

    get_sum = sum(n_num)
    mean = get_sum /n
    label_mean_result.config(text = "Result = %d" % float(mean))
    return



root = tk.Tk()
root.geometry('400x200+100+200')

root.title('Final program')

meanInput = tk.StringVar()

labelMean = tk.Label(root, text="Mean input:").grid(row=1, column=0)

labelResult = tk.Label(root)

labelResult.grid(row=7, column=2)

entryNum1 = tk.Entry(root, textvariable=meanInput).grid(row=1, column=2)


mean_result = partial(mean_result, labelResult, meanInput)

buttonCal = tk.Button(root, text="Calculate", command=mean_result).grid(row=3, column=0)

I expect the output to be a number with decimals, not an int.


Solution

  • You don't have to cast it into a float.

    Just use string formatting:

    label_mean_result.config(text="Result = {}".format(mean))
    

    Or f-string:

    label_mean_result.config(text=f"Result = {mean}")