栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

使用tkinter编写界面

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用tkinter编写界面

# *-* coding:utf8 *-*
import tkinter as tk
import tkinter.messagebox
  
window = tk.Tk()  # 实例化Tk
window.title("login")  # 设置标题
window.geometry("450x300")  # 设置窗口的大小
user_info = [{"name": "admin", "pwd": "1"}, {"name": "user", "pwd": "1"}]  # 存储账号信息
# 定义函数
def login():
    user_info_name = [i["name"] for i in user_info]
    user_info_pwd = [i["pwd"] for i in user_info]
    user_name = var_user_name.get()
    user_pwd = var_pwd.get()
    if user_name in user_info_name:
        if user_pwd == user_info_pwd[user_info_name.index(user_name)]:
            tk.messagebox.showinfo(title="欢迎回来", message="How are you")
        else:
            tk.messagebox.showerror(message="pwd error")
    else:
        answer = tk.messagebox.askquestion(title="warn", message="您还没有账号,是否需要注册?")
        if answer:
            sign()
def sign():
    def store():
        new_pwd1 = new_pwd.get()
        new_user_name1 = new_user_name.get()
        user_info.append({"name": new_user_name1, "pwd": new_pwd1})
        tk.messagebox.showinfo(message="注册成功")
        window_sign.destroy()  # 关闭窗口
    def close():
        window_sign.destroy()
    window_sign = tk.Toplevel(window)  # 窗口上的窗口
    window_sign.geometry("350x200")
    window_sign.title("Sign up window")
    # user
    tk.Label(window_sign, text="Name:").place(x=60, y=60)
    new_user_name = tk.StringVar()  # 注意要使用.get()得到值
    new_user_name.set("exampe@python.com")  # 默认值
    tk.Entry(window_sign, textvariable=new_user_name).place(x=90, y=60)  # 输入用户名
    # pwd
    tk.Label(window_sign, text="Pwd:").place(x=60, y=100)
    new_pwd = tk.StringVar()
    tk.Entry(window_sign, textvariable=new_pwd, show="*").place(x=90, y=100)  # 输入密码
    # 确认按钮
    tk.Button(window_sign, text="确认", command=store).place(x=100, y=140)
    # 取消按钮
    tk.Button(window_sign, text="取消", command=close).place(x=170, y=140)
if __name__ == '__main__':
     
    tk.Label(window, text="User Name:").place(x=50, y=50)
    var_user_name = tk.StringVar()  # 注意要使用.get()得到值
    var_user_name.set("exampe@python.com")  # 默认值
    tk.Entry(window, textvariable=var_user_name).place(x=130, y=50)  # 输入用户名
    tk.Label(window, text="Password:").place(x=50, y=90)
    var_pwd = tk.StringVar()
    tk.Entry(window, textvariable=var_pwd, show="*").place(x=130, y=90)  # 输入密码
    # login Button
    tk.Button(window, text="Login", command=login).place(x=130, y=130)  # 登录按钮
    # sign Button
    tk.Button(window, text="Sign Up", command=sign).place(x=200, y=130)  # 注册按钮
    window.mainloop()  # 显示窗口

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/977323.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号