如何将 Tkinter 与 Python 登录屏幕集成?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28156719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:49:04  来源:igfitidea点击:

How can I integrate Tkinter with Python log in screen?

pythonlogintkinterscreen

提问by TheHarpoon

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:

我在这里使用 Tkinter 创建登录屏幕。目前,底部的“让我保持登录”按钮是多余的,并将保持如此。我想要做的是使用此代码:

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)

in conjunction with:

和这个结合:

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")

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!

然而,登录代码不起作用,然后最重要的是我不知道从哪里开始将两者相互集成。我对 python 有点陌生,对 Tkinter 更是如此,但我迫切需要这种帮助!

Thanks in advance!

提前致谢!

采纳答案by Marcin

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()

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.

很抱歉没有详细介绍那里发生的每一行。我把它留给你去弄清楚。它的好运动。但我会说最重要的是command = self._login_btn_clicked。当您单击登录按钮时,将执行此功能。在此函数中,您获取用户名和密码的值,并检查它们是否正确。此外,我没有将任何回调附加到复选框。但这将类似于已经完成的工作。

Edit: Edited as requested in the comments.

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

Login prompt

登录提示

回答by fhdrsdg

You probably want a 'Login' button, right? If you make that, you can bind a function to run when it is clicked using button's the commandargument. In the function that the button calls you can do the checks for the correct username and password. Do not use the while loops though, just check once each time the button is pressed and respond accordingly.

您可能想要一个“登录”按钮,对吗?如果你这样做,你可以绑定一个函数,当它使用按钮的command参数被点击时运行。在按钮调用的函数中,您可以检查用户名和密码是否正确。但是不要使用while循环,每次按下按钮时检查一次并做出相应的响应。