Python日志屏幕错误 [英] Python log screen bug

查看:50
本文介绍了Python日志屏幕错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 tkinter 登录屏幕应用程序有一个错误,就是当我单击登录"按钮时,它只会显示错误.

My tkinter log in screen app has a bug that is when i click the log in button it just show the error.

from tkinter import *
from tkinter import messagebox


login_page = Tk()
login_page.title("login_page")
login_page.geometry("250x110")

def check_pass():
    if username == name and userpass == password:
        print("this code works")
    else :
        messagebox.askretrycancel(title="Try again",message="Wrong password ")



# name and password entry
log_name = Entry(login_page)
log_name.grid(row=0, column=1, pady=15)
log_pass = Entry(login_page)
log_pass.grid(row=1, column=1, pady=5)
name_label = Label(login_page, text="Name here:- ")
name_label.grid(row=0, column=0, pady=15)
password_label = Label(login_page, text="Password:- ")
password_label.grid(row=1, column=0, pady=5)

# LOG IN BUTTON
login_btn = Button(login_page, text="LOG IN", command=check_pass)
login_btn.grid(row=2, column=0, columnspan=2, ipadx=100, padx=5)

username = log_name.get()
userpass = log_pass.get()
name = "admin"
password= "admin"


mainloop()

推荐答案

问题是因为 username password 变量位于主块中,并且在您运行时输入框的代码是空的(最初),这将''分配给变量.因此,要克服这一点,您应该在函数内部分配变量,现在只在其中使用填充的输入框的值.

The problem is because the username and the password variables are in the main block and when you run the code the entry boxes are empty(initially) which assigns '' to the variables. So to overcome that you should assign the variables inside the function, where now the values of the filled entry box is only taken.

代码:

from tkinter import *
from tkinter import messagebox


login_page = Tk()
login_page.title("login_page")
login_page.geometry("250x110")


def check_pass():
    username = log_name.get()
    userpass = log_pass.get()
    if username == name and userpass == password:
        messagebox.showinfo('Successfull','Login successfull')
    else:
        choice = messagebox.askretrycancel("Try again","Wrong password ")
        if choice == True:
            pass
        else:
            login_page.destroy()

......
# ALL THE SAME LINES OF CODE TILL

name = "admin"
password = "admin"


login_page.mainloop()

此外,由于您使用的是诸如 askretrycancel 之类的消息框,因此您可以根据用户的选择执行某些操作.因此,在这里我已经说过,如果用户单击重试,然后要求他们再次登录,或者关闭应用程序.您可以将其更改为自己喜欢的任何一种(甚至也可以将其删除.)

Also since your using a messagebox like askretrycancel, you can do certain action based on the choice by the user. So here I have said if the user clicks on retry, then ask them to login again, or else close the application. You can change it to whatever you like(or even remove it too.)

此外,建议您说 login_page.mainloop(),以免tkinter在以后的任何步骤中混淆.

And also it is recommended to say login_page.mainloop(), just so that tkinter isnt confused at any later steps.

希望您的臭虫"已清除,请告诉我是否有任何错误:D

Hope your "bug" is cleared and do let me know if any errors :D

欢呼

这篇关于Python日志屏幕错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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