Tkinter / Entry widget problem

A

Andras Szabo

Hello. I searched the archives but couldn't find a solution to a
problem related to the Entry widget in Tkinter.

When creating a pop-up window in an app, which contains an Entry
widget, I want this widget to contain some default string, to have all
this default string selected (as if the user had manually selected
everything), and to have the focus transferred to this widget.

(The idea is then that if the window pops up, the user won't have to
click or press Tab any more before being able to type what is needed
in the textbox, overwriting what is written there already.)

I thought this might be the way to go:

entrybox=Entry(toplevel_parent_window)
entrybox.insert(0,"Some default string")
entrybox.select_range(0,END)
entrybox.focus_set()
entrybox.pack()

But it doesn't seem to work - the focus is not transferred to the
Entry widget, and the text does not appear to be selected (even though
after this entrybox.selection_present() returns True).

What am I doing wrong?

andras
 
P

Peter Otten

Andras said:
Hello. I searched the archives but couldn't find a solution to a
problem related to the Entry widget in Tkinter.

When creating a pop-up window in an app, which contains an Entry
widget, I want this widget to contain some default string, to have all
this default string selected (as if the user had manually selected
everything), and to have the focus transferred to this widget.

(The idea is then that if the window pops up, the user won't have to
click or press Tab any more before being able to type what is needed
in the textbox, overwriting what is written there already.)

I thought this might be the way to go:

entrybox=Entry(toplevel_parent_window)
entrybox.insert(0,"Some default string")
entrybox.select_range(0,END)
entrybox.focus_set()
entrybox.pack()

But it doesn't seem to work - the focus is not transferred to the
Entry widget, and the text does not appear to be selected (even though
after this entrybox.selection_present() returns True).

What am I doing wrong?

Nothing, I would think. Can you post a minimal runnable example?

Peter
 
J

John McMonagle

Andras said:
Hello. I searched the archives but couldn't find a solution to a problem
related to the Entry widget in Tkinter.

When creating a pop-up window in an app, which contains an Entry widget,
I want this widget to contain some default string, to have all this
default string selected (as if the user had manually selected
everything), and to have the focus transferred to this widget.

(The idea is then that if the window pops up, the user won't have to
click or press Tab any more before being able to type what is needed in
the textbox, overwriting what is written there already.)

I thought this might be the way to go:

entrybox=Entry(toplevel_parent_window)
entrybox.insert(0,"Some default string")
entrybox.select_range(0,END)
entrybox.focus_set()
entrybox.pack()

But it doesn't seem to work - the focus is not transferred to the Entry
widget, and the text does not appear to be selected (even though after
this entrybox.selection_present() returns True).

What am I doing wrong?

andras

You're probably not updating after the focus_set.

Try the following:

from Tkinter import *
r = Tk()


def click():
t = Toplevel(r)
e = Entry(t)
e.pack()
b = Button(t, text='Close', command=t.destroy)
b.pack()
e.insert(0, 'Default')
e.select_range(0, END)
e.focus_set()
r.update()



b = Button(r, text='Press', command=click)
b.pack()

r.mainloop()



Regards,

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top