下列实现代码说明:
  下列代码主要是实现计算器功能。由于之前在莫凡老师开设的《用 python 和 tkinter 做简单的窗口视窗》课程当中学习了tkinter的内容,在该课程的结束部分是老师带着做一个关于
登录窗口的小程序。
  在学习了该课程后,自己又去学习了计算器编写的代码,而后,想着能否把这两个小项目结合寄来,于是,新的一个小项目又诞生了。
  该程序的功能是在打开计算器程序之前需要进行一个登陆操作。
  该项目涉及到的前面的project如下:

  1、class13and14and15_登录窗口 - JY小脚丫 - 博客园

    https://www.cnblogs.com/jyfootprint/p/9571509.html

  2、project1_calculator(使用tkinter实现python计算器,含有具体过程与注释) - JY小脚丫 - 博客园

    https://www.cnblogs.com/jyfootprint/p/9570951.html

代码如下:


#!/usr/bin/env python
# -*- coding:utf-8 -*-
# ------------------------------------------------------------
import calculator import tkinter as tk
from tkinter import messagebox
import pickle window = tk.Tk()
window.title("登录窗口")
window.maxsize(460, 300)
window.minsize(460, 300)
# welcom image
canvas = tk.Canvas(window, width=500, height=200)
image_file = tk.PhotoImage(file='my_login.gif')
# 10, 0 表示锚点的横,纵位置; anchor='nw'表示锚点的位置是左上角
image = canvas.create_image(10, 0, anchor='nw', image=image_file)
canvas.pack(side='top') tk.Label(window, text='username:', font=('黑体', 12)).place(x=60, y=160, anchor='nw')
tk.Label(window, text='password:', font=('黑体', 12)).place(x=60, y=190, anchor='nw') # 设置存储的变量
username = tk.StringVar()
password = tk.StringVar()
# 设置登录的输入框,并获取信息
entry_username = tk.Entry(window, textvariable=username, width=30).place(x=150, y=160, anchor='nw')
entry_password = tk.Entry(window, textvariable=password, width=30, show='*').place(x=150, y=190, anchor='nw')
# 初始化 username 的信息
username.set('example@python.com') def usr_login():
usr_name = username.get()
usr_password = password.get()
try:
with open('usrs_info.pickle', 'rb') as usr_file:
usrs_info = pickle.load(usr_file)
except FileNotFoundError:
usrs_info = {'admin': 'admin'}
pickle.dump(usrs_info, usr_file) if usr_name in usrs_info:
if usr_password == usrs_info[usr_name]:
messagebox.showinfo(title='Welcom', message='Welcome to log in! \n'+ usr_name)
window.destroy()
calculator.Calculator()
else:
messagebox.showerror(title='Error', message='Password Error!\nTry again!!')
else:
is_sign_up = messagebox.askyesno(title='Ask', message='You have not sign up yet.\nSign up now?')
if is_sign_up is True:
usr_sign_up() def usr_sign_up():
# 设置窗口
window_sign_up = tk.Toplevel(window)
window_sign_up.title('Sign up window')
window_sign_up.maxsize(460, 180)
window_sign_up.minsize(460, 180) # 设置标签
tk.Label(window_sign_up, text='username:', font=('黑体', 12)).place(x=30, y=30, anchor='nw')
tk.Label(window_sign_up, text='password:', font=('黑体', 12)).place(x=30, y=60, anchor='nw')
tk.Label(window_sign_up, text='confirm password:', font=('黑体', 12)).place(x=30, y=90, anchor='nw') # 设置输入框
# 设置存储的变量
usr_username = tk.StringVar()
usr_password1 = tk.StringVar()
usr_password2 = tk.StringVar()
# 设置登录的输入框,并获取信息
usr_entry_username = tk.Entry(window_sign_up, textvariable=usr_username, width=40).place(x=170, y=30, anchor='nw')
usr_entry_password1 = tk.Entry(window_sign_up, textvariable=usr_password1, width=40, show='*').place(x=170, y=60, anchor='nw')
usr_entry_password2 = tk.Entry(window_sign_up, textvariable=usr_password2, width=40, show='*').place(x=170, y=90, anchor='nw')
# 初始化 username 的信息
usr_username.set('example@python.com')
def new_sign_up():
new_name = usr_username.get()
new_password = usr_password1.get()
new_password_confirm = usr_password2.get()
# 打开配置文件,查看注册的信息是否在文件中
with open('usrs_info.pickle', 'rb') as usr_file:
exist_usr_info = pickle.load(usr_file)
# 对比用户名是否已经储存在文件中
if new_name in exist_usr_info :
messagebox.showerror(title='Error',message='The user has already signed up!') else:
if new_password != new_password_confirm:
messagebox.showerror(title='Error', message='Password and confirm password must be the same!')
else:
exist_usr_info[new_name] = new_password
with open('usrs_info.pickle', 'wb') as usr_file:
# 写入到文件中
pickle.dump(exist_usr_info, usr_file)
messagebox.showinfo(title='Welcome', message='You have successfully signed up!')
window_sign_up.destroy() # 设置注册的按钮
tk.Button(window_sign_up, text='sign up', command=new_sign_up).place(x=220, y=120, anchor='nw') tk.Button(text='login', command=usr_login).place(x=170, y=220, anchor='nw')
tk.Button(text='sign up', command=usr_sign_up).place(x=240, y=220, anchor='nw') window.mainloop()

  



