Lor*_*rak 0 python if-statement tkinter python-3.x tkinter-entry
作为菜鸟,我在尝试学习新技能时弄乱了一些代码,现在我正在 Python 3.x 上学习 TKinter。一切都很好,直到有条件,但我正在尝试启动一些简单的东西,例如“用户访问界面”模拟器。只是经典的 if True 条件。
\n因此,作为用户,您需要介绍您的用户名和密码。我只是做了密码限制以使示例更容易。但由于某种原因,总是当我写出正确的密码时,条件判断条件为False并执行else。
\n我尝试更改这个词,甚至制作一个“if password_input is str:”来检查条件是否有效的最简单方法,但仍然收到错误。
\n在此先感谢您的帮助。代码如下:
\nfrom tkinter import *\n\nROOT = Tk()\nROOT.geometry("500x300+50+50")\nROOT.config(bg="grey")\nROOT.title("Ventana de acceso")\n\nuser_tag = Label(text="Usuario: ")\nuser_tag.grid(row=0, column=0)\nuser_tag.config(bg="grey", font=("Arial", 16))\n\npassword_tag = Label(text="Contrase\xc3\xb1a: ")\npassword_tag.grid(row=1, column=0)\npassword_tag.config(bg="grey", font=("Arial", 16))\n\nuser_input = Entry()\nuser_input.grid(row=0, column=1)\nuser_input.config(bg="red", font=("Comic Sans", 16))\nuser_input.insert(0, "Introduce tu usuario...") # Olvid\xc3\xa9 poner el argumento de posici\xc3\xb3n\n\npassword_input = Entry()\npassword_input.grid(row=1, column=1)\npassword_input.config(bg="red", font=("Comic Sans", 16), show="*")\npassword_input.insert(0, "***********")\n\nspace_0 = Label(text=" ")\nspace_0.grid(row=3)\nspace_0.config(bg="grey")\n\nspace_1 = Label(text=" ")\nspace_1.grid(row=4, column=0)\nspace_1.config(bg="grey")\n\nspace_2 = Label(text=" ")\nspace_2.grid(row=2, column=0)\nspace_2.config(bg="grey")\n\ndef click_check_button():\n if password_input == "python":\n access_granted = Label(text="\xc2\xa1Acceso concedido!")\n access_granted.grid(row=4, column=1)\n access_granted.config(bg="green", font=("Arial", 22))\n\n else:\n access_denied = Label(text="\xc2\xa1Acceso denegado!")\n access_denied.grid(row=4, column=1)\n access_denied.config(bg="red", font=("Arial", 22))\n\n\ncheck_button = Button(text="Acceder", font=("Arial", 20))\ncheck_button.grid(row=2, column=1)\ncheck_button.config(bg="yellow", activebackground="orange", command=click_check_button)\n\n\nROOT.mainloop()\n
Run Code Online (Sandbox Code Playgroud)\n
需要调用get()
获取widgetpassword_input
的内容Entry
if password_input.get() == "python":
Run Code Online (Sandbox Code Playgroud)
正如所写,password_input
评估其Entry
本身,正如您可能认为的那样,它不是一个字符串!希望有帮助!
归档时间: |
|
查看次数: |
46 次 |
最近记录: |