本文主要是介绍浔川画板v5.0——浔川python科技社,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
浔川画板v5.0
本代码由浔川python社、浔川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()# 创建主窗口
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()import time
import tkinter
import tkinter.simpledialog
import tkinter.colorchooser
import tkinter.filedialog
from PIL import Image, ImageTk, ImageGrabdef center_window(w, h):app.winfo_screenwidth()app.winfo_screenheight()app.geometry('%dx%d' % (w, h))app = tkinter.Tk()
app.title('画板')
x = 1200
y = 800
center_window(x, y)yesno = tkinter.IntVar(value=0)
what = tkinter.IntVar(value=1)
X = tkinter.IntVar(value=0)
Y = tkinter.IntVar(value=0)foreColor = '#000000'
backColor = '#FFFFFF'image = tkinter.PhotoImage()
canvas = tkinter.Canvas(app, bg='white', width=x, height=y)
canvas.create_image(x, y, image=image)lastDraw = 0
end = [0]
size = "20"def getter(widget):time.sleep(0.5)x = app.winfo_x() + widget.winfo_x()y = app.winfo_y() + widget.winfo_y()if app.winfo_x() < 0:x = 0if app.winfo_y() < 0:y = 0x1 = x + widget.winfo_width() + 200y1 = y + widget.winfo_height() + 200filename = tkinter.filedialog.asksaveasfilename(filetypes=[('.jpg', 'JPG')],initialdir='C:\\Users\\lin042\\Desktop\\')ImageGrab.grab().crop((x, y, x1, y1)).save(filename)def onLeftButtonDown(event):yesno.set(1)X.set(event.x)Y.set(event.y)if what.get() == 4:canvas.create_text(event.x, event.y, font=("等线", int(size)), text=text, fill=foreColor)what.set(1)def onLeftButtonMove(event):global lastDrawif yesno.get() == 0:returnif what.get() == 1:lastDraw = canvas.create_line(X.get(), Y.get(), event.x, event.y,fill=foreColor)X.set(event.x)Y.set(event.y)elif what.get() == 2:try:canvas.delete(lastDraw)except Exception:passlastDraw = canvas.create_line(X.get(), Y.get(), event.x, event.y,fill=foreColor)elif what.get() == 3:try:canvas.delete(lastDraw)except Exception:passlastDraw = canvas.create_rectangle(X.get(), Y.get(), event.x, event.y,outline=foreColor)elif what.get() == 5:lastDraw = canvas.create_rectangle(event.x - 10, event.y - 10, event.x + 10, event.y + 10,outline=backColor)elif what.get() == 6:try:canvas.delete(lastDraw)except Exception:passlastDraw = canvas.create_oval(X.get(), Y.get(), event.x, event.y,fill=backColor, outline=foreColor)def onLeftButtonUp(event):global lastDrawif what.get() == 2:lastDraw = canvas.create_line(X.get(), Y.get(), event.x, event.y, fill=foreColor)elif what.get() == 3:lastDraw = canvas.create_rectangle(X.get(), Y.get(), event.x, event.y, outline=foreColor)elif what.get() == 6:lastDraw = canvas.create_oval(X.get(), Y.get(), event.x, event.y, outline=foreColor)yesno.set(0)end.append(lastDraw)def onRightButtonUp(event):menu.post(event.x_root, event.y_root)canvas.bind('<Button-1>', onLeftButtonDown)
canvas.bind('<B1-Motion>', onLeftButtonMove)
canvas.bind('<ButtonRelease-1>', onLeftButtonUp)
canvas.bind('<ButtonRelease-3>', onRightButtonUp)
canvas.pack(fill=tkinter.BOTH, expand=tkinter.YES)'''主菜单及其关联的函数'''
menu = tkinter.Menu(app, bg="red")
app.config(menu=menu)def Open():filename = tkinter.filedialog.askopenfilename(title='导入图片',filetypes=[('image', '*.jpg *.png *.gif')])if filename:global imageimage = Image.open(filename)image = image.resize((800, 600), Image.ANTIALIAS)image = ImageTk.PhotoImage(image)canvas.create_image(400, 300, image=image)menu.add_command(label='导入', command=Open)def Save():getter(canvas)menu.add_command(label='保存', command=Save)def Clear():global lastDraw, endfor item in canvas.find_all():canvas.delete(item)end = [0]lastDraw = 0menu.add_command(label='清屏', command=Clear)def Back():global endtry:for i in range(end[-2], end[-1] + 1):canvas.delete(i)end.pop()except:end = [0]menu.add_command(label='撤销', command=Back)menu.add_separator()'''子菜单及其关联的函数'''
menuType = tkinter.Menu(menu, tearoff=0)def drawCurve():what.set(1)menuType.add_command(label='铅笔', command=drawCurve)def drawLine():what.set(2)menuType.add_command(label='直线', command=drawLine)def drawRectangle():what.set(3)menuType.add_command(label='矩形', command=drawRectangle)def drawCircle():what.set(6)menuType.add_command(label='圆形', command=drawCircle)def drawText():global text, sizetext = tkinter.simpledialog.askstring(title='输入文本', prompt='')if text is not None:size = tkinter.simpledialog.askinteger('输入字号', prompt='', initialvalue=20)if size is None:size = "20"what.set(4)menuType.add_command(label='文本', command=drawText)def onErase():what.set(5)menuType.add_command(label='橡皮擦', command=onErase)
menuType.add_separator()def chooseForeColor():global foreColorforeColor = tkinter.colorchooser.askcolor()[1]menuType.add_command(label='选择前景色', command=chooseForeColor)def chooseBackColor():global backColorbackColor = tkinter.colorchooser.askcolor()[1]menuType.add_command(label='选择背景色', command=chooseBackColor)menu.add_cascade(label='工具栏', menu=menuType)app.mainloop()
另其版本:
浔川画板v4.0 :浔川画板v4.0——浔川python科技社-CSDN博客
浔川画板v3.1 :
浔川画板v3.1——浔川python社、浔川python科技社-CSDN博客
浔川画板v3.0 :
浔川画板v3.0——浔川python社-CSDN博客
浔川画板v2.0 :
浔川画板v2.0——浔川python社-CSDN博客
浔川画板:
浔川python社画板——浔川总社部-CSDN博客
这篇关于浔川画板v5.0——浔川python科技社的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!