python tkinter 做简易登录界面

python tkinter 做简易登录界面

不要问我为什么突然开始搞tkinter了,问就是我们学校查成绩的网站一天能崩三次。

引入库

import tkinter as tk
import tkinter.messagebox

tkinter,一个python的gui的库。用pip install tkinter就可以了。

不过这里要注意特别的引用一下tkinter.messagebox,不然会报错。

判断用户名密码

既然是登录界面,最重要的就是一个用户名和密码的判断

PS:自己先在程序的同一目录下创建一个txt

def compare():
    name = var_usr_name.get()
    pwd = var_usr_pwd.get()
    usrName, usrPwd = name,pwd
    getData = removeN()
    g = 0
    while(g<(len(getData)-1)):
        if usrName == getData[g] and g%2 == 0 and usrPwd == getData[g+1]:
            tk.messagebox.showinfo(title='login', message='Welcome, {}. You have successfully logged in.'.format(usrName))
            break
        g = g + 1
    if g == len(getData)-1:
        tk.messagebox.showwarning(title = "error",message = "The user name and password are incorrect.")

这里要注意以下,len(getData)是必须减一的,不然会出现list out of range的报错。

而g%2就是在遍历txt的时候限制一下,就不会读到一个账号的密码和另一个账号的用户名了

txt内容处理
 

PS:我这个不一定是最优解。不要问我为什么不用replace,我也忘了。

PSS:不要问我为什么这样起变量名。

def removeN():
    asd = " "
    c = ""
    bd = []
    de = 0
    fg = "\n"
    with open('data.txt','r') as usrFile:
        asd = usrFile.readlines()
        for j in range(len(asd)):
            er = asd[j]
            pos = er.index(fg)
            cex = er[0:pos]
            bd.append(cex)
            de = 0
            cex = ""
        return bd

读取txt的内容转化list的时候,内容后面是自动加了"\n"的,所以必须要把它去掉。

语言转换功能

def language():
    global lan
    answer = False
    if lan == "english":
        answer = tk.messagebox.askquestion(title='language changing', message='Do you want to switch to Spanish?')
        if answer == "yes":
            lan = "spanish"
            d_name.set("elija el lenguaje")
            e_name.set("el nombre")
            f_name.set("cifra")
        else:
            pass
    elif lan == "spanish":
        answer = tk.messagebox.askquestion(title='language changing', message='Do you want to switch to English?')
        if answer == "yes":
            lan = "english"
            d_name.set("select language")
            e_name.set("user name")
            f_name.set("password")
        else:
            pass

主体程序

window = tk.Tk()
window.title('Fake PowerSchool Help Students!')
window.geometry('500x500')
z = tk.Label(window,anchor = 'w',bg='white',
                    justify = 'center', width=500, height=500)
z.place(x = 0, y = 0)
a = tk.Label(window,anchor = 'w',text="PowerSchool SIS",
                    fg = 'white', bg='darkblue', font=('TimesNewRoman', 30),
                    justify = 'center', width=20, height=1)
a.place(x = 50, y = 100)

b = tk.Label(window,anchor='nw',text="Student and Parent Sign In",
             fg='black',bg='white',font=('TimesNewRoman',12),
             justify='center',width=50,height=20)
b.place(x = 50, y = 150)

c = tk.Button(window, text='language', width=15,
              height=2, command=language)
c.place(x = 270,y = 200)

d_name = tk.StringVar()
d_name.set("select language")
d = tk.Label(window,anchor='nw',textvariable=d_name,
             fg='black',bg='white',font=('TimesNewRoman',12),
             justify='center',width=20,height=2)
d.place(x = 50, y = 210)

e_name = tk.StringVar()
e_name.set("user name")
e = tk.Label(window,anchor='nw',textvariable=e_name,
             fg='black',bg='white',font=('TimesNewRoman',12),
             justify='center',width=50,height=1)
e.place(x = 50, y = 290)

f_name = tk.StringVar()
f_name.set("password")
f = tk.Label(window,anchor='nw',textvariable=f_name,
             fg='black',bg='white',font=('TimesNewRoman',12),
             justify='center',width=50,height=1)
f.place(x = 50, y = 350)

g = tk.Button(window, text='login', width=4,
              height=1, command=compare)
g.place(x = 330,y = 410)

