Python實現(xiàn)的計算器功能示例
本文實例講述了Python實現(xiàn)的計算器功能。分享給大家供大家參考,具體如下:
源碼:
# -*- coding:utf-8 -*-
#! python2
from tkinter import *
__author__ = 'tianshl'
__date__ = '2017/10/16'
class Application(Frame):
def __init__(self):
Frame.__init__(self)
self.grid()
self.mem = '' # 內(nèi)存中的數(shù)據(jù)
self.opt = '' # 操作符
self.display = StringVar() # 顯示的數(shù)據(jù)
self.display.set('0') # 初始值
self.need_cls = False # 是否需要清屏
self.create_widgets()
# 清空
def clear(self):
self.mem = ''
self.display.set('0')
# 取反
def negative(self):
self.display.set(eval('-' + self.display.get()))
# 四則運算
def option(self, opt):
if not self.need_cls:
self.calculate()
self.opt = opt
self.need_cls = True
self.mem = self.display.get()
# 計算結(jié)果
def calculate(self):
if self.opt:
try:
self.display.set(eval(self.mem + self.opt + self.display.get()))
except Exception:
self.display.set('錯誤')
self.need_cls = True
self.opt = ''
self.mem = ''
# 百分比
def percent(self):
base = float(self.mem or 1) / 100
display = eval('{}*{}'.format(self.display.get(), base))
int_display = int(display)
display = int_display if display == int_display else display
self.display.set(display)
self.need_cls = True
# 輸入
def input(self, key):
if self.need_cls:
self.display.set('0')
self.need_cls = False
display = self.display.get()
if display == '0' and key != '.':
self.display.set(key)
else:
if '.' in display and key == '.':
return
self.display.set(display + key)
# 創(chuàng)建組件
def create_widgets(self):
# 顯示框
Entry(self, textvariable=self.display, state="readonly", width=35).grid(
row=0, column=0, columnspan=4)
# 鍵盤
keyboards = [
['C', '+/-', '%', '/'],
['7', '8', '9', '*'],
['4', '5', '6', '-'],
['1', '2', '3', '+'],
['0', '.', '=']
]
for row, keys in enumerate(keyboards):
row_num = 3 + row
for col, key in enumerate(keys):
if key == 'C':
command = self.clear
elif key == '+/-':
command = self.negative
elif key == '%':
command = self.percent
elif key in ['+', '-', '*', '/']:
command = lambda s=key: self.option(s)
elif key == '=':
command = self.calculate
else:
command = lambda s=key: self.input(s)
bt = Button(self, text=key, command=command, width=6)
bt.grid(row=row_num, column=col)
app = Application()
# 設(shè)置窗口標題:
app.master.title('www.dhdzp.com - 計算器')
# 設(shè)置窗口尺寸/位置
app.master.geometry("326x170+200+200")
# 設(shè)置窗口不可變
app.master.resizable(width=False, height=False)
# 主消息循環(huán):
app.mainloop()
運行效果:

PS:這里再為大家推薦幾款計算工具供大家進一步參考借鑒:
在線一元函數(shù)(方程)求解計算工具:
http://tools.jb51.net/jisuanqi/equ_jisuanqi
科學(xué)計算器在線使用_高級計算器在線計算:
http://tools.jb51.net/jisuanqi/jsqkexue
在線計算器_標準計算器:
http://tools.jb51.net/jisuanqi/jsq
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學(xué)運算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
python pytest進階之xunit fixture詳解
這篇文章主要介紹了python pytest進階之xunit fixture詳解,了解unittest的同學(xué)應(yīng)該知道我們在初始化環(huán)境和銷毀工作時,unittest使用的是setUp,tearDown方法,那么在pytest框架中同樣存在類似的方法,今天我們就來具體說明,需要的朋友可以參考下2019-06-06
python面試題Python2.x和Python3.x的區(qū)別
這篇文章主要介紹了python面試題Python2.x和Python3.x的區(qū)別 ,在面試中也經(jīng)常會問到,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05
Python使用Py2neo創(chuàng)建Neo4j的節(jié)點和關(guān)系
Neo4j是一款開源圖數(shù)據(jù)庫,使用Python語言訪問Neo4j可以使用Py2neo。本文介紹了使用Py2neo訪問Neo4j,批量創(chuàng)建節(jié)點和關(guān)系的方法2021-08-08
關(guān)于PyCharm安裝后修改路徑名稱使其可重新打開的問題
這篇文章主要介紹了關(guān)于PyCharm安裝后修改路徑名稱使其可重新打開的問題,本文通過圖文實例相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10

