1. 程式人生 > >利用tkinter實現簡單計算器功能(不使用eval函式)

利用tkinter實現簡單計算器功能(不使用eval函式)

利用tkinter實現簡單計算器功能(不使用eval函式)

一、思路

tkinter:

  • 佈置主介面;
  • 上部為數字顯示介面;
  • 下部為數字鍵與功能鍵介面;

邏輯:

  • 程式只考慮兩個運算元進行計算的情況,不考慮複雜情況

展示:

在這裡插入圖片描述

二、程式碼實現

根據上述思路,利用python實現簡單計算器程式,程式碼如下:

# 第一步:畫出圖形介面-上部
from tkinter import *

root = Tk() # 建立舞臺
# 定義面板的大小
root.geometry('250x380')
root.title('Calculator'
) # 定義面板 # Frame:框架,在螢幕上建立一塊矩形區域,多作為容器來佈局窗體 # bg代表背景顏色background,#dddddd是十六進位制表示顏色的一個串 frame_show = Frame(width=300,height=150,bg='#dddddd') frame_show.pack() # 定義頂部區域,設定字串-初始化為0 sv = StringVar() sv.set('0') # anchor:定義控制元件的錨點,西對齊”w”,東對齊”e”,北對齊”n”,南對齊”s”,預設為” center” # 還有“nw”, “sw”, “se”, “ne” # Label中的width和height單位不是畫素,而是佔據字元的位數
# font代表字型 show_label = Label(frame_show,textvariable=sv, \ bg='green', width=12, height=1, \ font=("黑體", 20, 'bold'), \ justify=LEFT, anchor='e') #用label顯示字串 # padx和pady用於設定框架的外部填充顯示,ipadx和ipady用於設定框架的內部顯示 show_label.pack(ipadx=5, ipady=5, padx=
10, pady=10) #frame_show.pack() # 第二步:畫出圖形介面-下部 # 定義退格函式 def delete(): # 先用get獲取sv的值,然後刪掉其最後一位 sv.set(sv.get()[:-1]) # 定義取反函式 def inverse(): # 先用get獲取sv的值,然後轉換為整形,再乘-1 inverse_value = float(sv.get()) * -1 sv.set(inverse_value) # 定義清零函式 def clear(): global num1, num2, operator sv.set('0') num1 = '' num2 = '' operator = '' # 定義下部按鍵區域 frame_bord = Frame(width=400, height=350, bg='#cccccc') button_del = Button(frame_bord, text="←", width=5, height=1, \ command=delete).grid(row=0, column=1) button_clear = Button(frame_bord, text='C', width=5, height=1, \ command=clear).grid(row=0, column=0) button_inverse = Button(frame_bord, text='±', width=5, height=1, \ command=inverse).grid(row=0, column=2) button_ce = Button(frame_bord, text='CE', width=5, height=1, \ command=clear).grid(row=0, column=3) ''' 考慮以下幾種情況: 1. 按下數字 2. 按下操作符號 3,只考慮兩個運算元操作,比考慮複雜情況 ''' # 初始化兩個數字與運算子 num1 = '' num2 = '' operator = '' def operation(op): # 顯示運算子 global num1, num2, operator if op in ['+', '-', 'x', '/']: # 用於在螢幕上顯示並存儲 operator = op sv.set(num1 + operator) if num2: # 當num2被賦值時 rst = 0 # 由於當num2被賦值時,並不會呼叫該函式,因此需要在op為“=”時,使用上步四則運算子資訊進行運算 # operator代表用於儲存上次呼叫該函式時的運算子 if operator == "+": rst = float(num1) + float(num2) if operator == "-": rst = float(num1) - float(num2) if operator == "x": rst = float(num1) * float(num2) if operator == "/": rst = float(num1) / float(num2) # “=”不用顯示在螢幕上,所以用op if op == "=": # 輸出結果,控制顯示的數值位數,最多為10位 sv.set(str(rst)[:10]) # 將上一步的計算結果賦值給num1,清除num2和operator的 num1 = str(rst) num2 = '' operator = '' # 定義加減乘除四則運算,與等於按鈕 b_plus = Button(frame_bord, text='+', width=5, height=2, \ command=lambda: operation('+')).grid(row=1, column=3) b_minus = Button(frame_bord, text='-', width=5, height=2, \ command=lambda: operation('-')).grid(row=2, column=3) b_times = Button(frame_bord, text='x', width=5, height=2, \ command=lambda: operation('x')).grid(row=3, column=3) b_divide = Button(frame_bord, text='/', width=5, height=2, \ command=lambda: operation('/')).grid(row=4, column=3) b_equal = Button(frame_bord, text='=', width=5, height=2, \ command=lambda: operation('=')).grid(row=4, column=2) # 定義數字改變函式 def change(num): ''' 按下一個數字需要考慮兩種情況: 1. 數字屬於第一個運算元 2. 數字屬於第二個運算元 3. 判斷是否屬於第一個運算元,可以通過operator判斷 ''' # 加入運算元是None,表明肯定是第一個運算元 global num1, num2, operator # None, False, 空字串"", 0, 空列表[], 空字典{}, 空元組()都相當於False, not Flase即為True if not operator: num1 = num1 + num # 如果是第一個運算元,則只顯示第一個運算元 sv.set(num1) else: num2 = num2 + num # 如果是第二個運算元 ,則應該顯示完整的計算式子 sv.set(num1 + operator + num2) # 計算器數字區域,形態分佈與小鍵盤數字類似 b_0 = Button(frame_bord, text='0', width=5, height=2, \ command=lambda: change("0")).grid(row=4, column=0) b_1 = Button(frame_bord, text='1', width=5, height=2, \ command=lambda: change("1")).grid(row=3, column=0) b_2 = Button(frame_bord, text='2', width=5, height=2, \ command=lambda: change("2")).grid(row=3, column=1) b_3 = Button(frame_bord, text='3', width=5, height=2, \ command=lambda: change("3")).grid(row=3, column=2) b_4 = Button(frame_bord, text='4', width=5, height=2, \ command=lambda: change("4")).grid(row=2, column=0) b_5 = Button(frame_bord, text='5', width=5, height=2, \ command=lambda: change("5")).grid(row=2, column=1) b_6 = Button(frame_bord, text='6', width=5, height=2, \ command=lambda: change("6")).grid(row=2, column=2) b_7 = Button(frame_bord, text='7', width=5, height=2, \ command=lambda: change("7")).grid(row=1, column=0) b_8 = Button(frame_bord, text='8', width=5, height=2, \ command=lambda: change("8")).grid(row=1, column=1) b_9 = Button(frame_bord, text='9', width=5, height=2, \ command=lambda: change("9")).grid(row=1, column=2) b_d = Button(frame_bord, text='.', width=5, height=2, \ command=lambda: change(".")).grid(row=4, column=1) frame_bord.pack(padx=10, pady=10) root.mainloop()