1

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

提前致谢 ...

4

3 回答 3

5

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())) 
于 2013-10-16T12:27:28.580 回答
0

您需要在函数的打印命令中使用方括号

def valueGET(val1, val2):
    print val1 + "  " + val2
于 2017-09-08T16:52:46.603 回答
-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()
于 2017-11-07T15:54:50.510 回答