先上效果图
界面设计
首先,需要一个canvas来展示 欢迎登录的图片,使用pack()方法将canvas固定在界面的顶部
两个输入框提示,使用Label标签,使用place方法准确的定位到界面上
输入框使用Entry控件,密码控件要设置show参数,这样才能不显示输入的面目
最后创建一个Button按钮,使用place方法固定在界面的指定位置
点击登录后,获取输入的用户名,使用messagebox显示提示信息
所有的控件,方法都是前面的章节里讲解过的,没什么难度,示例代码如下
import tkinter as tk
from PIL import ImageTk
from tkinter import messagebox
window = tk.Tk()
window.title('登录界面') # 设置窗口的标题
window.geometry('500x400') # 设置窗口的大小
# 画布放在window的顶部
canvas = tk.Canvas(window, height=200, width=500)
canvas.pack(side='top')
image_file = ImageTk.PhotoImage(file='./pic/login.png')
# 以图片中心定位到 (250, 100) 的位置上
image = canvas.create_image(250, 100, anchor='center', image=image_file)
# 输入框的提示语
tk.Label(window, text="用户名:").place(x=75, y=250, anchor='nw')
tk.Label(window, text="密码 :").place(x=75, y=280, anchor='nw')
# 两个输入框
usr_name_var = tk.StringVar()
password_var = tk.StringVar()
tk.Entry(window, textvariable=usr_name_var).place(x=150, y=250, anchor='nw')
tk.Entry(window, textvariable=password_var, show='*').place(x=150, y=280, anchor='nw')
def login():
user_name = usr_name_var.get()
messagebox.showinfo(title='登录成功', message="欢迎你, {name}".format(name=user_name))
# 登录按钮
tk.Button(window, text='登录', command=login).place(x=280, y=350)
window.mainloop()
扫描关注, 与我技术互动
QQ交流群: 211426309
分享日常研究的python技术和遇到的问题及解决方案