我是编码的新手,我正在尝试复制一个简单计算器的代码,当我尝试运行模块时,会给我一个重启错误,没有任何反应
代码是
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()
错误:
= RESTART:C:\ Users \ sibiyass \ AppData \ Local \ Programs \ Python \ Python36 \ calc1.py =
答案 0 :(得分:0)
这不是错误。
该行
= RESTART: C:\Users\sibiyass\AppData\Local\Programs\Python\Python36\calc1.py =
说python正在重新启动你的代码,它会在文件名calc1.py
下显示代码的路径
它正在重新启动,因为您的代码没有产生任何输出。
并且您的程序没有产生任何输出,因为存在许多关键字错误。我建议您运行代码,将if __name__ == '__main__'
替换为main()
并将Calculator().mainloop()
包含在main()
的定义中。继续纠正代码。
具体而言,您的代码有以下错误:
1)在第33行,chair
应为char
2)在第17行,tittle
应该是'title'
3)在第25行,ket
应为key
您修改后的代码应如下所示:
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()
尝试运行它。