如何将Tkinter与Python登录屏幕集成? [英] How can I integrate Tkinter with Python log in screen?

查看:113
本文介绍了如何将Tkinter与Python登录屏幕集成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Tkinter在此处创建登录屏幕.目前,底部的保持登录状态"按钮是多余的,并且将保持不变.我想做的是使用以下代码:

I am using Tkinter to create a login screen here. At the moment, the "keep me logged in" button at the bottom is redundant and will remain so. What I want to do is use this code:

from tkinter import *

root = Tk()

label_1 = Label(root, text="Username")
label_2 = Label(root, text="Password")

entry_1 = Entry(root)
entry_2 = Entry(root)

label_1.grid(row=0, sticky=E)
label_2.grid(row=1, sticky=E)
entry_1.grid(row=0, column=1)
entry_2.grid(row=1, column=1)

checkbox = Checkbutton(root, text="Keep me logged in")
checkbox.grid(columnspan=2)

结合:

username = "john"
input("Username: ")
while not username:
    if username == "john":
        print("Welcome")
    else:
        print("User not found")


password = "password"
while not password:
    input("password: ")
    if password == "password":
        print("Logged in")
    else:
        print("Incorrect password")

但是,登录代码无法正常工作,最重要的是,我不知道从哪里开始将两者相互集成. 我是python的新手,对于Tkinter来说更是新手,但是迫切需要这种帮助!

However the logging in code does not work and then on top of that I do not know where to begin with integrating the two with each other. I am some what new to python and even more so to Tkinter but am desperate for this help!

提前谢谢!

推荐答案

我扩展了您的示例.我制作了一个包含您的登录窗口的类.

I extended your example. I made a class that holds your login window.

from tkinter import *
import tkinter.messagebox as tm


class LoginFrame(Frame):
    def __init__(self, master):
        super().__init__(master)

        self.label_username = Label(self, text="Username")
        self.label_password = Label(self, text="Password")

        self.entry_username = Entry(self)
        self.entry_password = Entry(self, show="*")

        self.label_username.grid(row=0, sticky=E)
        self.label_password.grid(row=1, sticky=E)
        self.entry_username.grid(row=0, column=1)
        self.entry_password.grid(row=1, column=1)

        self.checkbox = Checkbutton(self, text="Keep me logged in")
        self.checkbox.grid(columnspan=2)

        self.logbtn = Button(self, text="Login", command=self._login_btn_clicked)
        self.logbtn.grid(columnspan=2)

        self.pack()

    def _login_btn_clicked(self):
        # print("Clicked")
        username = self.entry_username.get()
        password = self.entry_password.get()

        # print(username, password)

        if username == "john" and password == "password":
            tm.showinfo("Login info", "Welcome John")
        else:
            tm.showerror("Login error", "Incorrect username")


root = Tk()
lf = LoginFrame(root)
root.mainloop()

很抱歉,没有仔细检查每一行的内容.我留给你弄清楚.它很好的锻炼.但我会说最重要的是command = self._login_btn_clicked.当您单击登录按钮时,将执行此功能.在此功能中,您将获取用户名和密码的值,并检查它们是否正确.另外,我没有将任何回调附加到该复选框.但这将与已经完成的操作类似.

Sorry for not going over every single line what is happening there. I leave it to you to figure out. Its good exercise. But I will say that the most important is command = self._login_btn_clicked. This function will be executed when you click login button. In this function, you take the values of username and password, and check if they are correct. Also I did not attach any callbacks to the checkbox. But it would be similar to what is already done.

根据评论中的要求进行编辑.

Edited as requested in the comments.

这篇关于如何将Tkinter与Python登录屏幕集成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