简体   繁体   English

在tkinter中我如何将入口函数分配给变量

[英]In tkinter how do I assign the entry function to a variable

I am trying to use it in a if statment to check if the username is equal to the accepted answer. 我试图在if语句中使用它来检查用户名是否等于接受的答案。 I used .get() on my ent_username to try to get the name choosen but it did not work. 我在ent_username上使用.get()来尝试选择名称,但是它不起作用。 Is it that it never really gets entered as the username will i need to do more code with the button. 是因为它从未真正输入过,因为用户名需要我使用按钮执行更多代码。 Please help.... 请帮忙....

import tkinter
action = ""
#create new window
window = tkinter.Tk()

#name window
window.title("Basic window")

#window sized
window.geometry("250x200")

#creates label then uses ut
lbl = tkinter.Label(window, text="The game of a life time!", bg="#a1dbcd")

#pack label
lbl.pack()

#create username
lbl_username = tkinter.Label(window, text="Username", bg="#a1dbcd")
ent_username = tkinter.Entry(window)

#pack username
lbl_username.pack()
ent_username.pack()
#attempting to get the ent_username info to store
username = ent_username.get()

#configure window
window.configure(background="#a1dbcd")

#basic enter for password
lbl_password = tkinter.Label(window, text="Password", bg="#a1dbcd")
ent_password = tkinter.Entry(window)

#pack password
lbl_password.pack()
ent_password.pack()
#def to check if username is valid
def question():
    if username == "louis":
        print("you know")
    else:
        print("failed")

#will make the sign up button and will call question on click
btn = tkinter.Button(window, text="Sign up", command=lambda: question())

#pack buttons
btn.pack()

#draw window
window.mainloop()

Your issue is that you are trying to get the contents of the Entry widget when the widget is created. 您的问题是,在创建窗口小部件时,您试图get其内容。 And this will always be empty string. 并且这将始终是空字符串。 You need to move the .get() inside the function so it gets the value when clicking the button. 您需要在函数内部移动.get() ,以便在单击按钮时获取值。

def question():
    username = ent_username.get() # Get value here
    if username == "louis":
        print("you know")
    else:
        print("failed")

Or if ent_username.get() == "louis": 或者, if ent_username.get() == "louis":

You do have the option to use a StringVar but I've never found the need to use it except with OptionMenu widget 您确实可以选择使用StringVar但除OptionMenu小部件外,我从未发现需要使用它

Also, a couple of side notes. 另外,还有一些注意事项。 When using the command argument, you only need lambda when passing in a variable, just make sure to drop the () . 使用command参数时,只需要在传递变量时使用lambda ,只需确保删除()

btn = tkinter.Button(window, text="Sign up", command=question)

And a common practice is to import tkinter as tk . 通常的做法是import tkinter as tk This way you are not prefixing everything with tkinter and instead tk . 这样,您不会在所有内容前加上tkinter前缀,而不会在tk前面加上前缀。 It just saves typing and space. 它只是节省了键入和空间。 So it looks like so, 看起来像这样

ent_username = tk.Entry(window)

The easiest approach is to associate a variable with the Entry widget. 最简单的方法是将变量与Entry小部件关联。 For the variable, you have to use one of the Tkinter variables , and it has to be the tkinter variable associated with that sort of widget. 对于变量,您必须使用Tkinter变量之一 ,并且它必须是与此类小部件关联的tkinter变量。 For the Entry widget, you need a Stringvar. 对于Entry小部件,您需要一个Stringvar。 See Effbot's third-party documentation for the Entry widget . 有关Entry小部件的信息,请参见Effbot的第三方文档

username = tkinter.StringVar()
ent_password = tkinter.Entry(window, textvariable=username)

In the event handler question , you can then access the Tkinter variable's value. 在事件处理程序question ,您可以访问Tkinter变量的值。

if username.get() == name_you_want:
    print "as expected"

That handler function name question is the correct value for the command argument, as Summers said: 如萨默斯所说,该处理函数名称questioncommand参数的正确值:

btn = tkinter.Button(window, text="Sign up", command=question)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM