Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

python Tkinter get() value from Entry Field

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

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?

thanks in advance ...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

with:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...