實現簡易計算器

2021-09-24 23:27:32 字數 3758 閱讀 6069

python

剛學python ,老師帶著寫了乙個計算器。話不多說直接上**

from tkinter import* #匯入模組

top=tk() #設定視窗物件

top.title(「計算器」) #設定視窗標題

v = strin**ar() #接受使用者輸入的字元

v.set(「0」)#設定字元預設值

isopera = false #是否按下了運算子

#序列操作

def change(num): #傳參

global isopera #全域性變數

if isopera == false: #如果沒有按下運算子

if v.get() == 『0』:

v.set(』』)

v.set(num)

else:

v.set(v.get()+num)

else:

v.set(num)

isopera = false

#刪除操作

def delete():

if v.get()』』 or v.get()『0』:

v.set(『0』)

return

else:

num = len(v.get())

if num>1:

strnum = v.get()

strnum = strnum[0:num -1]

v.set(strnum)

else:v.set(『0』)

def clear():

global calc

calc =

v.set(『0』)

isopera = false

def percent():

strnum = v.get()

if strnum !=』』 and strnum !=『0』:

v.set(float(strnum)/100)

elif strnum==『0』:

pass

show_text=label(top,textvariable=v,bg=「white」,width=12,height=1,font=(「黑體」,20,「bold」,),anchor=『e』)#書寫label控制項,字型,初始位置

show_text.pack(padx=10,pady=15) #設定控制項與視窗之間的距離

frame_bord = frame(width=400,height=350,bg=』#cccccc』) #框架控制項

#columnspan設定網格中橫跨列數 grid網格 column代表列 row 代表行數

button_del = button(frame_bord,text=『←』,width=5,height=2,bg=』#bbbbbb』,command=delete).grid(row=0,column=0)

button_clear = button(frame_bord,text=『c』,width=5,height=2,bg=』#bbbbbb』,command=clear).grid(row=0,column=1)

button_mod = button(frame_bord,text=』%』,width=5,height=2,bg=』#bbbbbb』,command=percent).grid(row=0,column=2)

#button_mod = button(frame_bord,text=』%』,width=5,height=2,bg=』#bbbbbb』,command=lambda:operation(』%)).grid(row=0,column=2) 取餘

button_division_method = button(frame_bord,text=』/』,width=5,height=2,bg=』#bbbbbb』,command=lambda:operation(』/』)).grid(row=0,column=3)

button_7 = button(frame_bord,text=『7』,width=5,height=2,command=lambda:change(『7』)).grid(row=1,column=0)

button_8 = button(frame_bord,text=『8』,width=5,height=2,command=lambda:change(『8』)).grid(row=1,column=1)

button_9 = button(frame_bord,text=『9』,width=5,height=2,command=lambda:change(『9』)).grid(row=1,column=2)

button_multiplication = button(frame_bord,text=』』,width=5,height=2,bg=』#bbbbbb』,command=lambda:operation(』』)).grid(row=1,column=3)

button_4 = button(frame_bord,text=『4』,width=5,height=2,command=lambda:change(『4』)).grid(row=2,column=0)

button_5 = button(frame_bord,text=『5』,width=5,height=2,command=lambda:change(『5』)).grid(row=2,column=1)

button_6 = button(frame_bord,text=『6』,width=5,height=2,command=lambda:change(『6』)).grid(row=2,column=2)

button_subtraction = button(frame_bord,text=』-』,width=5,height=2,bg=』#bbbbbb』,command=lambda:operation(』-』)).grid(row=2,column=3)

button_1 = button(frame_bord,text=『1』,width=5,height=2,command=lambda:change(『1』)).grid(row=3,column=0)

button_2 = button(frame_bord,text=『2』,width=5,height=2,command=lambda:change(『2』)).grid(row=3,column=1)

button_3 = button(frame_bord,text=『3』,width=5,height=2,command=lambda:change(『3』)).grid(row=3,column=2)

button_addition = button(frame_bord,text=』+』,width=5,height=2,bg=』#bbbbbb』,command=lambda:operation(』+』)).grid(row=3,column=3)

button_0 = button(frame_bord,text=『0』,width=12,height=2,command=lambda:change(『0』)).grid(row=4,column=1,columnspan=2)

button_10 = button(frame_bord,text=』.』,width=5,height=2,command=lambda:change(』.』)).grid(row=4,column=0)

button_equal = button(frame_bord,text=』=』,width=5,height=2,bg=』#bbbbbb』,command=equal).grid(row=4,column=3)

frame_bord.pack(padx=10,pady=10) #設定框架間隙

top.mainloop() #加入訊息佇列

計算器簡易實現

testdlg.cpp 實現檔案 include stdafx.h include include test.h include testdlg.h include afxdialogex.h ifdef debug define new debug new endif define tag add...

簡易計算器(Java實現)

本文參考自侵刪。一.ui介面的布置 窗體中放置三個jpanel面板,第乙個面板放數字與操作符元件 0 9,4 4網格布局,置於窗體中部 第二個面板放文字框,置於頂部 第三個面板放 清空 按鈕與 歷史記錄 按鈕,置於底部。窗體為邊界布局。按鈕上的數字 操作符使用陣列儲存,乙個for迴圈就能建立完按鈕並...

C 實現簡易計算器

include include include define fin cin define fo using namespace std bool isoperator char x int getpriority char x bool isbracket char x int dooperate...