脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python ttkbootstrap 制作账户注册信息界面的案例代码

Python ttkbootstrap 制作账户注册信息界面的案例代码

2022-10-07 14:03终究不过路人 Python

ttkbootstrap 是一个基于 tkinter 的界面美化库,使用这个工具可以开发出类似前端 bootstrap 风格的 tkinter 桌面程序。本文重点给大家介绍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()

Python ttkbootstrap 制作账户注册信息界面的案例代码

 

开始我们今天的案例教学

完整代码,复制运行即可(明示)

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(
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()

Python ttkbootstrap 制作账户注册信息界面的案例代码

1、做个界面

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

Python ttkbootstrap 制作账户注册信息界面的案例代码

2、用户注册框

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)

Python ttkbootstrap 制作账户注册信息界面的案例代码

3、性别单选框

# 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)

Python ttkbootstrap 制作账户注册信息界面的案例代码

4、兴趣爱好

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)

Python ttkbootstrap 制作账户注册信息界面的案例代码

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())

Python ttkbootstrap 制作账户注册信息界面的案例代码

6、提交信息按钮

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)

Python ttkbootstrap 制作账户注册信息界面的案例代码

7、保存数据

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)

Python ttkbootstrap 制作账户注册信息界面的案例代码

Python ttkbootstrap 制作账户注册信息界面的案例代码

到此这篇关于Python ttkbootstrap 制作账户注册信息界面的文章就介绍到这了,更多相关Python ttkbootstrap账户注册界面内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/Python_ku/article/details/122992276

延伸 · 阅读

精彩推荐
  • Pythonpython 3.7.0 下pillow安装方法

    python 3.7.0 下pillow安装方法

    这篇文章主要为大家详细介绍了python 3.7.0 下pillow的安装方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    安徽王家卫8522021-03-29
  • PythonPython下载指定页面上图片的方法

    Python下载指定页面上图片的方法

    这篇文章主要介绍了Python下载指定页面上图片的方法,涉及Python的正则匹配、URL及文件操作相关技巧,需要的朋友可以参考下 ...

    charles_kao5532020-08-22
  • PythonPython教程之全局变量用法

    Python教程之全局变量用法

    这篇文章主要介绍了Python教程之全局变量用法,结合实例形式分析了Python全局变量的定义、修改等使用方法及注意事项,需要的朋友可以参考下...

    zeo17952020-08-29
  • PythonPython 实现进度条的六种方式

    Python 实现进度条的六种方式

    这篇文章主要介绍了Python 实现进度条的六种方式,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下...

    ゛竹先森゜5412021-08-22
  • Python浅谈Tensorflow 动态双向RNN的输出问题

    浅谈Tensorflow 动态双向RNN的输出问题

    今天小编就为大家分享一篇浅谈Tensorflow 动态双向RNN的输出问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 ...

    Michelleweii5392020-04-08
  • PythonPython处理时间戳和时间计算等的脚本分享

    Python处理时间戳和时间计算等的脚本分享

    这篇文章主要为大家整理总结了5个实用的Python脚本,可以实现时间戳处理和时间计算。文中的示例代码讲解详细,感兴趣的小伙伴可以学习一下...

    念槐聚6812022-07-27
  • Pythonpython2.x实现人民币转大写人民币

    python2.x实现人民币转大写人民币

    这篇文章主要为大家详细介绍了python2.x实现人民币转大写人民币,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    haeasringnar10612021-03-07
  • Pythonpython global的创建和修改实例讲解

    python global的创建和修改实例讲解

    在本篇文章里小编给大家整理了一篇关于python global的创建和修改实例讲解内容,有兴趣的朋友们可以学习下。...

    小妮浅浅8012022-01-05