首先,本例利用了label,entry和button控件
label1=tk.Label(window,text='User name:',width=20,height=3)
label2=tk.Label(window,text='Password:',width=20,height=3)
entryVar1=tk.StringVar()
entry1=tk.Entry(window,textvariable=entryVar1)
entryVar2=tk.StringVar()
entry2=tk.Entry(window,textvariable=entryVar2,show='*')
button1=tk.Button(window,text='登录',command=login)
button2=tk.Button(window,text='取消',command=quit)
其次,布局格式全部用place,可以随意调整控件位置
然后,定义一个login函数,利用tk.messagebox的形式显示登录信息
def login():
user_name=entryVar1.get()
password=entryVar2.get()
if user_name=='admin' and password=='1234':
tk.messagebox.showinfo(title='提示',message='登录成功')
window.quit()
else:
tk.messagebox.showwarning(title='提示',message='用户名或密码有误,请重新输入')
最后,完整的代码login.py
import tkinter as tk
import tkinter.messagebox
window=tk.Tk()
window.title("登录窗口")
window.geometry('500x400') #设置窗口尺寸
label1=tk.Label(window,text='User name:',width=20,height=3)
label1.place(x=100,y=50)
entryVar1=tk.StringVar()
entry1=tk.Entry(window,textvariable=entryVar1)
entry1.place(x=220,y=70)
label2=tk.Label(window,text='Password:',width=20,height=3)
label2.place(x=100,y=100)
entryVar2=tk.StringVar()
entry2=tk.Entry(window,textvariable=entryVar2,show='*')
entry2.place(x=220,y=120)
def login():
user_name=entryVar1.get()
password=entryVar2.get()
if user_name=='admin' and password=='1234':
tk.messagebox.showinfo(title='提示',message='登录成功')
window.quit()
else:
tk.messagebox.showwarning(title='提示',message='用户名或密码有误,请重新输入')
button1=tk.Button(window,text='登录',command=login)
button1.place(x=250,y=150)
button2=tk.Button(window,text='取消',command=quit)
button2.place(x=320,y=150)
window.mainloop()
最后呈现的效果:
版权声明:本文为qq_45323012原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。