Home > Software design >  My tkinter program keeps on freezing/crashing and i'm not sure if it's because of the code
My tkinter program keeps on freezing/crashing and i'm not sure if it's because of the code

Time:12-23

(I'm new to the site so sorry if there are any mistakes)This is the code and i don't know why it keeps crashing i know it is not my PC as many other of my tkinter programs can run on it. I posted all of the code(not very much) because i don't have a clue of the reason. Thanks for any help.

from tkinter import *
Calculator = Tk()
def input0():
 int(input(0))
def input1():
 int(input(1))
def input2():
 int(input(2))
def input3():
 int(input(3))
def input4():
 int(input(4))
def input5():
 int(input(5))
def input6():
 int(input(6))  
def input7():
 int(input(7))
def input8():
 int(input(8))
def input9():
 int(input(9))
def inputplus():
 str(input(" "))
def inputminus():
 str(input("-"))
def inputdivide():
 str(input("/"))
def inputtimes():
 str(input("*"))
def inputdecimal():
 str(input("."))
def equal():
    eval
 


Calculator.geometry("237x336")
Num1 = Button(Calculator, text="1",command= input1, width=7, height=2)
Num1.place(x=1,y=80)
Num2 = Button(Calculator, text="2",command= input2, width=7, height=2)
Num2.place(x=60,y=80)
Num3 = Button(Calculator, text="3",command= input3, width=7, height=2)
Num3.place(x=120,y=80)
Num4 = Button(Calculator, text="4",command= input4, width=7, height=2)
Num4.place(x=1,y=40)
Num5 = Button(Calculator, text="5",command= input5, width=7, height=2)
Num5.place(x=60,y=40)
Num6 = Button(Calculator, text="6",command= input6, width=7, height=2)
Num6.place(x=120,y=40 )
Num7 = Button(Calculator, text="7",command= input7, width=7, height=2)
Num7.place(x=1,y=1)
Num8 = Button(Calculator, text="8",command= input8, width=7, height=2)
Num8.place(x=60,y=1)
Num9 = Button(Calculator, text="9",command= input9, width=7, height=2)
Num9.place(x=120,y=1)
Num0 = Button(Calculator, text="0",command= input0, width=7, height=2)
Num0.place(x=1,y=120)
x = Button(Calculator, text="x",command= inputtimes, width=7, height=2)
x.place(x=180,y=1)
divide = Button(Calculator, text="÷",command= inputdivide, width=7, height=2)
divide.place(x=180,y=40)
add = Button(Calculator, text=" ",command= inputplus, width=7, height=2)
add.place(x=180,y=80)
minus = Button(Calculator, text="-",command= inputminus, width=7, height=2)
minus.place(x=180,y=120)
dot = Button(Calculator, text=".",command= inputdecimal, width=7, height=2)
dot.place(x=120,y=120)
equals = Button(Calculator, text="=",command= eval, width=7, height=2)
equals.place(x=60,y=120)

Calculator.mainloop()

CodePudding user response:

Things are wrong: when you input, you get the text passed by user, and leave it. in equal(), eval function has no parameters.

the fixed code:

work = ''

from tkinter import *

Calculator = Tk()


def input0():
    global work
    # int(input(0))
    work = work   '0'

def input1():
    global work
    # int(input(1))
    work = work   '1'


def input2():
    global work
    # int(input(2))
    work = work   '2'


def input3():
    global work
    # int(input(3))
    work = work   '3'


def input4():
    global work
    # int(input(4))
    work = work   '4'


def input5():
    global work
    # int(input(5))
    work = work   '5'


def input6():
    global work
    # int(input(6))
    work = work   '6'


def input7():
    global work
    # int(input(7))
    work = work   '7'


def input8():
    global work
    # int(input(8))
    work = work   '8'


def input9():
    global work
    # int(input(9))
    work = work   '9'


def inputplus():
    global work
    # str(input(" "))
    work = work   ' '


def inputminus():
    global work
    # str(input("-"))
    work = work   '-'


def inputdivide():
    global work
    # str(input("/"))
    work = work   '/'


def inputtimes():
    global work
    # str(input("*"))
    work = work   '*'


def inputdecimal():
    global work
    # str(input("."))
    work = work   '.'


def equal():
    global work
    print(eval(work))


Calculator.geometry("237x336")
Num1 = Button(Calculator, text="1", command=input1, width=7, height=2)
Num1.place(x=1, y=80)
Num2 = Button(Calculator, text="2", command=input2, width=7, height=2)
Num2.place(x=60, y=80)
Num3 = Button(Calculator, text="3", command=input3, width=7, height=2)
Num3.place(x=120, y=80)
Num4 = Button(Calculator, text="4", command=input4, width=7, height=2)
Num4.place(x=1, y=40)
Num5 = Button(Calculator, text="5", command=input5, width=7, height=2)
Num5.place(x=60, y=40)
Num6 = Button(Calculator, text="6", command=input6, width=7, height=2)
Num6.place(x=120, y=40)
Num7 = Button(Calculator, text="7", command=input7, width=7, height=2)
Num7.place(x=1, y=1)
Num8 = Button(Calculator, text="8", command=input8, width=7, height=2)
Num8.place(x=60, y=1)
Num9 = Button(Calculator, text="9", command=input9, width=7, height=2)
Num9.place(x=120, y=1)
Num0 = Button(Calculator, text="0", command=input0, width=7, height=2)
Num0.place(x=1, y=120)
x = Button(Calculator, text="x", command=inputtimes, width=7, height=2)
x.place(x=180, y=1)
divide = Button(Calculator, text="÷", command=inputdivide, width=7, height=2)
divide.place(x=180, y=40)
add = Button(Calculator, text=" ", command=inputplus, width=7, height=2)
add.place(x=180, y=80)
minus = Button(Calculator, text="-", command=inputminus, width=7, height=2)
minus.place(x=180, y=120)
dot = Button(Calculator, text=".", command=inputdecimal, width=7, height=2)
dot.place(x=120, y=120)
equals = Button(Calculator, text="=", command=equal, width=7, height=2)
equals.place(x=60, y=120)

Calculator.mainloop()

Now it will print the result when you click "=" button. There is no input needed because you give the value by clicking button

  • Related