开发者

Can't copy the value of a Tkinter Entry

So i have this code:

from Tkinter import *
Admin = Tk()

def searches():
    gett = search.get()
    lab = Label(frame开发者_如何学Python, 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