首頁 > 軟體

Python實現簡易計算器的範例程式碼

2022-11-23 14:00:55

上次我用我學習的python做一個簡易的計算器,我對計算器進行了,更改優化,變成了一個真正的計算器

實現流程

1.計算機佈局

2.計算機執行

首先匯入模組:

Tkinter 作為 Python GUI 開發工具之一,它具有 GUI 軟體包的必備的常用功能。比如,它提供了十多種不同型別的視窗控制元件、視窗佈局管理器、事件處理機制等,加之其開發效率高、程式碼簡潔易讀

import tkinter as tk
#Python3標準安裝包中自帶tkinter,即不用安裝,匯入即可使用。
#可以從命令列執行python -m tkinter會開啟一個視窗
#演示一個簡單的Tk介面,證明tkinter已正確安裝在系統上.
#python -m tkinter

計算器佈局

1、先完成整個計算機的佈局及其它輸出結果的一欄

前面完成整體佈局就細化計算器中的按鈕

1、前四行程式碼是定義按鈕的符號、長寬、字型、顏色

2、後四行是按鈕的間距

root = tk.Tk()
root.title('計算器')
root.geometry('295x280+100+100') #邊距
font = ('宋體', 20)
font_16 = ('宋體', 16)
root.attributes("-alpha", 0.9) #設定透明度
 
result_num = tk.StringVar() #賦予可變變數
result_num.set('')
 
tk.Label(root,
         textvariable=result_num, font=font, height=2,
         width=20, justify=tk.LEFT, anchor=tk.SE
         ).grid(row=1, column=1, columnspan=4) #顯示結果的佈局
 
 
