manuelBetancurt
manuelBetancurt

Reputation: 16168

python clear textField "Entry"

I am playing with python, and need to clear the entry widget after button is pressed, I am having problems with making the widget global?

here my code

#!/usr/bin/python3
from tkinter import *
from tkinter import ttk

root = Tk()
usernameVal = StringVar()

def submitForm(*args):
    try:
        print("submitForm pressed")

        print('name is %s' % usernameVal.get()) #mira como hace el print!

        #usernameVal.clear(1.0, END)  ??

    except ValueError:
        pass

def main():

#container view
    mainframe = ttk.Frame(root, padding="3 3 12 12") #  mainframe contained by root!, init

    mainframe.grid(column=0, row=0, sticky=(N, W, E, S)) #  add subview mainframe
    mainframe.columnconfigure(0, weight=1)
    mainframe.rowconfigure(0, weight=1)

#UI widgets
    createLbl = ttk.Label(mainframe, text='Create login & Password')
    createLbl.grid(column=2 , row=1, sticky=(W,E))

# user name
    nameLbl = ttk.Label(mainframe, text='User Name')
    nameLbl.grid(column=1 , row=2, sticky=(W,E))

    #usernameVal = StringVar()
    usernameTf = ttk.Entry(mainframe, textvariable = usernameVal)
    usernameTf.grid(column=2 , row=2, sticky=(W,E))

    button = ttk.Button(mainframe, text='Create User', command=submitForm)
    button.grid(column=3 , row=2, sticky=(W,E))  

#my loop
    root.mainloop()

if __name__ == "__main__": main()

so how do I clear my textField, also is this a good way to get the text from the entry widget? thanks!

Upvotes: 0

Views: 3313

Answers (1)

Alok
Alok

Reputation: 2689

I have changed the code a little bit here & there according to my coding style. But you will get the gist of it

from Tkinter import *
from Tkinter import Tk


root = Tk()
usernameVal = StringVar()
class myclass():

    def __init__(self):
        self.main()

    def submitForm(self,*args):
        try:
            print("submitForm pressed")

            print('name is %s' % self.usernameVal.get()) #mira como hace el print!
            self.usernameVal.set("")

            #usernameVal.clear(1.0, END)  ??

        except ValueError:
            pass

    def main(self):

    #container view
        mainframe = Frame(root) #  mainframe contained by root!, init

        mainframe.grid(column=0, row=0, sticky=(N, W, E, S)) #  add subview mainframe
        mainframe.columnconfigure(0, weight=1)
        mainframe.rowconfigure(0, weight=1)

    #UI widgets
        createLbl = Label(mainframe, text='Create login & Password')
        createLbl.grid(column=2 , row=1, sticky=(W,E))

    # user name
        nameLbl = Label(mainframe, text='User Name')
        nameLbl.grid(column=1 , row=2, sticky=(W,E))

        self.usernameVal = StringVar()
        usernameTf = Entry(mainframe, textvariable = self.usernameVal)
        usernameTf.grid(column=2 , row=2, sticky=(W,E))

        button = Button(mainframe, text='Create User', command=self.submitForm)
        button.grid(column=3 , row=2, sticky=(W,E))  

    #my loop
        root.mainloop()

if __name__ == "__main__":
    myclass()

What i've done basically is created a class & used self operator. To get the value of user input i've used self.usernameVal.get & to clear it i've used self.usernameVal.set=''.

Upvotes: 4

Related Questions