Beginner to learn Python to make a simple interface with it

foreword

Many Baozi who just started learning python think about starting to figure out some interfaces, but many of them are a bit difficult, and they can’t figure it out by themselves, so they can only copy and paste the code + run it

Now I will take you to understand a code for making a simple interface

insert image description here

ttkbootstrap is a tkinter-based interface beautification library,

Using this tool, you can develop a tkinter desktop program similar to the front-end bootstrap style.

Please add a picture description

ttkbootstrap not only has rich cases, but also has perfect official documents, unfortunately it is in English

However, for us, as long as we make good use of the translation software and the provided case codes, we can easily get started. Then we will introduce the use of this tool next.

Preparation

First of all, you must install ttkbootstrap

The version needs to be new, it is best not to use mirror source installation

pip install ttkbootstrap

win + R, enter cmd to enter the installation command or click Terminal (terminal) in pycharm to enter the installation command

Try a small case

You can try a small case first

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

Please add a picture description

Case implementation code

Start our case teaching today

1. Make an interface

Click on the business card at the end of the article to receive the complete code

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

Please add a picture description

2. User registration box

tk.Label(root, width=10).grid()
完整源码+v:xiaoyuanllsll
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)

insert image description here

3. Gender radio button

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

insert image description here

4. Hobbies

hobby_list = [
    完整源码+v:xiaoyuanllsll
    [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)

insert image description here

5. Birthday

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

insert image description here

6. Submit information button

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)

insert image description here

7. Save data

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)

insert image description here

insert image description here

at last

The article sharing ends here

If you have any questions about Python, you can leave a message in the comment area.

See you in the next article~

Guess you like

Origin blog.csdn.net/yxczsz/article/details/130388207