2

I have created a program which convert Miles into Km. and Km. into Miles the result displayed is almost of 10 characters so can can help me take out he first 3 characters which come after . here is the code--

from tkinter import *    
import tkinter.messagebox as tmsg

def convert():    
    if numv.get() == "":
        tmsg.showinfo("Error","You may have not entered anything in the text box or "
                      "you may have not selected wether you want to convert from "
                      "Km. to Mile or from Mile to Km.")
    elif numv.get() == 0:
        tmsg.showinfo("Error","You may have not entered anything in the text box or "
                      "you may have not selected wether you want to convert from "
                      "Km. to Mile or from Mile to Km.")
    else:
        if var.get() == "Km. to Mile":
            tmsg.showinfo("Result", f"{(numv.get()/1.609}")
        if var.get() == "Mile to Km.":
            tmsg.showinfo("Result", f"{numv.get()*1.609}")

root = Tk()
root.geometry("500x322")
root.configure(bg="khaki2")

Label(root, text="Select what operation would you like to have", bg="khaki2").pack()
var = StringVar()
var.set("Radio")
Radio = Radiobutton(root, text="Km. to Mile", variable=var,
                    value="Km. to Mile", bg="khaki2").pack()
Radio = Radiobutton(root, text="Mile to Km.", variable=var,
                    value="Mile to Km.", bg="khaki2").pack()

numv = IntVar()
num = Entry(root, textvariable=numv, bg="khaki3").pack()
Button(root, text="Convert", command=convert, bg="burlywood2").pack()
root.mainloop()
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69

2 Answers2

0

Your could round your number:

input = 12345.67890

print(round(input, 3)) # prints 12345.678
mamen
  • 1,202
  • 6
  • 26
0

Format the value inside f-string

f"{(numv.get()/1.609):.6f}"

Example

value = 1.234567891
print(f"{value:.3f}")

Output

'1.235'
Vishnudev Krishnadas
  • 10,679
  • 2
  • 23
  • 55