【python】制作一个简单的界面,有手就行的界面~

目录

      • 前言
      • 准备工作
      • 试手小案例
      • 开始我们今天的案例教学
      • 尾语 💝

前言

嗨喽~大家好呀,这里是魔王呐 ❤ ~!

ttkbootstrap 是一个基于 tkinter 的界面美化库,

使用这个工具可以开发出类似前端 bootstrap 风格的 tkinter 桌面程序。

ttkbootstrap 不仅有丰富的案例,同时还有完善的官方文档,可惜是英文的。

不过对于程序员来说,只要用好翻译软件与提供的案例代码,一样可以轻松上手,那么接下来我们就介绍一下这个工具的使用。

准备工作

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

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

  • pip install ttkbootstrap

win + R 输入cmd 输入安装命令 或 在pycharm中点击Terminal(终端) 输入安装命令

试手小案例

可以先来个小案例试试手

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

开始我们今天的案例教学

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)

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)

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

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

尾语 💝

要成功,先发疯,下定决心往前冲!

学习是需要长期坚持的,一步一个脚印地走向未来!

未来的你一定会感谢今天学习的你。

—— 心灵鸡汤

本文章到这里就结束啦~感兴趣的小伙伴可以复制代码去试试哦 😝

👇问题解答 · 源码获取 · 技术交流 · 抱团学习请联系👇

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
乘风的头像乘风管理团队
上一篇 2023年6月21日
下一篇 2023年6月21日

相关推荐