0

I was writing a program to show Integer Value from Entry using Tkinter and python 3.8.2 but it's not working when I enter zero before the number and also if I enter 08 its show error and if i enter 8 it's working here's the code `

from tkinter import *

win = Tk()
win.geometry("300x400")
win.title("My Gui")
win.resizable(0, 0)
text_var = IntVar(win)  # DoubleVar(win)
show_var = StringVar(win)


def show():
    try:
        r = text_var.get()
        show_var.set(r)
    except:
        show_var.set("Enter Number only")


get_text = Entry(win, textvariable=text_var, bd=3).grid(row=0, column=0, padx=15, pady=15)
btn = Button(win, text="Click", activebackground="red", command=show).grid(row=0, column=1)
show_text = Label(win, textvariable=show_var).grid(row=1, column=0, columnspan=2)

win.mainloop()

` enter image description here

enter image description here

Synonian_raj
  • 154
  • 1
  • 13
  • Python may treat the string starts with `0` as octal number. So `04564` is valid octal number, but `0888` is not. – acw1668 Jun 22 '20 at 10:53

1 Answers1

0

Use lstrip to remove the leading zeroes:

def show():
    try:
        r = text_var.get()
        r.lstrip('0')
        show_var.set(r)
    except:
        show_var.set("Enter Number only")

(strip removes both leading and trailing zeroes.)

Jonas
  • 1,749
  • 1
  • 10
  • 18