from tkinter import *
root = Tk()
# 计算机窗口大小 (宽x高)
root.geometry("250x380")
# 设置计算机title
root.title("计算器")
# Frame就是在屏幕上的一块矩形区域 多用来作为容器使用
frame_show = Frame(width=250, height=150, bg='#ddd')
# 添加到主窗体
frame_show.pack()
# 主窗体
# 实例化一个产生变量的类
v = StringVar()
# 初始化赋值'0'
v.set('0')
# Lable(用于存放父组件,属性参数 )
# anchor 文本相对于标签中心的位置 默认是center N S W E
show_label = Label(frame_show, textvariable=v, bg='white', width='30', height='1', anchor='e', font=("黑体", 20, "bold"))
# 添加到主窗体
show_label.pack(padx=10, pady=10)
frame_bord = Frame(width=250, height=350, bg='#ccc')
frame_bord.pack(padx=10, pady=10)
calc = []
isoperate = False
def change(num):
global isoperate
if isoperate == False:
if v.get() == "0" and num == '.':
v.set(length(v.get() + num))
elif v.get() == "0":
v.set(length(num))
else:
if num == ".":
if v.get().count(".") < 1:
v.set(v.get() + num)
else:
v.set(v.get() + num)
else:
v.set(length(num))
isoperate = False
# 检验字符串的长度的函数
def length(str):
if len(str) > 12:
return str[0:12]
else:
return str
# 清空函数
def clear():
global calc
calc = []
# 屏幕窗口恢复到0
v.set("0")
# 操作 + - * /
def operate(sign):
global calc
global isoperate
isoperate = 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)
# 运算
global calcstr
def equal():
global calc
# 获取当前界面的值
calc.append(v.get())
print(calc)
# 列表变字符串 join 把列表用什么拼接成字符串
calcstr = "".join(calc)
print(calcstr)
print(type(calcstr))
# 运算操作 eval()把str当成有效的表达式进行计算
result = eval(calcstr)
if len(str(result)) > 12:
result = str(result)
result = result[0:12]
v.set(result)
else:
v.set(result)
print(result)
# 定义退格函数
def delete():
# 获取v.get()长度
num = len(v.get())
# 如果长度>1 怎么办
if num > 1:
strnum = v.get()
strnum = strnum[0:num - 1]
v.set(strnum)
# 小于等于1的时候
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_del = Button(frame_bord, text='←', width='5', height='1', command=lambda: delete()).grid(row='1', column='0')
button_del = Button(frame_bord, text='CE', width='5', height='1', command=lambda: clear()).grid(row='1', column='1')
button_del = Button(frame_bord, text='C', width='5', height='1', command=lambda: clear()).grid(row='1', column='2')
button_del = Button(frame_bord, text='±', width='5', height='1', command=lambda: fan()).grid(row='1', column='3')
button_del = Button(frame_bord, text='7', width='5', height='1', command=lambda: change("7")).grid(row='2', column='0')
button_del = Button(frame_bord, text='8', width='5', height='1', command=lambda: change("8")).grid(row='2', column='1')
button_del = Button(frame_bord, text='9', width='5', height='1', command=lambda: change("9")).grid(row='2', column='2')
button_del = Button(frame_bord, text='/', width='5', height='1', command=lambda: operate("/")).grid(row='2', column='3')
button_del = Button(frame_bord, text='4', width='5', height='1', command=lambda: change("4")).grid(row='3', column='0')
button_del = Button(frame_bord, text='5', width='5', height='1', command=lambda: change("5")).grid(row='3', column='1')
button_del = Button(frame_bord, text='6', width='5', height='1', command=lambda: change("6")).grid(row='3', column='2')
button_del = Button(frame_bord, text='-', width='5', height='1', command=lambda: operate("-")).grid(row='3', column='3')
button_del = Button(frame_bord, text='1', width='5', height='1', command=lambda: change("1")).grid(row='4', column='0')
button_del = Button(frame_bord, text='2', width='5', height='1', command=lambda: change("2")).grid(row='4', column='1')
button_del = Button(frame_bord, text='3', width='5', height='1', command=lambda: change("3")).grid(row='4', column='2')
button_del = Button(frame_bord, text='*', width='5', height='1', command=lambda: operate("*")).grid(row='4', column='3')
button_del = Button(frame_bord, text='0', width='5', height='1', command=lambda: change("0")).grid(row='5', column='0')
button_del = Button(frame_bord, text='.', width='5', height='1', command=lambda: change(".")).grid(row='5', column='1')
button_del = Button(frame_bord, text='+', width='5', height='1', command=lambda: operate("+")).grid(row='5', column='2')
button_del = Button(frame_bord, text='=', width='5', height='1', command=lambda: equal()).grid(row='5', column='3')
button_del = Button(frame_bord, text='查看作者', width='25', height='1', command=lambda: print("twilight")).grid(row='6',columnspan='4')
root.mainloop()
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
python例程 (1).rar_Python 例程_python_python例程 (308个子文件)
output.avi 5.99MB
test.gif 818KB
timg.gif 54KB
timg.gif 54KB
python.gif 38KB
python.gif 38KB
hbu.html 752B
taobao.html 752B
Title.ico 23KB
Title.ico 23KB
empire.jpg 266KB
s1.jpg 215KB
s1.jpg 215KB
girl2.jpg 98KB
Lenna.jpg 41KB
Lenna.jpg 41KB
chessboard.jpg 31KB
butterfly.jpg 28KB
girl1.jpg 26KB
robot1.jpg 25KB
robot2.jpg 24KB
hot.jpg 21KB
eye.jpg 14KB
minilenna.jpg 4KB
minion.mp4 8.91MB
res.png 242KB
redrock.png 215KB
redrock.png 215KB
hot.png 129KB
Lenna.png 41KB
Lenna.png 41KB
link.png 35KB
test.png 29KB
opencv-logo2.png 24KB
exit.png 14KB
exit.png 14KB
max.png 9KB
max.png 9KB
exit24.png 7KB
exit24.png 7KB
qq.png 2KB
mute.png 2KB
mute.png 2KB
med.png 1KB
med.png 1KB
min.png 662B
min.png 662B
calculator.py 5KB
calculator.py 5KB
mat29.py 3KB
mat34.py 2KB
mat33.py 2KB
sk18.py 2KB
mat30.py 2KB
receive.py 2KB
sk19.py 2KB
pyqt16.py 2KB
pyqt16.py 2KB
chatroom.py 2KB
chatroom.py 2KB
visual_app1.py 2KB
visual_app1.py 2KB
mat6.py 2KB
mat14.py 2KB
mat32.py 2KB
spider2.py 2KB
spider.py 2KB
send_mail4.py 2KB
pyqt18.py 2KB
pyqt18.py 2KB
pyqt14.py 2KB
pyqt14.py 2KB
sk17.py 2KB
pyqt17.py 1KB
pyqt17.py 1KB
pyqt12.py 1KB
pyqt12.py 1KB
test.py 1KB
pyqt13.py 1KB
pyqt13.py 1KB
pyqt9.py 1KB
pyqt9.py 1KB
mat28.py 1KB
mat4.py 1KB
mat31.py 1KB
pyqt22.py 1KB
pyqt22.py 1KB
pyqt10.py 1KB
pyqt10.py 1KB
pyqt11.py 1KB
pyqt11.py 1KB
pyqt5.py 1KB
pyqt5.py 1KB
pyqt19.py 1KB
pyqt19.py 1KB
pyqt8.py 1KB
pyqt8.py 1KB
receive2.py 1KB
pyqt3.py 1KB
pyqt3.py 1KB
共 308 条
- 1
- 2
- 3
- 4
局外狗
- 粉丝: 64
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0