shahar
shahar

Reputation: 79

how can i get the input from the entry widget, tkinter, python

I searched how to get the input and this should work but it doesnt... i dont understand why it doesnt working... its start running and get stuck in the mainloop line... it shows nothing

from Tkinter import *


class GUI:

def __init__(self):

    self.root = Tk()

    self.label1 = Label(self.root, text="name")
    self.label2 = Label(self.root, text="password")
    self.entry1 = Entry(self.root)
    self.entry2 = Entry(self.root, show="*")
    self.button = Button(self.root, text="hello", command=self.printName)
    self.button.pack()

    self.label1.grid(row=0, sticky=W)  # N, S, E, W
    self.label2.grid(row=1, sticky=E)
    self.entry1.grid(row=0, column=1)
    self.entry2.grid(row=1, column=1)

    self.c = Checkbutton(self.root, text="forgot my password")
    self.c.grid(columnspan=2)

    self.root.mainloop()

def printName(self):

    print self.entry1.get()


hi = GUI()

Upvotes: 0

Views: 85

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386342

The problem is that you are using both grid and pack for widgets that share the same parent. You can't do that -- you have to pick one or the other.

Also, to be pedantic you should ove the call of self.root.mainloop() outside of the __init__. The reason is that with it being inside, the object is never fully created because mainloop won't return until the widget is destroyed. Typically you call mainloop in the same scope that created the root window.

For example:

hi = GUI()
hi.root.mainloop()

If you don't like referencing the internal widget, give GUI a method like start or mainloop:

class GUI():
    ...
    def start(self):
        self.root.mainloop()

...
hi = GUI()
hi.start()

Upvotes: 1

Related Questions