button_clear = tk.Button(root, text='C', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') #定義按鈕的符號、長寬、字型、顏色
button_back = tk.Button(root, text='←', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') #定義按鈕的符號、長寬、字型、顏色
button_division = tk.Button(root, text='÷', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') #定義按鈕的符號、長寬、字型、顏色
button_multiplication = tk.Button(root, text='×', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') #定義按鈕的符號、長寬、字型、顏色
button_clear.grid(row=2, column=1, padx=4, pady=2)
button_back.grid(row=2, column=2, padx=4, pady=2)
button_division.grid(row=2, column=3, padx=4, pady=2)
button_multiplication.grid(row=2, column=4, padx=4, pady=2) #定義按鈕位置
 
#依葫蘆畫瓢
button_seven = tk.Button(root, text='7', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_eight = tk.Button(root, text='8', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_nine = tk.Button(root, text='9', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_subtraction = tk.Button(root, text='-', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_seven.grid(row=3, column=1, padx=4, pady=2)
button_eight.grid(row=3, column=2, padx=4, pady=2)
button_nine.grid(row=3, column=3, padx=4, pady=2)
button_subtraction.grid(row=3, column=4, padx=4, pady=2)
 
#依葫蘆畫瓢
button_four = tk.Button(root, text='4', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_five = tk.Button(root, text='5', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_six = tk.Button(root, text='6', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_addition = tk.Button(root, text='+', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_four.grid(row=4, column=1, padx=4, pady=2)
button_five.grid(row=4, column=2, padx=4, pady=2)
button_six.grid(row=4, column=3, padx=4, pady=2)
button_addition.grid(row=4, column=4, padx=4, pady=2)
 
#依葫蘆畫瓢
button_one = tk.Button(root, text='1', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_two = tk.Button(root, text='2', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_three = tk.Button(root, text='3', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_equal = tk.Button(root, text='=', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_one.grid(row=5, column=1, padx=4, pady=2)
button_two.grid(row=5, column=2, padx=4, pady=2)
button_three.grid(row=5, column=3, padx=4, pady=2)
button_equal.grid(row=5, column=4, padx=4, pady=2)
 
 
button_zero1 = tk.Button(root, text=' ', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_zero = tk.Button(root, text='0', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_dot = tk.Button(root, text='.', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_equal2 = tk.Button(root, text=' ', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_zero1.grid(row=6, column=1, padx=4, pady=2)
button_zero.grid(row=6, column=2, padx=4, pady=2)
button_dot.grid(row=6, column=3, padx=4, pady=2)
button_equal2.grid(row=6, column=4, padx=4, pady=2)

計算機執行

計算器佈局完成後(主體和細節) 賦予計算器計算能力

1、點選按鈕實現出現結果輸出欄及計算器的輸出欄

2、賦予事件能力

def click_button(x):
    print('X:t', x)
    result_num.set(result_num.get() + x)

點選按鈕可以輸入所對應的結果 並且呼叫lambda函數計算輸入結果並反饋

ambda 函數是匿名的: 所謂匿名函數,通俗地說就是沒有名字的函數。lambda函數沒有名字。 lambda 函數有輸入和輸出: 輸入是傳入到參數列argument_list的值,輸出是根據表示式expression計算得到的值。

button_one.config(command=lambda: click_button('1'))
button_two.config(command=lambda: click_button('2'))
button_three.config(command=lambda: click_button('3'))
button_four.config(command=lambda: click_button('4'))
button_five.config(command=lambda: click_button('5'))
button_six.config(command=lambda: click_button('6'))
button_seven.config(command=lambda: click_button('7'))
button_eight.config(command=lambda: click_button('8'))
button_nine.config(command=lambda: click_button('9'))
button_zero.config(command=lambda: click_button('0'))
button_dot.config(command=lambda: click_button('.'))
button_addition.config(command=lambda: click_button('+'))
button_subtraction.config(command=lambda: click_button('-'))
button_multiplication.config(command=lambda: click_button('*'))
button_division.config(command=lambda: click_button('/'))
button_clear.config(command=lambda: btnClearDisplay())
button_equal.config(command=calculation)

呼叫calculation時間用來計算輸入結果 其中str來轉化字串.

呼叫btnClearDisplay清除來清空輸入結果

def calculation():
    opt_str = result_num.get()
    result = eval(opt_str)
    result_num.set(str(result))
 
 
def btnClearDisplay():
    opt_str = result_num.get()
    result = eval(str(opt_str))
    result_num.set("")

最後一行程式碼不需要觸發lambda 因為不需要匿名函數也不用傳遞引數     

button_clear.config(command=lambda: btnClearDisplay())
button_equal.config(command=calculation)

程式碼展示

import tkinter as tk
 
root = tk.Tk()
root.title('計算器')
root.geometry('295x280+100+100')
font = ('宋體', 20)
font_16 = ('宋體', 16)
root.attributes("-alpha", 0.9)
 
result_num = tk.StringVar()
result_num.set('')
 
tk.Label(root,
         textvariable=result_num, font=font, height=2,
         width=20, justify=tk.LEFT, anchor=tk.SE
         ).grid(row=1, column=1, columnspan=4)
 
 
button_clear = tk.Button(root, text='C', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_back = tk.Button(root, text='←', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_division = tk.Button(root, text='÷', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_multiplication = tk.Button(root, text='×', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_clear.grid(row=2, column=1, padx=4, pady=2)
button_back.grid(row=2, column=2, padx=4, pady=2)
button_division.grid(row=2, column=3, padx=4, pady=2)
button_multiplication.grid(row=2, column=4, padx=4, pady=2)
 
 
button_seven = tk.Button(root, text='7', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_eight = tk.Button(root, text='8', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_nine = tk.Button(root, text='9', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_subtraction = tk.Button(root, text='-', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_seven.grid(row=3, column=1, padx=4, pady=2)
button_eight.grid(row=3, column=2, padx=4, pady=2)
button_nine.grid(row=3, column=3, padx=4, pady=2)
button_subtraction.grid(row=3, column=4, padx=4, pady=2)
 
button_four = tk.Button(root, text='4', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_five = tk.Button(root, text='5', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_six = tk.Button(root, text='6', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_addition = tk.Button(root, text='+', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_four.grid(row=4, column=1, padx=4, pady=2)
button_five.grid(row=4, column=2, padx=4, pady=2)
button_six.grid(row=4, column=3, padx=4, pady=2)
button_addition.grid(row=4, column=4, padx=4, pady=2)
 
 
button_one = tk.Button(root, text='1', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_two = tk.Button(root, text='2', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_three = tk.Button(root, text='3', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_equal = tk.Button(root, text='=', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_one.grid(row=5, column=1, padx=4, pady=2)
button_two.grid(row=5, column=2, padx=4, pady=2)
button_three.grid(row=5, column=3, padx=4, pady=2)
button_equal.grid(row=5, column=4, padx=4, pady=2)
 
 
button_zero1 = tk.Button(root, text=' ', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_zero = tk.Button(root, text='0', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_dot = tk.Button(root, text='.', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_equal2 = tk.Button(root, text=' ', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_zero1.grid(row=6, column=1, padx=4, pady=2)
button_zero.grid(row=6, column=2, padx=4, pady=2)
button_dot.grid(row=6, column=3, padx=4, pady=2)
button_equal2.grid(row=6, column=4, padx=4, pady=2)
 
 
def click_button(x):
    print('X:t', x)
    result_num.set(result_num.get() + x)
 
 
def calculation():
    opt_str = result_num.get()
    result = eval(opt_str)
    result_num.set(str(result))
 
 
def btnClearDisplay():
    opt_str = result_num.get()
    result = eval(str(opt_str))
    result_num.set("")
 
 
button_one.config(command=lambda: click_button('1'))
button_two.config(command=lambda: click_button('2'))
button_three.config(command=lambda: click_button('3'))
button_four.config(command=lambda: click_button('4'))
button_five.config(command=lambda: click_button('5'))
button_six.config(command=lambda: click_button('6'))
button_seven.config(command=lambda: click_button('7'))
button_eight.config(command=lambda: click_button('8'))
button_nine.config(command=lambda: click_button('9'))
button_zero.config(command=lambda: click_button('0'))
button_dot.config(command=lambda: click_button('.'))
button_addition.config(command=lambda: click_button('+'))
button_subtraction.config(command=lambda: click_button('-'))
button_multiplication.config(command=lambda: click_button('*'))
button_division.config(command=lambda: click_button('/'))
button_clear.config(command=lambda: btnClearDisplay())
button_equal.config(command=calculation)
 
 
root.mainloop()

執行展示

可以看到計算器的頁面特別好看,跟著我一起學python,成就感滿滿 !!!

到此這篇關於Python實現簡易計算器的範例程式碼的文章就介紹到這了,更多相關Python計算器內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com