前言
大家早好、午好、晚好吖 ? ~歡迎光臨本文章
在我們手機(jī)上大家都有一個(gè)計(jì)算器,對(duì)吧
那它這功能是怎么實(shí)現(xiàn)的呢?
今天我們?cè)陔娔X上來(lái)實(shí)現(xiàn)一個(gè)電腦端計(jì)算器界面~
開(kāi)發(fā)環(huán)境:
-
Python 3.8 / 編譯器
-
Pycharm 2021.2版本 / 編輯器
本文所有模塊\環(huán)境\源碼\教程皆可點(diǎn)擊文章下方名片獲取此處跳轉(zhuǎn)
代碼展示
界面設(shè)置
1.導(dǎo)入模塊
import tkinter as tk
2.實(shí)例化一個(gè)窗體對(duì)象
root = tk.Tk()
3.標(biāo)題
root.title('計(jì)算器')
4.大小以及出現(xiàn)的位置
root.geometry("295x280+150+150")
5.透明度
root.attributes("-alpha", 0.9)
6.背景
root["background"] = "#ffffff"
7.標(biāo)簽
lable1 = tk.Label(root, textvariable=result_num, width=20, height=2, font=('宋體', 20), justify='left', background='#ffffff', anchor='se')
8.布局
lable1.grid(padx=4, pady=4, row=0, column=0, columnspan=4)
9.按鈕
button_clear = tk.Button(root, text='C', width=5, font=('宋體', 16), relief='flat', background='#C0C0C0', command=lambda: clear())
button_back = tk.Button(root, text='←', width=5, font=('宋體', 16), relief='flat', background='#C0C0C0', command=lambda: back())
button_division = tk.Button(root, text='/', width=5, font=('宋體', 16), relief='flat', background='#C0C0C0', command=lambda: operator('/'))
button_multiplication = tk.Button(root, text='x', width=5, font=('宋體', 16), relief='flat', background='#C0C0C0', command=lambda: operator('*'))
button_clear .grid(padx=4, row=1, column=0)
button_back .grid(padx=4, row=1, column=1)
button_division .grid(padx=4, row=1, column=2)
button_multiplication .grid(padx=4, row=1, column=3)
button_seven = tk.Button(root, text='7', width=5, font=('宋體', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('7'))
button_eight = tk.Button(root, text='8', width=5, font=('宋體', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('8'))
button_nine = tk.Button(root, text='9', width=5, font=('宋體', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('9'))
button_subtraction = tk.Button(root, text='—', width=5, font=('宋體', 16), relief='flat', background='#C0C0C0', command=lambda: operator('-'))
button_seven .grid(padx=4, row=2, column=0)
button_eight .grid(padx=4, row=2, column=1)
button_nine .grid(padx=4, row=2, column=2)
button_subtraction .grid(padx=4, row=2, column=3)
button_four = tk.Button(root, text='4', width=5, font=('宋體', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('4'))
button_four.grid(padx=4, pady=4, row=3, column=0)
button_five = tk.Button(root, text='5', width=5, font=('宋體', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('5'))
button_five.grid(padx=4, row=3, column=1)
button_six = tk.Button(root, text='6', width=5, font=('宋體', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('6'))
button_six.grid(padx=4, row=3, column=2)
button_addition = tk.Button(root, text='+', width=5, font=('宋體', 16), relief='flat', background='#C0C0C0', command=lambda: operator('+'))
button_addition.grid(padx=4, row=3, column=3)
button_one = tk.Button(root, text='1', width=5, font=('宋體', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('1'))
button_one.grid(padx=4, row=4, column=0)
button_two = tk.Button(root, text='2', width=5, font=('宋體', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('2'))
button_two.grid(padx=4, row=4, column=1)
button_three = tk.Button(root, text='3', width=5, font=('宋體', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('3'))
button_three.grid(padx=4, row=4, column=2)
button_equal = tk.Button(root, text='=', width=5, height=3, font=('宋體', 16), relief='flat', background='#C0C0C0', command=lambda: equal())
button_equal.grid(padx=4, row=4, rowspan=5, column=3)
button_zero = tk.Button(root, text='0', width=12, font=('宋體', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('0'))
button_zero.grid(padx=4, pady=4, row=5, column=0, columnspan=2)
button_decimal = tk.Button(root, text='.', width=5, font=('宋體', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('.'))
button_decimal.grid(padx=4, row=5, column=2)
現(xiàn)在得出界面效果
功能
添加數(shù)字
def append_num(i):
lists.append(i)
result_num.set(''.join(lists))
選擇運(yùn)算符號(hào)
def operator(i):
if len(lists) > 0:
if lists[-1] in ['+', '-', '*', '/']:
lists[-1] = i
else:
lists.append(i)
result_num.set(''.join(lists))
清零
def clear():
lists.clear()
result_num.set(0)
退格
def back():
del lists[-1]
result_num.set(lists)
等號(hào)
def equal():
a = ''.join(lists)
end_num = eval(a)
result_num.set(end_num)
lists.clear()
lists.append(str(end_num))
定義一個(gè)列表收集輸入的內(nèi)容
lists = []
result_num = tk.StringVar()
result_num.set(0)
最后運(yùn)行代碼,效果如下圖
先試試
運(yùn)算得出結(jié)果
尾語(yǔ) ??
好了,今天的分享就差不多到這里了!
完整代碼、更多資源、疑惑解答直接點(diǎn)擊下方名片自取即可。
對(duì)下一篇大家想看什么,可在評(píng)論區(qū)留言哦!看到我會(huì)更新噠(? ?_?)?
喜歡就關(guān)注一下博主,或點(diǎn)贊收藏評(píng)論一下我的文章叭?。?!文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-415371.html
最后,宣傳一下呀~??????更多源碼、資料、素材、解答、交流皆點(diǎn)擊下方名片獲取呀??????文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-415371.html
到了這里,關(guān)于python界面開(kāi)發(fā)案例:制作一個(gè)計(jì)算器軟件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!