python Tkinter get() 来自输入字段的值 [英] python Tkinter get() value from Entry Field

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

问题描述

我对从 Tkinter() 输入字段获取值感到困惑.我有这种代码...

I have getting confused in getting the value from the Tkinter() Entry Field. I have this kind of code...

from Tkinter import*

def valueGET(val1, val2):
    print val1 + "  " + val2

class ContentUI():
def showLogin(self, frame):

        self.contentUI = ContentUI()    

        L1 = Label(frame, text="Name")
        L1.pack( side = LEFT)
        L1.grid()

        E1 = Entry(frame, bd =5)
        E1.pack(side = RIGHT)
        E1.grid()

        L2 = Label(frame, text="Secret")
        L2.pack( side = LEFT)
        L2.grid()       

        E2 = Entry(frame, bd =5, show="*")
        E2.pack(side = RIGHT)
        E2.grid()

        submit = Button(frame, text="Enter", width=15, command=valueGET(E1.get(), E2.get())) 
        submit.grid()

class UIDisplay():
    def play(self):
        root = Tk()

        root.title(title)
        root.geometry(dimension)

        app = Frame(root)

        contentUI = ContentUI()
        contentUI.showLogin(app)

        app.grid()


        root.mainloop()

adkooPlay = UIDisplay()
adkooPlay.play()

但似乎我的 valueGET(val1, val2) 方法不起作用,它没有打印值,甚至没有使用 Tkinter() 条目的 get() 方法获取它,我做错了吗?以及应该怎么做?

but it seems my valueGET(val1, val2) method is not working, it didn't print the value or even get it by using the get() method of the Tkinter() Entry, did I do wrong? and how should it be done?

提前谢谢...

推荐答案

即使在创建 submit 按钮之前,代码调用 valueGET.然后它将函数的返回值作为 command 参数传递给 Button 构造函数.

The code call valueGET even before submit button is created. Then it pass the return value of the function to Button constructor as command argument.

要将函数注册为回调,请替换以下行:

To register the function as callback, replace folloiwng line:

submit = Button(frame, text="Enter", width=15, command=valueGET(E1.get(), E2.get())) 

与:

submit = Button(frame, text="Enter", width=15, command=lambda: valueGET(E1.get(), E2.get())) 

这篇关于python Tkinter get() 来自输入字段的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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