[Tkinter-discuss] Focus for first key press

Mark Summerfield list at qtrac.plus.com
Thu Jun 21 09:03:55 CEST 2012


Hi Michael,

On Wed, 20 Jun 2012 18:33:58 +0200
Michael Lange <klappnase at web.de> wrote:
> Hi Mark,
> 
> Thus spoketh Mark Summerfield <list at qtrac.plus.com> 
> unto us on Wed, 20 Jun 2012 09:55:00 +0100:
> 
> > Hi,
> > 
> > I have a couple of Tkinter applications and what I've noticed is that
> > if I do Alt+F as soon as they start (i.e., for Alt+F,N for New or
> > Alt+F,O for Open), the keypress is ignored! It works fine the second
> > and all subsequent times.
> > 
> > I've tried calling focus() on the invisible root window & on the
> > topmost visible window, but neither has any effect.
> > 
> > Can anyone tell me a solution?
> 
> I am not sure what you are actually trying to do, could you please post a
> simple example that shows the problem?

Below is an example. If I do

$ python3 hello.pyw

and then type Alt+F, the File menu does _not_ pop up. If I then type
Alt+F a second (or subsequent) time it does.

It seems that Tkinter is ignoring the *very first keypress*. If I press
any key (e.g., Space, or x) and _then_ Alt+F, the File menu pops up as
it should.

############################################################
import tkinter as tk
import tkinter.ttk as ttk

class Window(ttk.Frame):

    def __init__(self, master=None):
        super().__init__(master)
        self.menubar = tk.Menu(self.master)
        self.master.config(menu=self.menubar)
        fileMenu = tk.Menu(self.menubar, name="apple")
        fileMenu.add_command(label="Quit", underline=0,
                command=self.master.destroy)
        self.menubar.add_cascade(label="File", underline=0,
                menu=fileMenu)
        helloLabel = ttk.Label(self, text="Hello")
        helloLabel.pack()
        self.pack()
        self.master.minsize(100, 100)

window = Window()
window.master.title("Hello")
window.master.mainloop()
############################################################

Thanks!

-- 
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
    C++, Python, Qt, PyQt - training and consultancy
        "Programming in Go" - ISBN 0321774639
            http://www.qtrac.eu/gobook.html


More information about the Tkinter-discuss mailing list