上面代码说明:

语句:  import calculator 
说明:  calculator 是一个自己编写的一个名称为“calculator.py"文件,是用来实现计算器计算功能程序,该程序的内容见一下转接链接
代码内容如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# ------------------------------------------------------------
# ------------------------------------------------分割线-------------------------------------------------
'''
# ------------------------------------------------------------
# # 7、(完成全部设计)实现基本的计算器功能,补充错误检测, 修改‘C’为清楚功能, '<=='为后退功能
# # # 改动位置位置: def click_button(self, event):
# ------------------------------------------------------------
''' class Calculator:
import tkinter as tk
import tkinter.messagebox as mbox def __init__(self):
# 生成一个窗口对象
self.window = self.tk.Tk()
# 命名窗口对象的显示title
self.window.title('计算器')
# 设置窗口的大小 minsize最小 maxsize最大
self.window.minsize(240, 290)
self.window.maxsize(240, 290)
# 是否清空显示框判定参数
# 设置菜单
self.set_menu()
# 设置显示框
self.label_show = self.tk.Label(text='', anchor='se', font=('黑体', 30), fg='black')
self.label_show.place(x=0, y=0, width=240,height=80)
# 设置按钮组件
self.set_buttons()
# 将窗口放入主消息队列
self.window.mainloop() def set_menu(self):
'''
设置菜单
:return: None
'''
# 创建总菜单
menubar = self.tk.Menu(self.window)
# 创建一个下拉菜单,并且加入文件菜单
filemenu = self.tk.Menu(menubar, tearoff=False)
# 创建菜单中的选项
filemenu.add_command(label='退出计算器', command=self.window.quit ) # print author的函数
def show_author():
self.mbox.showinfo(title='作者信息',message='作者:许建荣\n联系邮箱:jyfootprint@foxmail.com') filemenu.add_command(label='作者信息', command=show_author)
# 将文件菜单作为下拉菜单添加到总菜单中,并且将命名为操作
menubar.add_cascade(label='查看', menu=filemenu)
# 显示总菜单
self.window.config(menu=menubar) def set_buttons(self):
# 基础坐标, x0, y0,开始坐标; x_width, y_width 间隔
x0, y0, x_width, y_width,height = 0, 90, 60, 40, 40
# 7
btn7 = self.tk.Button(text='7', bd=2, font='黑体')
btn7.place( x=x0, y=y0, width=x_width, height=y_width)
# 8
btn8 = self.tk.Button(text='8', bd=2, font='黑体')
btn8.place( x=x0+x_width*1, y=y0, width=x_width, height=y_width)
# 9
btn9 = self.tk.Button(text='9', bd=2, font='黑体')
btn9.place( x=x0+x_width*2, y=y0, width=x_width, height=y_width)
# +
btn_add = self.tk.Button(text='+', bd=2, font='黑体')
btn_add.place( x=x0+x_width*3, y=y0, width=x_width, height=y_width) # 4
btn4 = self.tk.Button(text='4', bd=2, font='黑体')
btn4.place( x=x0, y=y0+y_width, width=x_width, height=y_width)
# 5
btn5 = self.tk.Button(text='5', bd=2, font='黑体')
btn5.place( x=x0+x_width*1, y=y0+y_width, width=x_width, height=y_width)
# 6
btn6 = self.tk.Button(text='6', bd=2, font='黑体')
btn6.place( x=x0+x_width*2, y=y0+y_width, width=x_width, height=y_width)
# -
btn_subtract = self.tk.Button(text='-', bd=2, font='黑体')
btn_subtract.place( x=x0+x_width*3, y=y0+y_width, width=x_width, height=y_width) # 1
btn1 = self.tk.Button(text='1', bd=2, font='黑体')
btn1.place( x=x0, y=y0+y_width*2, width=x_width, height=y_width)
# 2
btn2 = self.tk.Button(text='2', bd=2, font='黑体')
btn2.place( x=x0+x_width*1, y=y0+y_width*2, width=x_width, height=y_width)
# 3
btn3 = self.tk.Button(text='3', bd=2, font='黑体')
btn3.place( x=x0+x_width*2, y=y0+y_width*2, width=x_width, height=y_width)
# *
btn_mutiply = self.tk.Button(text='*', bd=2, font='黑体')
btn_mutiply.place( x=x0+x_width*3, y=y0+y_width*2, width=x_width, height=y_width) # 0
btn0 = self.tk.Button(text='0', bd=2, font='黑体')
btn0.place( x=x0, y=y0+y_width*3, width=x_width*2, height=y_width)
# .
btn_point = self.tk.Button(text='.', bd=2, font='黑体')
btn_point.place( x=x0+x_width*2, y=y0+y_width*3, width=x_width, height=y_width)
# /
btn_divid = self.tk.Button(text='/', bd=2, font='黑体')
btn_divid.place( x=x0+x_width*3, y=y0+y_width*3, width=x_width, height=y_width) # C后退
btn_clear = self.tk.Button( text='C', bd=2, font='黑体')
btn_clear.place( x=x0, y=y0+y_width*4, width=x_width, height=y_width) # C后退
btn_back = self.tk.Button( text='<==', bd=2, font='黑体')
btn_back.place( x=x0+x_width*1, y=y0+y_width*4, width=x_width, height=y_width) # =
btn_equal = self.tk.Button( text='=', bd=2, font='黑体')
btn_equal.place( x=x0+x_width*2, y=y0+y_width*4, width=x_width*2, height=y_width) # 绑定Button的点击事件
btn7.bind_class('Button', '<Button-1>', self.click_button) def click_button(self, event): # 获取点击的按钮信息信息
input_event = event.widget['text']
# 输入的按钮信息显示
self.label_show['text'] = self.label_show['text'] + input_event
# 异常捕获
try:
# 计算符号
cal_symbol = ['+', '-', '*', '/']
# 判定运算符号重复的时候,使用最后输入的符号
if self.label_show['text'][-1] in cal_symbol and self.label_show['text'][-2] in cal_symbol:
# 取重复符号前面的内容
header = self.label_show['text'][:-2]
footer = self.label_show['text'][-1]
self.label_show['text'] = header + footer
except:
pass # 进行普通计算
if event.widget['text'] == '=':
# 异常捕获
try:
res_bit = 2 # 计算保留的位数
res = eval(self.label_show['text'][:-1]) # 计算点击 “=” 之前的计算表达式
# print(type(res))
self.label_show['text'] = str(round(float(res), res_bit))
except ZeroDivisionError:
# 除法时,除数不能为0
self.mbox.showerror(title='错误', message='除法计算时!除数不能为0!')
except:
self.mbox.showerror(title='未知名错误', message='算式错误,请检查!') elif event.widget['text'] == '<==':
# 点击的‘<==’也计算在内,它占3个字符,因此是倒数4位去掉
back_res = self.label_show['text'][:-4]
self.label_show['text'] = back_res elif event.widget['text'] == 'C':
# 点击的‘C’也计算在内,因此是倒数2位去掉
self.label_show['text'] = '' if __name__=='__main__':
start = Calculator()

  

