用python实现的简易计算器

来源:互联网 发布:python重定向到文件 编辑:程序博客网 时间:2024/05/28 11:47
from tkinter import *class Change:    def __init__(self):        window=Tk()        window.title("Button deal")        s=''        frame1=Frame(window)        frame2=Frame(window)        frame2.pack()        frame1.pack()        button0=Button(frame1,text='0',command=self.pressb0)        button1=Button(frame1,text='1',command=self.pressb1)        button2=Button(frame1,text='2',command=self.pressb2)        button3=Button(frame1,text='3',command=self.pressb3)        button4=Button(frame1,text='4',command=self.pressb4)        button5=Button(frame1,text='5',command=self.pressb5)        button6=Button(frame1,text='6',command=self.pressb6)        button7=Button(frame1,text='7',command=self.pressb7)        button8=Button(frame1,text='8',command=self.pressb8)        button9=Button(frame1,text='9',command=self.pressb9)        buttona=Button(frame1,text='+',command=self.pressba)        buttonb=Button(frame1,text='-',command=self.pressbb)        buttonc=Button(frame1,text='*',command=self.pressbc)        buttond=Button(frame1,text='/',command=self.pressbd)        buttone=Button(frame1,text='c',command=self.pressbe)        buttonf=Button(frame1,text='=',command=self.pressbf)        self.label=Label(frame2,text=s)        self.label.pack()        #self.label.grid(row=1)        button0.grid(row=5,column=2)        button1.grid(row=2,column=1)        button2.grid(row=2,column=2)        button3.grid(row=2,column=3)        button4.grid(row=3,column=1)        button5.grid(row=3,column=2)        button6.grid(row=3,column=3)        button7.grid(row=4,column=1)        button8.grid(row=4,column=2)        button9.grid(row=4,column=3)        buttona.grid(row=2,column=4)        buttonb.grid(row=3,column=4)        buttonc.grid(row=4,column=4)        buttond.grid(row=5,column=1)        buttone.grid(row=5,column=3)        buttonf.grid(row=5,column=4)                                window.mainloop()    def pressb0(self):        self.label['text']+='0'    def pressb1(self):        self.label['text']+='1'    def pressb2(self):        self.label['text']+='2'    def pressb3(self):        self.label['text']+='3'    def pressb4(self):        self.label['text']+='4'    def pressb5(self):        self.label['text']+='5'    def pressb6(self):        self.label['text']+='6'    def pressb7(self):        self.label['text']+='7'    def pressb8(self):        self.label['text']+='8'    def pressb9(self):        self.label['text']+='9'    def pressba(self):        self.label['text']+='+'    def pressbb(self):        self.label['text']+='-'    def pressbc(self):        self.label['text']+='*'    def pressbd(self):        self.label['text']+='/'    def pressbe(self):#清除        self.label['text']=''    def pressbf(self):        self.label['text']=str(eval(self.label['text']))Change()

写的有点长(新手),可以用循环去简化一些代码,这是我写的第一个GUI程序,可能中间会有bug,但是通过这个程序,自己确实也学到了很多东西。

下面说一下我对类中self新的理解吧,self就相当于C语言中的define,使用self的变量就相当于一个全局变量,调用后就可以在整个类的函数中都可以改变它。

原创粉丝点击