-1

I'm new to coding, I'm trying to replicate a code for a simple calculator, when I try to run the module is gives me a restart error and nothing happens

code is

from tkinter import*

def frame(root, side):
    w=Frame(root)
    w.pack(side=side, expand=YES, fill=BOTH)
    return w

def button(root,side,text, command=None):
    w=Button(root,text=text, command=command)
    w.pack(side=side, expand=YES,fill=BOTH)
    return w

class Calculator(Frame):
    def __init__ (self):
        Frame.__init__(self)
        self.pack(expand=YES, fill=BOTH)
        self.master.tittle('simple Calculator')
        self.master.iconname('calcl')

        display = StringVar()
        Entry(self, relief=SUNKEN,
              textvariable=display).pack(side=TOP, expand=YES, fill=BOTH)
        for key in ('123', '456', '789' '-0'):
            keyF= frame(self, TOP)
            for char in ket:
                button (keyF, LEFT, char, lambda w=display, s='%s '%char:w.set(w.get()+s))
            opsF= frame(self, TOP)
            for char in '+-/*=':
                if char=='=':
                    btn=button(opsF, LEFT, char)
                    btn.bind('<ButtonRelease-1>',lambda e, s=self, w=display: s.calc(w), '+')
                else:
                    btn=button (opsF, LEFT, char, lambda w=display, c=chair: w.set(w.get()+' '+c+' '))
            clearF = frame (self, BOTTOM)
            button(clearF, LEFT, 'CLr', lambda w=display: w.set(''))

    def calc(self, display):
        try:
             display.set('eval(display.get())')
        except ValueError:
                 display.set('ERROR')

if __name__ == 'main__':
    Calculator().mainloop()

error :

= RESTART: C:\Users\sibiyass\AppData\Local\Programs\Python\Python36\calc1.py =

Bentaye
  • 9,403
  • 5
  • 32
  • 45
SANELE
  • 1
  • 2

1 Answers1

0

It is not an error.

The line

= RESTART: C:\Users\sibiyass\AppData\Local\Programs\Python\Python36\calc1.py =

says that python is restarting your code and it shows the path of your code under file name calc1.py

It is restarting because your code is not producing any output.

And your program was not producing any output because there were many keyword errors. I would suggest that you run your code by replacing if __name__ == '__main__' by main() and wrapping Calculator().mainloop() inside the definition of main(). Keep correcting the code.

To be specific, your code has following errors:

1) On the line 33, chair should be char

2) On the line 17, tittle should be 'title'

3) On the line 25, ket should be key

Your modified code should look like:

from tkinter import*

def frame(root, side):
    w=Frame(root)
    w.pack(side=side, expand=YES, fill=BOTH)
    return w

def button(root,side,text, command=None):
    w=Button(root,text=text, command=command)
    w.pack(side=side, expand=YES,fill=BOTH)
    return w

class Calculator(Frame):
    def __init__ (self):
        Frame.__init__(self)
        self.pack(expand=YES, fill=BOTH)
        self.master.title('simple Calculator')
        self.master.iconname('calcl')

        display = StringVar()
        Entry(self, relief=SUNKEN,
              textvariable=display).pack(side=TOP, expand=YES, fill=BOTH)
        for key in ('123', '456', '789' '-0'):
            keyF= frame(self, TOP)
            for char in key:
                button (keyF, LEFT, char, lambda w=display, s='%s '%char:w.set(w.get()+s))
            opsF= frame(self, TOP)
            for char in '+-/*=':
                if char=='=':
                    btn=button(opsF, LEFT, char)
                    btn.bind('<ButtonRelease-1>',lambda e, s=self, w=display: s.calc(w), '+')
                else:
                    btn=button (opsF, LEFT, char, lambda w=display, c=char: w.set(w.get()+' '+c+' '))
            clearF = frame (self, BOTTOM)
            button(clearF, LEFT, 'CLr', lambda w=display: w.set(''))

    def calc(self, display):
        try:
             display.set('eval(display.get())')
        except ValueError:
                 display.set('ERROR')

def main():
    Calculator().mainloop()

main()

Try running it.

harshvardhan
  • 765
  • 13
  • 32