資源簡介
簡易計算器
代碼片段和文件信息
#?-*-?encoding:?utf-8?-*-
#?please?input?your?code?here.
from?tkinter?import?*
def?clear():???????????#定義函數清空表達式
????display.set(‘‘)
def?nExp(num):???????????????#定義函數將新字符添加進表達式
????exp=display.get()+num
????display.set(exp)
def?calculate():??????????????????#定義函數計算表達式結果
????try:
????????exp=display.get()
????????result=eval(exp)???????????????????#調用內置函數
????????display.set(exp+‘=‘+str(result))
????except:
????????display.set(‘Error!‘)?????????????????????????????????????????#如果表達式不合法則提示錯誤
root=Tk()?????????????????????????????????????????????????????????????#繪制窗口
root.title(‘簡易計算器‘)???????????????????????????????????????????????#給窗口命名
display=StringVar()???????????????????????????????????????????????????#定義表達式為交互式對象
LabelA=Label(rootrelief=‘sunken‘borderwidth=3anchor=SE)????????????
- 上一篇:Python 自定義FTP功能
- 下一篇:python 入門
評論
共有 條評論