Tkinter calculator program not working when i import it

  calculator, import, python, tkinter

this is my first time on StackOverflow, and I’m stuck on this python calculator program.

The code works just fine when I run it standalone, but if I import the program it just doesn’t do anything. The buttons worked fine as I checked them with print statements, I think there’s a problem with the entry or the StringVar() variable.
I tried finding solutions to this but wasn’t able to find anything related to this.

Here’s the code:

from tkinter import *
exp=""
def press(num):
    global exp
    exp=exp + str(num)
    equation.set(exp)
def clrscr():
    global exp
    exp=""
    equation.set("")
def equalpress():
    try:
        global exp
        total=str(eval(exp))
        equation.set(total)
        exp=""
    except:
        equation.set('ERROR!')
        exp=""
win=Tk()
win.title('Calculator')
win.geometry('450x575')
win.configure(bg="#EAF0F1")
win.resizable(width=False, height=False)
equation=StringVar()
equation.set('Enter Value')
disp=Frame(win, width=500, height=100, bg="#dfe4ea")
entry=Entry(disp, width=33, textvariable=equation, font="Roboto 20", bg='#f1f2f6', foreground="#1e272e" )
entry.grid(padx=5, pady=5, ipady=20)
disp.grid(row=0, column=0, sticky="nsew")
btns=Frame(win, width=500, height=400, bg="white")
button1 = Button(btns, text=' 1 ', fg='black', bg='red', command=lambda: press(1), height=6, width=12) 
button1.grid(row=2, column=0) 

button2 = Button(btns, text=' 2 ', fg='black', bg='red',  command=lambda: press(2), height=6, width=12) 
button2.grid(row=2, column=2) 

button3 = Button(btns, text=' 3 ', fg='black', bg='red', command=lambda: press(3), height=6, width=12) 
button3.grid(row=2, column=4) 

button4 = Button(btns, text=' 4 ', fg='black', bg='red', command=lambda: press(4), height=6, width=12) 
button4.grid(row=3, column=0) 

button5 = Button(btns, text=' 5 ', fg='black', bg='red', command=lambda: press(5), height=6, width=12) 
button5.grid(row=3, column=2) 

button6 = Button(btns, text=' 6 ', fg='black', bg='red', command=lambda: press(6), height=6, width=12) 
button6.grid(row=3, column=4) 

button7 = Button(btns, text=' 7 ', fg='black', bg='red', command=lambda: press(7), height=6, width=12) 
button7.grid(row=4, column=0) 

button8 = Button(btns, text=' 8 ', fg='black', bg='red', command=lambda: press(8), height=6, width=12) 
button8.grid(row=4, column=2) 

button9 = Button(btns, text=' 9 ', fg='black', bg='red', command=lambda: press(9), height=6, width=12) 
button9.grid(row=4, column=4) 

button0 = Button(btns, text=' 0 ', fg='black', bg='red', command=lambda: press(0), height=6, width=12) 
button0.grid(row=5, column=0) 

plus = Button(btns, text=' + ', fg='black', bg='red', command=lambda: press("+"), height=6, width=12) 
plus.grid(row=2, column=6) 

minus = Button(btns, text=' - ', fg='black', bg='red', command=lambda: press("-"), height=6, width=12) 
minus.grid(row=3, column=6) 

multiply = Button(btns, text=' * ', fg='black', bg='red',command=lambda: press("*"), height=6, width=12) 
multiply.grid(row=4, column=6) 

divide = Button(btns, text=' / ', fg='black', bg='red', command=lambda: press("/"), height=6, width=12) 
divide.grid(row=5, column=6) 

equal = Button(btns, text=' = ', fg='black', bg='red', command=lambda: equalpress(), height=6, width=12) 
equal.grid(row=5, column=4) 

clear = Button(btns, text='Clear', fg='black', bg='red', command=lambda: clrscr(), height=6, width=50) 
clear.grid(row=6, columnspan=8) 

Decimal= Button(btns, text='.', fg='black', bg='red', command=lambda: press('.'), height=6, width=12) 
Decimal.grid(row=5, column=2)
btns.grid(sticky="nsew")

Source: Python Questions

LEAVE A COMMENT