VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • Python ttkbootstrap 制作账户注册信息界面

前言

ttkbootstrap 是一个基于 tkinter 的界面美化库,使用这个工具可以开发出类似前端 bootstrap 风格的 tkinter 桌面程序。
ttkbootstrap 不仅有丰富的案例,同时还有完善的官方文档,可惜是英文的。不过对于程序员来说,只要用好翻译软件与提供的案例代码,一样可以轻松上手,那么接下来我们就介绍一下这个工具的使用。

准备工作

首先肯定是需要安装一下 ttkbootstrap

版本要新,最好不要用镜像源安装
pip install ttkbootstrap

可以先来个小案例试试手

复制代码
import ttkbootstrap as ttk
from ttkbootstrap.constants import *

# root = tk.Tk()  # 使用 tkinter 创建窗口对象
root = ttk.Window()  # 使用 ttkbootstrap 创建窗口对象

root.geometry('300x150')

b1 = ttk.Button(root, text="按钮 1", bootstyle=SUCCESS)  # 使用 ttkbootstrap 的组件
b1.pack(side=LEFT, padx=5, pady=10)

b2 = ttk.Button(root, text="按钮 2", bootstyle=(INFO, OUTLINE))   # OUTLINE 是指定边框线
b2.pack(side=LEFT, padx=5, pady=10)

root.mainloop()
复制代码

 

 

 

对于本篇文章有疑问的同学可以加【资料白嫖、解答交流群:910981974】

开始我们今天的案例教学

完整代码

复制代码
import ttkbootstrap as tk

root = tk.Window(themename='litera')
root.geometry('350x500+500+500')
root.title('萌新-注册页面')
root.wm_attributes('-topmost', 1)

username_str_var = tk.StringVar()
password_str_var = tk.StringVar()

# 0 女 1 男 -1 保密
gender_str_var = tk.IntVar()
# 兴趣爱好
hobby_list = [
    [tk.IntVar(), ''],
    [tk.IntVar(), ''],
    [tk.IntVar(), ''],
    [tk.IntVar(), ''],
]

# 账户信息
tk.Label(root, width=10).grid()
tk.Label(root, text='用户名:').grid(row=1, column=1, sticky=tk.W, pady=10)
tk.Entry(root, textvariable=username_str_var).grid(row=1, column=2, sticky=tk.W)
tk.Label(root, text='密  码:').grid(row=2, column=1, sticky=tk.W, pady=10)
tk.Entry(root, textvariable=password_str_var).grid(row=2, column=2, sticky=tk.W)

# 性别 单选框
tk.Label(root, text='性别:').grid(row=4, column=1, sticky=tk.W, pady=10)
radio_frame = tk.Frame()
radio_frame.grid(row=4, column=2, sticky=tk.W)
tk.Radiobutton(radio_frame, text='', variable=gender_str_var, value=1).pack(side=tk.LEFT, padx=5)
tk.Radiobutton(radio_frame, text='', variable=gender_str_var, value=0).pack(side=tk.LEFT, padx=5)
tk.Radiobutton(radio_frame, text='保密', variable=gender_str_var, value=-1).pack(side=tk.LEFT, padx=5)

# 兴趣爱好
tk.Label(root, text='兴趣:').grid(row=6, column=1, sticky=tk.W, pady=10)
check_frame = tk.Frame()
check_frame.grid(row=6, column=2, sticky=tk.W)
tk.Checkbutton(check_frame, text=hobby_list[0][1], variable=hobby_list[0][0]).pack(side=tk.LEFT, padx=5)
tk.Checkbutton(check_frame, text=hobby_list[1][1], variable=hobby_list[1][0], bootstyle="square-toggle").pack(
    side=tk.LEFT, padx=5)
tk.Checkbutton(check_frame, text=hobby_list[2][1], variable=hobby_list[2][0], bootstyle="round-toggle").pack(
    side=tk.LEFT, padx=5)
tk.Checkbutton(check_frame, text=hobby_list[3][1], variable=hobby_list[3][0]).pack(side=tk.LEFT, padx=5)

# 生日
tk.Label(root, text='生日:').grid(row=7, column=1, sticky=tk.W, pady=10)
data_entry = tk.DateEntry()
data_entry.grid(row=7, column=2, sticky=tk.W, pady=10)
print(data_entry.entry.get())

