How to use decimal mark in shortcut from IntVar to tkinter

from tkinter import *
v=Tk()
v.geometry("400x400")
a= IntVar()
a.set(5.494949)


l=Label(textvariable= a)
l.pack()

I use this and return the label with 5.494949 and I need 5.49

+4
source share
2 answers

It might just help :)

from tkinter import *
v=Tk()
v.geometry("400x400")
a= IntVar()
# round function simply rounds the var upto given number of decimal places in the function argument
a.set(round(5.494949,2))

l=Label(textvariable= a)
l.pack()
+2
source

There is no explicit support for what you want. However, you can use StringVaror simply set the value of the label:

l = Label(text="%.2f" % 5.494949)
...
l.configure(text="%.2f" % 3.14159)
+1
source

All Articles