意想不到的Python ttkbootstrap 製作賬戶註冊信息界面

嗨害大家好,我是小熊貓🖤

今天給大家來整一個舊活~

在這裡插入圖片描述

前言

ttkbootstrap 是一個基於 tkinter 的界面美化庫,使用這個工具可以開發出類似前端 bootstrap 風格的tkinter 桌面程序。


ttkbootstrap不僅有豐富的案例,同時還有完善的官方文檔,可惜是英文的。不過對於程序員來說,只要用好翻譯軟件與提供的案例代碼,一樣可以輕鬆上手,那麼接下來我們就介紹一下這個工具的使用。


準備工作

首先肯定是需要安裝一下== ttkbootstrap==

版本要新,最好不要用鏡像源安裝
pip install ttkbootstrap

可以先來個小案例試試手

有什麼python相關報錯解答自己不會的、或者源碼資料/模塊安裝/女裝大佬精通技巧 都可以來這裡:(//jq.qq.com/?_wv=1027&k=2Q3YTfym)或者文末私號問我

import ttkbootstrap as ttk
from ttkbootstrap.constants import *

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

root.geometry('300x150')  python學習交流群:660193417###

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

請添加圖片描述

開始我們今天的案例教學

完整代碼,複製運行即可(明示👍👍👍)

import ttkbootstrap as tk

root = tk.Window(themename='litera')
root.geometry('350x500+500+500')
root.title('萌新-註冊頁面')python學習交流群:660193417###
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()


請添加圖片描述
1、做個界面

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

請添加圖片描述
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學習交流群:660193417###

請添加圖片描述
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)

請添加圖片描述
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)

請添加圖片描述
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())

請添加圖片描述
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)

請添加圖片描述
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學習交流群:660193417###

請添加圖片描述
請添加圖片描述

有什麼python相關報錯解答自己不會的、或者源碼資料/模塊安裝/女裝大佬精通技巧 都可以來這裡:(//jq.qq.com/?_wv=1027&k=2Q3YTfym)或者文末私號問我

就是這些啦,希望這篇文章對你有所幫助鴨~

我是小熊貓,咱下篇文章見(✿◡‿◡)

在這裡插入圖片描述