var_usr_name = tk.StringVar()
entry_usr_name = tk.Entry(window, textvariable=var_usr_name)
entry_usr_name.place(x=270, y=290)
var_usr_pwd = tk.StringVar()
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')
entry_usr_pwd.place(x=270, y=350)

这个没啥好说的,基本学了几小时tkinter的人都应该会了。

这里推荐一个大佬:木子欢儿。TA的这篇关于tkinter的文章就很适合初学tkinter的人参考链接在这里

总体程序

import tkinter as tk
import tkinter.messagebox

lan = "english"
getData = []

def signUp(usrName, usrPwd):
    with open('data.txt','a') as usrFile:
        usrFile.writelines("{}\n{}\n".format(usrName, usrPwd))

def compare():
    name = var_usr_name.get()
    pwd = var_usr_pwd.get()
    usrName, usrPwd = name,pwd
    getData = removeN()
    g = 0
    while(g<(len(getData)-1)):
        if usrName == getData[g] and g%2 == 0 and usrPwd == getData[g+1]:
            tk.messagebox.showinfo(title='login', message='Welcome, {}. You have successfully logged in.'.format(usrName))
            break
        g = g + 1
    if g == len(getData)-1:
        tk.messagebox.showwarning(title = "error",message = "The user name and password are incorrect.")
        

def removeN():
    asd = " "
    c = ""
    bd = []
    de = 0
    fg = "\n"
    with open('data.txt','r') as usrFile:
        asd = usrFile.readlines()
        for j in range(len(asd)):
            er = asd[j]
            pos = er.index(fg)
            cex = er[0:pos]
            bd.append(cex)
            de = 0
            cex = ""
        return bd
 
def language():
    global lan
    answer = False
    if lan == "english":
        answer = tk.messagebox.askquestion(title='language changing', message='Do you want to switch to Spanish?')
        if answer == "yes":
            lan = "spanish"
            d_name.set("elija el lenguaje")
            e_name.set("el nombre")
            f_name.set("cifra")
        else:
            pass
    elif lan == "spanish":
        answer = tk.messagebox.askquestion(title='language changing', message='Do you want to switch to English?')
        if answer == "yes":
            lan = "english"
            d_name.set("select language")
            e_name.set("user name")
            f_name.set("password")
        else:
            pass
    

window = tk.Tk()
window.title('Fake PowerSchool Help Students!')
window.geometry('500x500')
z = tk.Label(window,anchor = 'w',bg='white',
                    justify = 'center', width=500, height=500)
z.place(x = 0, y = 0)
a = tk.Label(window,anchor = 'w',text="PowerSchool SIS",
                    fg = 'white', bg='darkblue', font=('TimesNewRoman', 30),
                    justify = 'center', width=20, height=1)
a.place(x = 50, y = 100)

b = tk.Label(window,anchor='nw',text="Student and Parent Sign In",
             fg='black',bg='white',font=('TimesNewRoman',12),
             justify='center',width=50,height=20)
b.place(x = 50, y = 150)

c = tk.Button(window, text='language', width=15,
              height=2, command=language)
c.place(x = 270,y = 200)

d_name = tk.StringVar()
d_name.set("select language")
d = tk.Label(window,anchor='nw',textvariable=d_name,
             fg='black',bg='white',font=('TimesNewRoman',12),
             justify='center',width=20,height=2)
d.place(x = 50, y = 210)

e_name = tk.StringVar()
e_name.set("user name")
e = tk.Label(window,anchor='nw',textvariable=e_name,
             fg='black',bg='white',font=('TimesNewRoman',12),
             justify='center',width=50,height=1)
e.place(x = 50, y = 290)

f_name = tk.StringVar()
f_name.set("password")
f = tk.Label(window,anchor='nw',textvariable=f_name,
             fg='black',bg='white',font=('TimesNewRoman',12),
             justify='center',width=50,height=1)
f.place(x = 50, y = 350)

g = tk.Button(window, text='login', width=4,
              height=1, command=compare)
g.place(x = 330,y = 410)

var_usr_name = tk.StringVar()
entry_usr_name = tk.Entry(window, textvariable=var_usr_name)
entry_usr_name.place(x=270, y=290)
var_usr_pwd = tk.StringVar()
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')
entry_usr_pwd.place(x=270, y=350)

window.mainloop()

效果

​​​​​​​

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A Python 萌新花花

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值