浔川画板v5.1——浔川python科技社

浔川画板v5.1


本代码由浔川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] = np
            with 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 / x
        canvas.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 / x
        canvas.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, ImageGrab
 
def 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 = 0
    if app.winfo_y() < 0:
        y = 0
    x1 = x + widget.winfo_width() + 200
    y1 = y + widget.winfo_height() + 200
    filename = 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 lastDraw
    if yesno.get() == 0:
        return
    if 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:
            pass
 
        lastDraw = canvas.create_line(X.get(), Y.get(), event.x, event.y,
                                      fill=foreColor)
    elif what.get() == 3:
 
        try:
            canvas.delete(lastDraw)
        except Exception:
            pass
        lastDraw = 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:
            pass
        lastDraw = canvas.create_oval(X.get(), Y.get(), event.x, event.y,
                                      fill=backColor, outline=foreColor)
 
def onLeftButtonUp(event):
    global lastDraw
    if 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 image
 
        image = 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, end
    for item in canvas.find_all():
        canvas.delete(item)
    end = [0]
    lastDraw = 0
 
menu.add_command(label='清屏', command=Clear)
 
def Back():
    global end
    try:
        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, size
    text = 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 foreColor
    foreColor = tkinter.colorchooser.askcolor()[1]
 
menuType.add_command(label='选择前景色', command=chooseForeColor)
 
def chooseBackColor():
    global backColor
    backColor = tkinter.colorchooser.askcolor()[1]
 
menuType.add_command(label='选择背景色', command=chooseBackColor)
 
menu.add_cascade(label='工具栏', menu=menuType)
 
app.mainloop()

 


另其版本:

 另其版本:

请看:浔川画板v5.0——浔川python科技社-CSDN博客

  • 37
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值