Tkinter绑定和.get()问题 [英] Tkinter binding and .get() issues

查看:169
本文介绍了Tkinter绑定和.get()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将ENTER键绑定到电子邮件密码输入框.这样,当我输入所有三个项目时,可以按Enter调用callback()函数.我遇到的另一个问题是.get()方法.似乎没有将我在输入框中键入的值分配给我在代码中定义的变量.

I am trying to bind the ENTER key to the email password entry box. That way when I have entered all three items I can press enter to call the callback() function. The other issue I have is with the .get() method. It does not seem to assign the values I typed into the entry boxes to the variables I defined in my code.

from Tkinter import *

root = Tk()

# grabs the values in the entry boxes and assigns them to variablse
def callback():
    steamUser = steamUserW.get()
    steamPass = steamPassW.get()
    emailPass = emailPassW.get()
    root.destroy()

# labels for each entry
Label(root, text="Steam Username").grid(row=0)
Label(root, text="Steam Password").grid(row=1)
Label(root, text="Email Password").grid(row=2)

# entry and button widgets
steamUserW = Entry(root)
steamPassW = Entry(root, show="*")
emailPassW = Entry(root, show="*")
submit = Button(root, text="Submit", command=callback)

# bind the ENTER key to callback function
emailPassW.bind("<Return>", callback)

# space out the widgets
steamUserW.grid(row=0, column=1)
steamPassW.grid(row=1, column=1)
emailPassW.grid(row=2, column=1)
submit.grid(row=3, column=1)

root.mainloop()

print steamUser

编辑,我的新代码修复了.get()问题,但按Enter键仍然存在绑定问题

from Tkinter import *

class gui:
    def __init__(self, master):
        self.master = master

        # labels for each entry
        Label(self.master, text="Steam Username").grid(row=0)
        Label(self.master, text="Steam Password").grid(row=1)
        Label(self.master, text="Email Password").grid(row=2)

        # button widget
        self.steamUserW = Entry(self.master)
        self.steamPassW = Entry(self.master, show="*")
        self.emailPassW = Entry(self.master, show="*")
        self.submit = Button(self.master, text="Submit", command=self.assign)

        # bind the ENTER key to callback function
        self.emailPassW.bind("<Return>", self.assign)
        self.emailPassW.bind("<KP_Enter>", self.assign)

        # space out the widgets
        self.steamUserW.grid(row=0, column=1)
        self.steamPassW.grid(row=1, column=1)
        self.emailPassW.grid(row=2, column=1)
        self.submit.grid(row=3, column=1)

    # grabs the values in the entry boxes and assigns them to variablse
    def assign(self):
        self.steamUser = self.steamUserW.get()
        self.steamPass = self.steamPassW.get()
        self.emailPass = self.emailPassW.get()
        self.close()

    # closes GUI window
    def close(self):
        self.master.destroy()


root = Tk()
userGui = gui(root)
root.mainloop()
print userGui.steamUser

推荐答案

分配失败的原始错误是因为您正在将值分配给局部变量.它正在工作,但是回调之外的任何函数都看不到它们.解决方法是将变量声明为全局变量,或者重新构造代码以使用类,并使变量具有该类的属性.

The original error with the assign not working was because you were assigning the values to local variables. It was working, but no function outside of the callback could see them. The fix is to either declare the variables as global, or re-architect your code to use classes, and make the variables attributes of the class.

绑定的问题是您的回调需要接受一个参数.当您使用bind时,被调用的函数将始终传递一个参数.此参数是代表事件的对象.从该对象中,您可以获取触发事件的小部件,光标的x/y坐标以及其他一些信息.

The problem with the binding is that your callback needs to accept an argument. When you use bind, the function that is called will always be passed an argument. This argument is an object representing the event. From this object you can get the widget that triggered the event, the x/y coordinates of the cursor, and several other bits of information.

对于您的原始代码,您可以通过以下方式对其进行修复:

For your original code, you can fix it like this:

def callback(event):
    global steamUser, steamPass, emailPass
    steamUser = steamUserW.get()
    steamPass = steamPassW.get()
    emailPass = emailPassW.get()
    root.destroy()

由于您已切换为使用类,因此必须为事件指定一个参数,为self指定一个参数:

Since you've switched to using a class, you have to have one argument for the event and one argument for self:

def callback(self, event):
    ...

这种传递参数的行为已得到充分证明.例如,事件和绑定上的effbot页面说:

This behavior of passing in an argument is well documented. For example, the effbot page on Events and Bindings says:

"如果在窗口小部件中发生了与事件描述相匹配的事件, 给定的处理程序将与描述事件的对象一起调用."

"If an event matching the event description occurs in the widget, the given handler is called with an object describing the event."

这篇关于Tkinter绑定和.get()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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