本文主要是介绍浔川画板v2.0——浔川python社,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
系列文章目录
浔川画板v2.0——浔川python社
文章目录
- 系列文章目录
- 前言
- 总结
前言
浔川画板v2.0——浔川python社
正式代码:
# -*- coding: utf-8 -*-
import tkinter as tk
import tkinter.messagebox
import pickle
import random# 窗口
window = tk.Tk()
window.title('欢迎进入python')
window.geometry('450x200')
# 画布放置图片
# canvas=tk.Canvas(window,height=300,width=500)
# imagefile=tk.PhotoImage(file='qm.png')
# image=canvas.create_image(0,0,anchor='nw',image=imagefile)
# canvas.pack(side='top')
# 标签 用户名密码
Verification_Code = random.randint(1000, 9999)#设置一个随机的四位数
Verification_Code = str(Verification_Code)#把类型转换为str型
print(type(Verification_Code))
tk.Label(window, text='用户名:').place(x=100, y=30)
tk.Label(window, text='密码:').place(x=100, y=70)
tk.Label(window, text='验证码').place(x=100, y=110)
tk.Label(window, text=Verification_Code).place(x=320, y=110)
# 用户名输入框
var_usr_name = tk.StringVar()
entry_usr_name = tk.Entry(window, textvariable=var_usr_name)
entry_usr_name.place(x=160, y=30)
# 密码输入框
var_usr_pwd = tk.StringVar()
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')
entry_usr_pwd.place(x=160, y=70)
#验证码输入框
var_usr_yzm = tk.StringVar()
entry_usr_yzm = tk.Entry(window, textvariable=var_usr_yzm)
entry_usr_yzm.place(x=160, y=110)# 登录函数
def usr_log_in():# 输入框获取用户名密码usr_name = var_usr_name.get()usr_pwd = var_usr_pwd.get()usr_yzm = var_usr_yzm.get()#测试类型print(type(usr_yzm),type(Verification_Code))# 从本地字典获取用户信息,如果没有则新建本地数据库try:with open('usr_info.pickle', 'rb') as usr_file:usrs_info = pickle.load(usr_file)except FileNotFoundError:with open('usr_info.pickle', 'wb') as usr_file:usrs_info = {'admin': 'admin'}pickle.dump(usrs_info, usr_file)# 判断验证码是否正确用户名和密码是否匹配if usr_yzm == Verification_Code:if usr_name in usrs_info:if usr_pwd == usrs_info[usr_name]:tk.messagebox.showinfo(title='welcome',message='欢迎您:' + usr_name)else:tk.messagebox.showerror(message='密码错误')# 用户名密码不能为空elif usr_name == '' or usr_pwd == '':tk.messagebox.showerror(message='用户名或密码为空')# 不在数据库中弹出是否注册的框else:is_signup = tk.messagebox.askyesno('欢迎', '您还没有注册,是否现在注册')if is_signup:usr_sign_up()elif usr_yzm == '':tk.messagebox.showerror(message='验证码不能为空')else:tk.messagebox.showerror(message='验证码有误!')# 注册函数
def usr_sign_up():# 确认注册时的相应函数def signtowcg():# 获取输入框内的内容nn = new_name.get()np = new_pwd.get()npf = new_pwd_confirm.get()# 本地加载已有用户信息,如果没有则已有用户信息为空try:with open('usr_info.pickle', 'rb') as usr_file:exist_usr_info = pickle.load(usr_file)except FileNotFoundError:exist_usr_info = {}# 检查用户名存在、密码为空、密码前后不一致if nn in exist_usr_info:tk.messagebox.showerror('错误', '用户名已存在')elif np == '' or nn == '':tk.messagebox.showerror('错误', '用户名或密码为空')elif np != npf:tk.messagebox.showerror('错误', '密码前后不一致')# 注册信息没有问题则将用户名密码写入数据库else:exist_usr_info[nn] = npwith open('usr_info.pickle', 'wb') as usr_file:pickle.dump(exist_usr_info, usr_file)tk.messagebox.showinfo('欢迎', '注册成功')# 注册成功关闭注册框window_sign_up.destroy()# 新建注册界面window_sign_up = tk.Toplevel(window)window_sign_up.geometry('350x200')window_sign_up.title('注册')# 用户名变量及标签、输入框new_name = tk.StringVar()tk.Label(window_sign_up, text='用户名:').place(x=10, y=10)tk.Entry(window_sign_up, textvariable=new_name).place(x=150, y=10)# 密码变量及标签、输入框new_pwd = tk.StringVar()tk.Label(window_sign_up, text='请输入密码:').place(x=10, y=50)tk.Entry(window_sign_up, textvariable=new_pwd, show='*').place(x=150, y=50)# 重复密码变量及标签、输入框new_pwd_confirm = tk.StringVar()tk.Label(window_sign_up, text='请再次输入密码:').place(x=10, y=90)tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*').place(x=150, y=90)# 确认注册按钮及位置bt_confirm_sign_up = tk.Button(window_sign_up, text='确认注册',command=signtowcg)bt_confirm_sign_up.place(x=150, y=130)# 退出的函数
def usr_sign_quit():window.destroy()# 登录 注册按钮
bt_login = tk.Button(window, text='登录', command=usr_log_in)
bt_login.place(x=140, y=150)
bt_logup = tk.Button(window, text='注册', command=usr_sign_up)
bt_logup.place(x=210, y=150)
bt_logquit = tk.Button(window, text='退出', command=usr_sign_quit)
bt_logquit.place(x=280, y=150)
# 主循环
window.mainloop()import tkinter as tk
import time# 创建主窗口
window = tk.Tk()
window.title('进度条')
window.geometry('630x150')# 设置下载进度条
tk.Label(window, text='下载进度:', ).place(x=50, y=60)
canvas = tk.Canvas(window, width=465, height=22, bg="white")
canvas.place(x=110, y=60)# 显示下载进度
def progress():# 填充进度条fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="green")x = 500 # 未知变量,可更改n = 465 / x # 465是矩形填充满的次数for i in range(x):n = n + 465 / xcanvas.coords(fill_line, (0, 0, n, 60))window.update()time.sleep(0.02) # 控制进度条流动的速度# 清空进度条fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="white")x = 500 # 未知变量,可更改n = 465 / x # 465是矩形填充满的次数for t in range(x):n = n + 465 / x# 以矩形的长度作为变量值更新canvas.coords(fill_line, (0, 0, n, 60))window.update()time.sleep(0) # 时间为0,即飞速清空进度条btn_download = tk.Button(window, text='启动进度条', command=progress)
btn_download.place(x=400, y=105)
window.mainloop()#画板
from tkinter import *
from tkinter.colorchooser import askcolor
import time
win_width = 1200
win_height = 750
bgcolor = 'white'
print("画笔初始颜色为黑色.")
print("橡皮擦要先点橡皮擦再点画笔用哦!")
time.sleep(1)
class Application(Frame):"""一个经典的GUI写法"""def __init__(self, master=None):"""初始化方法"""super().__init__(master) # 调用父类的初始化方法self.x = 0self.y = 0self.fgcolor = "black"self.lastdraw = 0self.start_flag = Falseself.master = masterself.pack()self.createWidget()def createWidget(self):"""创建画图区域"""self.drawpad = Canvas(self, width=win_width, height=win_height, bg=bgcolor)self.drawpad.pack()# 创建按钮self.btn_start = Button(self, name='start', text='开始')self.btn_start.pack(side='left', padx=50)self.btn_pen = Button(self, name='pen', text='画笔')self.btn_pen.pack(side='left', padx=50)self.btn_rect = Button(self, name='rect', text='矩形')self.btn_rect.pack(side='left', padx=50)self.btn_clear = Button(self, name='clear', text='清屏')self.btn_clear.pack(side='left', padx=50)self.btn_erasor = Button(self, name='erasor', text='橡皮擦')self.btn_erasor.pack(side='left', padx=50)self.btn_line = Button(self, name='line', text='直线')self.btn_line.pack(side='left', padx=50)self.btn_line_arrow = Button(self, name='line_arrow', text='箭头直线')self.btn_line_arrow.pack(side='left', padx=50)self.btn_color = Button(self, name='color', text='颜色')self.btn_color.pack(side='left', padx=50)# 绑定事件self.btn_line.bind('<Button-1>', self.eventManager) # 点击按钮事件self.btn_line_arrow.bind('<Button-1>', self.eventManager) # 点击按钮事件self.btn_rect.bind('<Button-1>', self.eventManager) # 点击按钮事件self.btn_pen.bind('<Button-1>', self.eventManager) # 点击按钮事件self.btn_erasor.bind('<Button-1>', self.eventManager) # 点击按钮事件self.btn_clear.bind('<Button-1>', self.eventManager) # 点击按钮事件self.btn_color.bind('<Button-1>', self.eventManager) # 点击按钮事件self.master.bind('<KeyPress-r>', self.hotKey) # 绑定快捷键self.master.bind('<KeyPress-g>', self.hotKey) # 绑定快捷键self.master.bind('<KeyPress-b>', self.hotKey) # 绑定快捷键self.master.bind('<KeyPress-y>', self.hotKey) # 绑定快捷键self.drawpad.bind('<ButtonRelease-1>', self.stopDraw) # 左键释放按钮def eventManager(self, event):name = event.widget.winfo_name()print(name)self.start_flag = Trueif name == 'line':# 左键拖动self.drawpad.bind('<B1-Motion>', self.myline)elif name == 'line_arrow':self.drawpad.bind('<B1-Motion>', self.myline_arrow)elif name == 'rect':self.drawpad.bind('<B1-Motion>', self.myrect)elif name == 'pen':self.drawpad.bind('<B1-Motion>', self.mypen)elif name == 'erasor':self.drawpad.bind('<B1-Motion>', self.myerasor)elif name == 'clear':self.drawpad.delete('all')elif name == 'color':c = askcolor(color=self.fgcolor, title='请选择颜色')print(c) # c的值 ((128.5, 255.99609375, 0.0), '#80ff00')self.fgcolor = c[1]def startDraw(self, event):self.drawpad.delete(self.lastdraw)if self.start_flag:self.start_flag = Falseself.x = event.xself.y = event.ydef stopDraw(self, event):self.start_flag = Trueself.lastdraw = 0def myline(self, event):self.startDraw(event)self.lastdraw = self.drawpad.create_line(self.x, self.y, event.x, event.y, fill=self.fgcolor)def myline_arrow(self, event):self.startDraw(event)self.lastdraw = self.drawpad.create_line(self.x, self.y, event.x, event.y, arrow=LAST, fill=self.fgcolor)def myrect(self, event):self.startDraw(event)self.lastdraw = self.drawpad.create_rectangle(self.x, self.y, event.x, event.y, outline=self.fgcolor)def mypen(self, event):self.startDraw(event)print('self.x=', self.x, ',self.y=', self.y)self.drawpad.create_line(self.x, self.y, event.x, event.y, fill=self.fgcolor)self.x = event.xself.y = event.ydef myerasor(self, event):self.fgcolor = "white"def hotKey(self, event):c = event.charif c == 'r':self.fgcolor = 'red'elif c == 'g':self.fgcolor = 'green'elif c == 'b':self.fgcolor = 'blue'elif c == 'y':self.fgcolor = 'yellow'if __name__ == '__main__':root = Tk()root.title('浔川python社画板')root.geometry('1200x1000+400+400')app = Application(master=root)root.mainloop()
这篇关于浔川画板v2.0——浔川python社的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!