gadss
gadss

Reputation: 22489

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 ...

Upvotes: 1

Views: 25247

Answers (3)

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

Upvotes: 0

user8581232
user8581232

Reputation: 1

you need brackets in the print command in your function

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

Upvotes: 0

falsetru
falsetru

Reputation: 368904

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

Upvotes: 5

Related Questions