python 06 計算機

來源:互聯網
上載者:User

標籤:作者   add   個數   負數   justify   cti   odi   imp   layout   

import tkinterimport mathclass Deom:    operation_sign = False    sign1 = False    sign2 = False    list1 = []    list2 = []    def __init__(self):        self.root = tkinter.Tk()        self.root.minsize(320, 400)        self.root.title(‘什麼都算不對牌計算機‘)        self.layout()        self.root.mainloop()    def layout(self):        self.val0 = tkinter.StringVar()        self.val0.set(‘0‘)        self.val = tkinter.StringVar()        self.val.set(‘0‘)  # 文本設定        #標題        def newwindow():                # 相當於主介面            tp = tkinter.Toplevel(width=300, height=330, bg=‘white‘)            # 禁止調整大小            #tp.resizable(width=False, height=False)            # 定義標題            tp.title(‘help‘)            self.val3 = tkinter.StringVar()            self.val3.set(‘實現功能:\n簡易計算機,加減乘除,開方,平方,百分數,分數,正負\n數,clear error ,clear,delete\n解決bug:\n1. 一直點擊運算子號以及等號會連加\n2. 除數為零提示除數不能為0\n3. 小數結果如果大於15位,則保留15位,整數則提示超\n出範圍\n4. 小數點控製為一個,先按小數點會先出零\n5. 已修複目前已知bug\n\n\ntips:\n若發現bug請及時聯絡作者修改!!\n本介面趨於原生\n\n著作權@趙晗python04‘)            # 訊息記錄            label3 = tkinter.Label(tp,  bg = ‘white‘,textvariable = self.val3,anchor = ‘nw‘,justify = ‘left‘)            label3.place(x = 0, y = 0)        # 建立總菜單        menubar = tkinter.Menu(self.root,bg = ‘#F2F2F2‘,activeforeground = ‘white‘,activebackground = ‘#1717FF‘)        # 建立一個下拉式功能表,並且加入檔案菜單        filemenu = tkinter.Menu(menubar,bg = ‘#F2F2F2‘,activeforeground = ‘white‘,activebackground = ‘#1717FF‘)        # 建立下來菜單的選項        filemenu.add_command(label="about",command=newwindow)        # 建立下拉式功能表的分割線        filemenu.add_separator()        filemenu.add_command(label="Exit", command=self.root.quit)        # 將檔案菜單作為下拉式功能表添加到總菜單中,並且將命名為File        menubar.add_cascade(label="help", menu=filemenu)        # 顯示總菜單        self.root.config(menu=menubar)        # 標籤        label0 = tkinter.Label(self.root,textvariable=self.val0, bg=‘white‘, fg = ‘#7B7B7B‘,font=(‘黑體‘, 15), width=‘15‘, anchor=‘e‘,borderwidth=‘10‘).place(x=‘0‘, y=‘0‘,width= 320,height = 40)        # 按鍵)        label1 = tkinter.Label(self.root, textvariable=self.val, bg=‘white‘, font=(‘黑體‘, 25), width=‘15‘, anchor=‘e‘,borderwidth=‘10‘).place(x=‘0‘, y=‘40‘,width = 320,height = 60)        # 按鍵        btn9 = tkinter.Button(self.root, text=‘9‘, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activebackground = ‘#CFEFFE‘, command=lambda :self.num(‘9‘)).place(x=160, y=200, width=80, height=50)        btn8 = tkinter.Button(self.root, text=‘8‘, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activebackground = ‘#CFEFFE‘,command=lambda :self.num(‘8‘)).place(x=80, y=200, width=80, height=50)        btn7 = tkinter.Button(self.root, text=‘7‘, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activebackground = ‘#CFEFFE‘,command=lambda :self.num(‘7‘)).place(x=0, y=200, width=80, height=50)        btn6 = tkinter.Button(self.root, text=‘6‘, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activebackground = ‘#CFEFFE‘,command=lambda :self.num(‘6‘)).place(x=160, y=250, width=80, height=50)        btn5 = tkinter.Button(self.root, text=‘5‘, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activebackground = ‘#CFEFFE‘,command=lambda :self.num(‘5‘)).place(x=80, y=250, width=80, height=50)        btn4 = tkinter.Button(self.root, text=‘4‘, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activebackground = ‘#CFEFFE‘, command=lambda :self.num(‘4‘)).place(x=0, y=250, width=80, height=50)        btn3 = tkinter.Button(self.root, text=‘3‘, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activebackground = ‘#CFEFFE‘, command=lambda :self.num(‘3‘)).place(x=160, y=300, width=80, height=50)        btn2 = tkinter.Button(self.root, text=‘2‘, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activebackground = ‘#CFEFFE‘, command=lambda :self.num(‘2‘)).place(x=80, y=300, width=80, height=50)        btn1 = tkinter.Button(self.root, text=‘1‘, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activebackground = ‘#CFEFFE‘, command=lambda :self.num(‘1‘)).place(x=0, y=300, width=80, height=50)        btn0 = tkinter.Button(self.root, text=‘0‘, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activebackground = ‘#CFEFFE‘, command=lambda :self.num(‘0‘)).place(x=80, y=350, width=80, height=50)        btnsign = tkinter.Button(self.root, text=‘±‘, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activebackground = ‘#CFEFFE‘,command = self.plus_minus).place(x=0, y=350, width=80, height=50)        btnpoint = tkinter.Button(self.root, text=‘.‘, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activebackground = ‘#CFEFFE‘,command=lambda :self.num(‘.‘)).place(x=160, y=350, width=80, height=50)        btndiv = tkinter.Button(self.root, text=‘÷‘, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activebackground = ‘#CFEFFE‘,command=lambda :self.operation(‘/‘)).place(x=240, y=150, width=80, height=50)        btnmul = tkinter.Button(self.root, text=‘ב, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activebackground = ‘#CFEFFE‘,command=lambda :self.operation(‘*‘)).place(x=240, y=200, width=80, height=50)        btnadd = tkinter.Button(self.root, text=‘+‘, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activebackground = ‘#CFEFFE‘, command=lambda :self.operation(‘+‘)).place(x=240, y=300, width=80, height=50)        btnsub = tkinter.Button(self.root, text=‘-‘, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activebackground = ‘#CFEFFE‘,command=lambda :self.operation(‘-‘)).place(x=240, y=250, width=80, height=50)        btnequal = tkinter.Button(self.root, text=‘=‘, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activeforeground = ‘white‘,activebackground = ‘#1717FF‘, command=self.eq_operation).place(x=240, y=350, width=80, height=50)        btndel = tkinter.Button(self.root, text=‘del‘, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activebackground = ‘#CFEFFE‘,command = self.delete_operation).place(x=160, y=150, width=80, height=50)        btnclr = tkinter.Button(self.root, text=‘c‘, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activeforeground = ‘white‘,activebackground = ‘#FE524E‘,command = self.clear_all).place(x=80, y=150, width=80, height=50)        btnce = tkinter.Button(self.root, text=‘ce‘, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activebackground = ‘#CFEFFE‘,command = self.clear_error).place(x=0, y=150, width=80, height=50)        btnpercent = tkinter.Button(self.root, text=‘%‘, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activebackground = ‘#CFEFFE‘,command = self.percent_operation).place(x=0, y=100, width=80, height=50)        btnevolution = tkinter.Button(self.root, text=‘√‘, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activebackground = ‘#CFEFFE‘,command =self.evolution_operation).place(x=80, y=100, width=80, height=50)        btnsquare = tkinter.Button(self.root, text=‘x2‘, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activebackground = ‘#CFEFFE‘,command = self.square_operation).place(x=160, y=100, width=80, height=50)        btngrade = tkinter.Button(self.root, text=‘1/x‘, bg = ‘#F2F2F2‘,font=(‘黑體‘, 15),activebackground = ‘#CFEFFE‘,command = self.grade_operation).place(x=240, y=100, width=80, height=50)    #鍵盤輸入操作    def num(self,number):        a = number        print(number)        ‘‘‘if self.sign2 == True:            if number == ‘0‘:                print(‘error‘)‘‘‘        if self.operation_sign == True:            self.val.set(‘0‘)            self.operation_sign = False        if self.val.get() == ‘0‘:  # 如果螢幕中數字為0,即一開始的預設值,判斷,是0則變為按下的數字不是0,進行拼接            self.val.set(number)            if number == ‘.‘:                self.val.set(‘0‘ + number)        else:  #不是0的情況            if self.val.get().count(‘.‘) == 0:  #如果擷取的字串裡面只有一個.                self.val.set(self.val.get() + number)  # 螢幕所得到的數字  字串串連下一次按入的數字            elif number != ‘.‘:                self.val.set(self.val.get() + number)                ‘‘‘if self.sign2 == True:                    if self.val.get() == ‘0‘:                        print(‘error‘)‘‘‘        #self.sign2 = False        self.sign1 = True        self.sign2 = True    # 正負操作    def plus_minus(self):        list2 = self.val.get()        if list2[0] == ‘-‘:            self.val.set(list2[1:])        elif list2[0] != ‘-‘ and list2 != ‘0‘:            self.val.set(‘-‘ + list2)        self.sign1 = True    #退格操作    def delete_operation(self):        if len(self.val.get()) >1:            self.list1 = list(self.val.get())            self.list1.pop()            result = ‘‘.join(self.list1)            self.val.set(result)        else:            self.val.set(‘0‘)    #平方操作    def square_operation(self):        self.val.get()        self.list1.append(self.val.get())        list2 = ‘** 2‘        self.list1.append(list2)        str1 = ‘‘.join(self.list1)  # 轉換為字串!        print(str1)        self.val0.set(str1)  # 副框顯示字串也可以是列表        result = eval(str1)  # 進行運算,python 代碼        if result > 1:            result1 = str(result)            if len(result1) > 17:                self.val.set(‘結果超過範圍‘)            else:                self.val.set(result)        else:            result1 = round(result, 15)            self.val.set(result1)        print(self.list1)        self.list1.clear()        self.operation_sign = True    #開平方操作    def evolution_operation(self):        try:            self.val.get()            list2 = ‘math.sqrt({0})‘.format(self.val.get())            print(list2)            self.list1.append(list2)            str1= ‘‘.join(self.list1)            print(str1)            self.val.set(eval(str1))            result = eval(str1)  # 進行運算,python 代碼            result1 = round(result, 15)            self.val.set(result1)            self.list1.clear()            self.operation_sign = True        except ValueError:            self.val.set(‘不能對負數開根號‘)    #%操作    def percent_operation(self):        self.val.get()        self.list1.append(self.val.get())        list2 = ‘/ 100‘        self.list1.append(list2)        result = ‘‘.join(self.list1)        self.val.set(eval(result))        print(self.list1)        self.list1.clear()        self.operation_sign = True    #分數    def grade_operation(self):        try:            self.val.get()            print(self.list1)            list2 = ‘1/{}‘.format(self.val.get())            self.list1.append(list2)            str1= ‘‘.join(self.list1)            print(str1)            self.val.set(eval(str1))            result = eval(str1)  # 進行運算,python 代碼            result1 = round(result, 15)            self.val.set(result1)            print(self.list1)            self.list1.clear()            self.operation_sign = True        except ZeroDivisionError:            self.val.set(‘除數不能為零‘)    #清空全部資料操作    def clear_all(self):        self.val.set(‘0‘)        self.list1 = []        self.val0.set(‘0‘)        self.operation_sign = False    #清空錯誤操作    def clear_error(self):        if self.val.get() != ‘‘:            self.val.set(‘0‘)    #運算操作    def operation(self,addnum):        if self.sign1 == True:  #滿足這個條件即可按下運算鍵,否則不可以,即0-9與等號            print(addnum)  # addnum代表符號            print(self.val.get())  # 擷取文本  字串格式            self.list1.append(self.val.get())  # 將文本追加到list1中,此時val.get為文本中內容    列表            self.list1.append(addnum)  # 【2 + 】            print(self.list1)  # 需要判斷運算子號是否按下            self.val0.set(self.list1)        self.operation_sign = True        self.sign1 = False    #等號操作    def eq_operation(self):        try:            if self.sign2 == True:                if self.sign1 == True:   #sign1 為true 可以等於                    self.list1.append(self.val.get())  # 按下等號之後將之前的一個數值傳入list1                    print(self.list1)                    str1 = ‘‘.join(self.list1)  # 轉換為字串!                    print(str1)                    self.val0.set(str1)  # 副框顯示字串也可以是列表                    result = eval(str1)  # 進行運算,python 代碼                    if result > 1:                        result1 = str(result)                        if len(result1) > 15:                            self.val.set(‘結果超過範圍‘)                        else:                            self.val.set(result)                    else:                        result1 = round(result,15)                        self.val.set(result1)                    self.list1.clear()            self.operation_sign = True #此時判斷出按鍵是否按下            self.sign1 = True    #按下等號  變為true  數字可以按下            self.sign2 = False        except ZeroDivisionError:            self.val.set(‘除數不能為零‘)deom = Deom()

  

python 06 計算機

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.