python gui界面问题,新手求教
fromTkinterimport*defframe(root,side):w=Frame(root)w.pack(side=side,expand=YES,fill=B...
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."):
deyF=frame(self,TOP)
for char in key:
button(deyF,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.clac(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 clac(self,display):
try:
display.set(eval(display.get()))
except SyntaxError:
display.set("ERROR")
except ZeroDivisionError:
display.set("ZeroDivisionError")
if __name__=="__main__": Calculator().mainloop()
上面的代码是一个网上找到的python计算器界面,小弟python新手,想修改成一个界面,就是可以运行处计算器输入数字可以显示、同时也可以粘贴进去一串数字、点下面的clr不是归0了 而是运行其他的python代码,请问应该如何修改。首先+-*/可以全部删除掉 ,删除中间的一段代码就可以 实现,但是对于clr点击后的效果应该如何改?求高手赐教,QQ280435089 谢谢!
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."):
deyF=frame(self,TOP)
for char in key:
button(deyF,LEFT,char,lambda w=display,s="%s"%char: w.set\
(w.get()+s))
opsF=frame(self,TOP)
clearF=frame(self,BOTTOM)
button(clearF,LEFT,'Clr',lambda w=display: w.set(' '))
def clac(self,display):
try:
display.set(eval(display.get()))
except SyntaxError:
display.set("ERROR")
except ZeroDivisionError:
display.set("ZeroDivisionError")
if __name__=="__main__": Calculator().mainloop() 展开
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."):
deyF=frame(self,TOP)
for char in key:
button(deyF,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.clac(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 clac(self,display):
try:
display.set(eval(display.get()))
except SyntaxError:
display.set("ERROR")
except ZeroDivisionError:
display.set("ZeroDivisionError")
if __name__=="__main__": Calculator().mainloop()
上面的代码是一个网上找到的python计算器界面,小弟python新手,想修改成一个界面,就是可以运行处计算器输入数字可以显示、同时也可以粘贴进去一串数字、点下面的clr不是归0了 而是运行其他的python代码,请问应该如何修改。首先+-*/可以全部删除掉 ,删除中间的一段代码就可以 实现,但是对于clr点击后的效果应该如何改?求高手赐教,QQ280435089 谢谢!
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."):
deyF=frame(self,TOP)
for char in key:
button(deyF,LEFT,char,lambda w=display,s="%s"%char: w.set\
(w.get()+s))
opsF=frame(self,TOP)
clearF=frame(self,BOTTOM)
button(clearF,LEFT,'Clr',lambda w=display: w.set(' '))
def clac(self,display):
try:
display.set(eval(display.get()))
except SyntaxError:
display.set("ERROR")
except ZeroDivisionError:
display.set("ZeroDivisionError")
if __name__=="__main__": Calculator().mainloop() 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询