来自Entry Field的python Tkinter get()值

时间:2013-10-16 12:26:06

标签: python tkinter

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

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()Entry的get()方法获取它,我做错了吗?应该怎么做?

提前感谢...

3 个答案:

答案 0 :(得分:4)

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

要将该功能注册为回调,请替换以下行:

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())) 

答案 1 :(得分:0)

你需要在函数

中的print命令中使用括号
def valueGET(val1, val2):
    print val1 + "  " + val2

答案 2 :(得分:-1)

from tkinter import *
import tkinter as tk
root =tk.Tk()
mystring =tk.StringVar(root)
def getvalue():
    print(mystring.get())
e1 = Entry(root,textvariable = mystring,width=100,fg="blue",bd=3,selectbackground='violet').pack()
button1 = tk.Button(root, 
                text='Submit', 
                fg='White', 
                bg= 'dark green',height = 1, width = 10,command=getvalue).pack()

root.mainloop()