# print(birth_day.get())

tk.Label(root, text="").grid(row=9, column=2, sticky=tk.W)
button = tk.Button(root, text='提交', width=20)
button.grid(row=10, column=2, sticky=tk.W)


def get_info():
    data = {
        '用户名': username_str_var.get(),
        '密码': password_str_var.get(),
        '性别': gender_str_var.get(),
        '兴趣': [h for v, h in hobby_list if v.get()],
        '生日': data_entry.entry.get()
    }
    print(data)
    with open('1.txt', mode='a') as f:
        f.write('\n')
        f.write(str(data))



button.config(command=get_info)
root.mainloop()
复制代码

 

 

 

做个界面

root = tk.Window(themename='litera')
root.geometry('350x500+500+500')
root.title('萌新-注册页面')
root.wm_attributes('-topmost', 1)
root.mainloop()

 

 

 

用户注册框

tk.Label(root, width=10).grid()
tk.Label(root, text='用户名:').grid(row=1, column=1, sticky=tk.W, pady=10)
tk.Entry(root, textvariable=username_str_var).grid(row=1, column=2, sticky=tk.W)
tk.Label(root, text='密  码:').grid(row=2, column=1, sticky=tk.W, pady=10)
tk.Entry(root, textvariable=password_str_var).grid(row=2, column=2, sticky=tk.W)

 

 

 

性别单选框

复制代码
# 0 女 1 男 -1 保密
gender_str_var = tk.IntVar()

tk.Label(root, text='性别:').grid(row=4, column=1, sticky=tk.W, pady=10)
radio_frame = tk.Frame()
radio_frame.grid(row=4, column=2, sticky=tk.W)
tk.Radiobutton(radio_frame, text='', variable=gender_str_var, value=1).pack(side=tk.LEFT, padx=5)
tk.Radiobutton(radio_frame, text='', variable=gender_str_var, value=0).pack(side=tk.LEFT, padx=5)
tk.Radiobutton(radio_frame, text='保密', variable=gender_str_var, value=-1).pack(side=tk.LEFT, padx=5)
复制代码

 

 

 

兴趣爱好

复制代码
hobby_list = [
    [tk.IntVar(), ''],
    [tk.IntVar(), ''],
    [tk.IntVar(), ''],
    [tk.IntVar(), ''],
]

tk.Label(root, text='兴趣:').grid(row=6, column=1, sticky=tk.W, pady=10)
check_frame = tk.Frame()
check_frame.grid(row=6, column=2, sticky=tk.W)
tk.Checkbutton(check_frame, text=hobby_list[0][1], variable=hobby_list[0][0]).pack(side=tk.LEFT, padx=5)
tk.Checkbutton(check_frame, text=hobby_list[1][1], variable=hobby_list[1][0], bootstyle="square-toggle").pack(
    side=tk.LEFT, padx=5)
tk.Checkbutton(check_frame, text=hobby_list[2][1], variable=hobby_list[2][0], bootstyle="round-toggle").pack(
    side=tk.LEFT, padx=5)
tk.Checkbutton(check_frame, text=hobby_list[3][1], variable=hobby_list[3][0]).pack(side=tk.LEFT, padx=5)
复制代码

 

 

 

生日

tk.Label(root, text='生日:').grid(row=7, column=1, sticky=tk.W, pady=10)
data_entry = tk.DateEntry()
data_entry.grid(row=7, column=2, sticky=tk.W, pady=10)
print(data_entry.entry.get())

 

 

 

提交信息按钮

tk.Label(root, text="").grid(row=9, column=2, sticky=tk.W)
button = tk.Button(root, text='提交', width=20)
button.grid(row=10, column=2, sticky=tk.W)

 

 

 

 

复制代码
def get_info():
    data = {
        '用户名': username_str_var.get(),
        '密码': password_str_var.get(),
        '性别': gender_str_var.get(),
        '兴趣': [h for v, h in hobby_list if v.get()],
        '生日': data_entry.entry.get()
    }
    print(data)
    with open('1.txt', mode='a') as f:
        f.write('\n')
        f.write(str(data))
button.config(command=get_info)
复制代码

 

 

 

 
出处:
https://www.cnblogs.com/qshhl/p/15909308.html


相关教程