首页 > 解决方案 > Tkinter Python gui登录不起作用,我该如何解决?

问题描述

我的 GUI 登录跳过了 If 语句的 If 部分,我不明白会出现什么问题,我该如何修复这个错误?当用户名和密码正确时,它应该使用 If 部分,但由于某种原因,它似乎并不认为它是其中的一部分。

from tkinter import *

root = Tk()
root.title("My Login")
root.geometry("650x650")

frame = Frame(root)

app = Frame(root)
app.grid

l = Label(root, text = "Login",font="Times 30", padx=5,  pady=5)
l.grid()

l1 = Label(root, text = "Username:",font="Times 30", padx=5,  pady=5)
l1.grid()

l2 = Label(root, text = "Password:",font="Times 30", padx=5,  pady=5)
l2.grid()

user = Entry(root)
user.grid( row= 1, column= 2)
user.configure(font = ("Courier", 44))
code = Entry(root)
code.grid( row= 2, column= 2)
code.configure(font = ("Courier", 44))

operator = user.get()
passcode = code.get()

admin = ""  #This would be the Username
password = ""  #This would be the Password

def enter():
    if (operator == admin and passcode == password):
        import subprocess
        subprocess.Popen("") #This would be a directory to open
    else: 
        l3 = Label(root, text = "check login", font=("Courier", 22))
        l3.grid()

b1 = Button(root, text = "Enter", command = enter, font=("Courier", 44))
b1.grid()

root.mainloop()

标签: pythontkinter

解决方案


您不能从 get() 预先分配值,您必须在需要它们的时候调用它们。

def enter():
    if user.get() == admin and code.get() == password:
        import subprocess # this belongs at the top of the file
        subprocess.Popen("")
    else: 
        l3 = Label(root, text = "check login", font=("Courier", 22))
        l3.grid()

推荐阅读