"Useless" very simple login interface example ("you can do it")

Hello everyone!

Today we use the tkinter methods or functions we learned before to complete a login window interface!

I have sorted out the tkinter methods or functions that need to be used in the login interface into the links below. If you are not sure, you can click to view and come back:

1. Create a window

2. Create a canvas and insert a picture

3. The method of placing the position that needs to be used

4. How to create label objects and buttons

5. Use of input box

6. Use of pop-up windows

alright!

Let's take a look at the results shown below!

 The implementation interface is like this!

Then I enter the corresponding account number and password, here I set account number: 123, password: 123

 After clicking Login, a window pops up to confirm the login, click OK to display the login success!

If the input is wrong, the input error window will pop up!

 The complete code is as follows:

# 导入模块
import tkinter as tk
from PIL import Image, ImageTk
import tkinter.messagebox

# 创建窗口
window = tk.Tk()
# 设置窗口标题
window.title("MY Window")
# 设置标题
window.geometry("720x520")

# 创建画布对象
canvas = tk.Canvas(window, bg="white", height=500, width=420)
img = Image.open("./米奇老鼠.jpg")  # 打开图片
photo = ImageTk.PhotoImage(img)  # 使用ImageTk的PhotoImage方法
# 安置图片在画布上面
"""0,0其实代表着我们放置图片的具体;
位置anchor="nw"代表着原点位置;
image代表图片对象是谁"""
image = canvas.create_image(0, 0, anchor="nw", image=photo)
canvas.pack(side="top")

# 定义Var对象获取字符
var_user_name = tk.StringVar()
# 这里是初始化的,我这里为空,如果不想经常输入账号就可以设置一个默认账号
# 比如把下面的代码改为:var_user_name.set("123")
var_user_name.set("")
var_user_password = tk.StringVar()
var_user_password.set("")

# 定义标签
tk.Label(window, text="User name:", font=("Arial", 12), bg="blue").place(x=220, y=390)
tk.Label(window, text="password:", font=("Arial", 12), bg="blue").place(x=220, y=420)
# 定义输入框
entry_user_name = tk.Entry(window, textvariable=var_user_name).place(x=320, y=390)
entry_user_password = tk.Entry(window, textvariable=var_user_password, show="*").place(x=320, y=420)


def Login():
    user_name = var_user_name.get()
    user_password = var_user_password.get()
    if user_name == "123":
        if user_password == "123":
            a = tk.messagebox.askquestion(title="确认", message="是否确认登录")
            if a:
                tk.messagebox.showinfo(title="登录成功!")
    else:
        tk.messagebox.showwarning(title="警告", message="输入错误!")

# 这个还没完善
def Sign_up():
    pass


# 定义按钮
b1 = tk.Button(window, text="Login", command=Login)
b2 = tk.Button(window, text="Sign up", command=Sign_up)
b1.place(x=300, y=460)
b2.place(x=380, y=460)

# 显示窗口
window.mainloop()

The first step is to create a window:

# 创建窗口
window = tk.Tk()
# 设置窗口标题
window.title("MY Window")
# 设置标题
window.geometry("720x520")

The second step is to create a canvas object and insert a background image:

# 创建画布对象
canvas = tk.Canvas(window, bg="white", height=500, width=420)
img = Image.open("./米奇老鼠.jpg")  # 打开图片
photo = ImageTk.PhotoImage(img)  # 使用ImageTk的PhotoImage方法
# 安置图片在画布上面
"""0,0其实代表着我们放置图片的具体;
位置anchor="nw"代表着原点位置;
image代表图片对象是谁"""
image = canvas.create_image(0, 0, anchor="nw", image=photo)
canvas.pack(side="top")

The third step defines the label and input box:

# 定义标签
tk.Label(window, text="User name:", font=("Arial", 12), bg="blue").place(x=220, y=390)
tk.Label(window, text="password:", font=("Arial", 12), bg="blue").place(x=220, y=420)
# 定义输入框
entry_user_name = tk.Entry(window, textvariable=var_user_name).place(x=320, y=390)
entry_user_password = tk.Entry(window, textvariable=var_user_password, show="*").place(x=320, y=420)

The fourth step is to define the Var object for obtaining characters:

# 定义Var对象获取字符
var_user_name = tk.StringVar()
# 这里是初始化的,我这里为空,如果不想经常输入账号就可以设置一个默认账号
# 比如把下面的代码改为:var_user_name.set("123")
var_user_name.set("")
var_user_password = tk.StringVar()
var_user_password.set("")

The fifth step is to set the button:

# 定义按钮
b1 = tk.Button(window, text="Login", command=Login)
b2 = tk.Button(window, text="Sign up", command=Sign_up)
b1.place(x=300, y=460)
b2.place(x=380, y=460)

The last step defines the function:

def Login():
    user_name = var_user_name.get()
    user_password = var_user_password.get()
    if user_name == "123":
        if user_password == "123":
            a = tk.messagebox.askquestion(title="确认", message="是否确认登录")
            if a:
                tk.messagebox.showinfo(title="登录成功!")
    else:
        tk.messagebox.showwarning(title="警告", message="输入错误!")

# 这个还没完善
def Sign_up():
    pass

Remember to call window.mainloop() to display the window content!!!

After writing, we can get the displayed results!

Well, that's all for today's sharing, if there is something unclear or I wrote something wrong, please give me your advice!

Private message, comment me! ! ! ! ! !

I hope that there will be another fun window interface in the future!

Share cute pictures! !

Guess you like

Origin blog.csdn.net/hhR888888/article/details/128351465