project3_NeedToLoginCalculator(需要进行登陆确认的计算器)的更多相关文章

  1. IdentityServer4 禁用 Consent screen page(权限确认页面)

    IdentityServer4 在登录完成的适合,会再跳转一次页面(权限确认),如下: 我之前以为 IdentityServer4 就是这样使用的,但实际业务场景并不需要进行权限确认,而是登陆成功后直 ...

  2. angularjs实现IOS8自带计算器

    最近看到一则面试题目,要求使用angularjs实现一个计算器,利用放假时间实现了一个仿iOS8风格的计算器,功能基本和iOS自带的计算器是一致的. 查看demo,接着给出实现过程. 首先创建angu ...

  3. MySQL数据库的登陆

    MySQL数据库的登陆 MySQL是一种C/S结构. C/S(Client/Server)客户端/服务器 MySQL的客户端: 1.cmd客户端 2.可视化图形界面 3.php代码 登陆: MySQL ...

  4. Python 爬虫五 进阶案例-web微信登陆与消息发送

    首先回顾下网页微信登陆的一般流程 1.打开浏览器输入网址 2.使用手机微信扫码登陆 3.进入用户界面 1.打开浏览器输入网址 首先打开浏览器输入web微信网址,并进行监控: https://wx.qq ...

  5. 前端验证用户登陆状态(vue.js)

    首先用户需要进行登陆(请求登陆接口),接口请求成功之后后台会返回对应的用户信息(可以把用户信息存放在浏览器缓存中),并且后台会设置浏览器的cookie值(可以在network->header-& ...

  6. MVC登陆认证简单设置

    首先,弄个基类 /// <summary> /// 所有控制器基类,里面重写了OnActionExecuted方法 /// </summary> public class Ba ...

  7. react-router-dom实现全局路由登陆拦截

    相比与vue的路由集中式管理,能够很好的进行统一的路由操作,react的路由看起来更乱,想要进行像vue的全局路由管理不是那么得心应手.在我们的项目中,有很多页面是需要登陆权限验证的,最好的方式就是能 ...

  8. asp.net 在AcquireRequestState事件中判断登陆验证。

    Global中添加AcquireRequestState事件. protected void Application_AcquireRequestState(object sender, EventA ...

  9. Hadoop生态圈-Knox网关的应用案例

    Hadoop生态圈-Knox网关的应用案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Knox网关简介 据Knox官网所述(http://knox.apache.org/) ...

随机推荐

  1. 【LeetCode 14】最长公共前缀

    题目链接 [题解] 二分最长前缀的长度. 然后暴力把第2..n个字符串和第1个字符串的前mid个字符匹配. 还有一种比较厉害的算法. 把这n个字符串加入到字典树当中去. 然后根节点到第一个有分支的节点 ...

  2. JCF——set

    HashSet LinkedHashSet TreeSet 联系与区别

  3. vue笔记一:设置表头的背景颜色以及下边框颜色

    最近做的一个项目有个小小的需求,就是把表头的背景颜色改为黑色,表格边框颜色改为这个颜色,中间出了些问题,因为vue的表格样式是内联样式,一修改就影响到其他地方的表格样式,尽管我单独加了id还是不行,经 ...

  4. ebay上传图片的要求

    eBay's Picture Requirements Introduction to Pictures in Listings Pictures make an item more appealin ...

  5. Java-Class-C:org.springframework.util.Assert

    ylbtech-Java-Class-C:org.springframework.util.Assert 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部 1. /* * Copyr ...

  6. js实现点击按钮传值

    js实现点击按钮传值 page1源码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8&quo ...

  7. 3.4 redux 异步

    在大多数的前端业务场景中,需要和后端产生异步交互,在本节中,将详细讲解 redux 中的异步方案以及一些异步第三方组件,内容有: redux 异步流 redux-thunk redux-promise ...

  8. 在Windows上安装部署Cuckoo

    1. Cuckoo使用的第三方工具及库 Yara:http://plusvic.github.io/yara/ Pydeep:https://github.com/kbandla/pydeep Yar ...

  9. php连接mysql遇到的问题: (HY000/1130) 和 [caching_sha2_password]

    说明一下我的mysql是安装在虚拟机上的 所以遇到的第一个问题就是访问问题 解决: update user set host = '%' where user = 'root'; 重启mysql服务 ...

  10. webpack 配置之入门二(css 篇)

    在项目中我们通过 css 来美化页面,css 也成为了网站不可或缺的一部分,这章节主要介绍 webpack 处理 css 部分, 1.webpack 处理 css 在 webpack 中,我们通过 s ...