0

So i have this code:

from Tkinter import *
Admin = Tk()

def searches():
    gett = search.get()
    lab = Label(frame, text='searching for ' + gett)
    lab.pack(side='bottom')
frame = Frame(Admin)
frame.pack()
search = Entry(frame)
search.pack(side='left')

button = Button(frame, text='Search', command=searches)
button.pack(side='right')

getts = search.get()

Admin.mainloop()

other = getts

print other

but the "other" doesn't inherit the text in the entry please help.

bluish
  • 26,356
  • 27
  • 122
  • 180
psao
  • 1
  • 2
  • `getts` is being assigned the value before `mainloop` (i.e. before your window is even being displayed); assigning `other` to `getts` won't help you. Instead, look into callbacks for an appropriate time to get the value. – li.davidm Apr 05 '11 at 02:22

2 Answers2

0

You are calling search.get() and assigning the result to getts -- and then assigning that to other -- prior to the GUI ever being displayed on the screen. Because of this the result of search.get() will be the empty string since you don't pre-load the widget with any data. And because getts is empty, when it is assigned to other, other is empty, too.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

You're setting getts before the main loop executes. When you run the program and enter something in the Entry field, that doesn't change the value of a variable you've already set.

If you want to read the value of the entry field after Admin.mainloop() exits, you'll have to have a Tkinter object set the value of getts in response to some GUI action. One way would be with an on-exit callback. See Intercept Tkinter "Exit" command? for example. In your case you want something like

Admin.protocol("WM_DELETE_WINDOW", SomeFunctionWhichSetsGetts)

Or better yet (perusing that linked post further) create a subclass of Entry with a destroy() method that sets getts for you.

Community
  • 1
  • 1
Peter Milley
  • 2,768
  • 19
  • 18