python - 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()
解决方案
您不能从 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()
推荐阅读
- r - 如何查询母公司是否在子公司所在城市?
- javascript - CloudFront 像素跟踪器(简单版)
- vb.net - 左键单击时的 Visual Basic 声音
- docker - 安装 docker 后启动 virtualbox 出现问题
- html - 下拉菜单项行不能从同一位置顺利开始
- julia - 通过具有相同抽象父类型的对象集合保持/循环的最佳实践 (Julia)
- excel - 从数组中返回值大于 0 在下面的数组中
- asp.net - ASP.NET MVC CORE - 多对多关系 - 创建视图
- python - 使用 Django 删除一对多相关模型
- amazon-web-services - Gunicorn Elastic Beanstalk 更改超时