Python实现经典计算器

此方式做了一个简单的计算器
基本的功能都能实现
主要练习python的布局
有不足的地方还请多多斧正

#引入Tkinter库
from tkinter import *

#实例化这个类  得到对象
root = Tk()
# 设置计算机窗口大小 宽250 高380  x轴50 y轴50
root.geometry('250x380+950+150')
#title 设置窗口标题
root.title('python经典计算器')
#resiza  设置窗口是否可以变化长 宽
root.resizable(width=TRUE,height=TRUE)

#frame 在屏幕上开辟一个矩形的区域  多用于作为容器布局使用
frame_show = Frame(width=250,height=150,bg="#ddd")

#要在顶部区域放置组件Label
frame_show.pack()
#顶部区域  实例化一个产生变量的类
v = StringVar()
#初始化默认值
v.set("0")
#Label (存放父组件)
show_label = Label(frame_show,textvariable = v,bg="white",width="12",font=("黑体",20,"bold"),
                   justify=LEFT, height="1",anchor="e")
#添加到主窗体
show_label.pack(padx=10,pady=10)
#将矩形添加到主窗体
frame_show.pack()
#开辟一个矩形
frame_bord = Frame(width = 250,height = 300,bg ="#ccc")
#添加到主窗口
frame_bord.pack(padx=10,pady =20)

# 主消息循环
#root.mainloop()

isopear = False
# 初始化一个容器
calc = []
# 是否运算初始值
state = 0
def change(num):
    global isopear
    global state
    if state == 1:
        v.set("0")
        state = 0
    else:
        v.set(str(v.get()))
    #设置屏幕显示数字
    #操作数
    if isopear == False:
        if v.get() == "0" and num ==".":
            v.set(length(v.get() + num))
        elif v.get() == "0":
            v.set(length(num))
        elif v.get().count(".") == 1 and num ==".":
            v.set(v.get())
        else:
            v.set(length(v.get()+num))
    else:
        v.set(length(num))
        isopear = False

def opear(sign):
    global isopear
    isopear = True
    #把点运算符前屏幕的数字压进入
    calc.append(v.get())
    if sign == "+":
        calc.append(sign)
    elif sign =="-":
        calc.append(sign)
    elif sign =="*":
        calc.append(sign)
    elif sign =="/":
        calc.append(sign)
    print(calc)

def equal():
    #获取当前界面的值
    global calc
    global state
    calc.append(v.get())
    print(calc)
    calcstr = "".join(calc)
    print(calcstr)
    result = eval(calcstr)
    v.set("%g"%result)
    #清空列表
    calc.clear()
    state = 1

def length(str):
    if len(str) > 11:
        return str[0:11]
    else:
        return str

def clear():
    global calc
    calc = []
    v.set("0")
#退格
def dell():
    num = len(v.get())
    print(num)
    if num >1:
         strnum = v.get()
         strnum = strnum[0:num-1]
         v.set(strnum)
    else:
         v.set("0")
#正反
def fan():
    strnum = v.get()
    if strnum[0] == "-":
        v.set(strnum[1:])
    elif strnum[0] != "-" and strnum != "0":
        v.set("-" +strnum)

#Button(父组件 属性类表)
Button(frame_bord,text="←",width="5",height='1',command=lambda:dell()).grid(row='0',column="0")
Button(frame_bord,text="CE",width="5",height="1",command=lambda:clear()).grid(row='0',column="1")
Button(frame_bord,text="C",width="5",height="1",command=lambda:clear()).grid(row='0',column="2")
Button(frame_bord,text="±",width="5",height="1",command=lambda:fan()).grid(row="0",column="3")
Button(frame_bord,text="7",width="5",height="1",command=lambda:change("7")).grid(row="1",column="0")
Button(frame_bord,text="8",width="5",height="1",command=lambda:change("8")).grid(row="1",column="1")
Button(frame_bord,text="9",width="5",height="1",command=lambda:change("9")).grid(row="1",column="2")
Button(frame_bord,text="/",width="5",height="1",command=lambda:opear("/")).grid(row="1",column="3")
Button(frame_bord,text="4",width="5",height="1",command=lambda:change("4")).grid(row="2",column="0")
Button(frame_bord,text="5",width="5",height="1",command=lambda:change("5")).grid(row="2",column="1")
Button(frame_bord,text="6",width="5",height="1",command=lambda:change("6")).grid(row="2",column="2")
Button(frame_bord,text="*",width="5",height="1",command=lambda:opear("*")).grid(row="2",column="3")
Button(frame_bord,text="1",width="5",height="1",command=lambda:change("1")).grid(row="3",column="0")
Button(frame_bord,text="2",width="5",height="1",command=lambda:change("2")).grid(row="3",column="1")
Button(frame_bord,text="3",width="5",height="1",command=lambda:change("3")).grid(row="3",column="2")
Button(frame_bord,text="-",width="5",height="1",command=lambda:opear("-")).grid(row="3",column="3")
Button(frame_bord,text="0",width="5",height="1",command=lambda:change("0")).grid(row="4",column="0")
Button(frame_bord,text=".",width="5",height="1",command=lambda:change(".")).grid(row="4",column="1")
Button(frame_bord,text="+",width="5",height="1",command=lambda:opear("+")).grid(row="4",column="2")
Button(frame_bord,text="=",width="5",height="1",command=lambda:equal()).grid(row="4", column="3")
Button(frame_bord,text="查看作者",width="25",height="1",command=lambda:print("sam")).grid(row="5",column="0",columnspan="4")

#主消息循环  主窗口
root.mainloop()

效果如下:
Python实现经典计算器_第1张图片

你可能感兴趣的:(python基础,笔记)