1

I am trying to make an on screen keyboard. It works for widgets in the window but when I press a button it moves focus from the window I am trying to type in to the window that has the buttons. How do I prevent python from moving?

from tkinter import *
from pynput.keyboard import Key, Controller
keyboard = Controller()

class App:
    def __init__(self, master):
        self.entry = Entry()
        self.buttonOne = Button(text='1')
        self.buttonTwo = Button(text='2')
        self.buttonThree = Button(text='3')
        self.buttonOne.bind("<Button-1>", lambda event, keyPressed='1': self.pressed(event, keyPressed))
        self.buttonTwo.bind("<Button-1>", lambda event, keyPressed='2': self.pressed(event, keyPressed))
        self.buttonThree.bind("<Button-1>", lambda event, keyPressed='3': self.pressed(event, keyPressed))
        self.entry.grid(row=0, column=0, columnspan=3)
        self.buttonOne.grid(row=1, column=0)
        self.buttonTwo.grid(row=1, column=1)
        self.buttonThree.grid(row=1, column=2)
    def pressed(self, event, keyPressed):
        keyboard.press(keyPressed)
        keyboard.release(keyPressed)
root = Tk()
app = App(root)
root.mainloop()

2 Answers2

2

I would suggest using withdraw() and deiconify(). This will make it so the window with the button is invisible once you call it on that window. Once you use deiconify() it reverses this and makes it visible again.

More information can be found here.

  • This solution provides the functionality I am looking for, however you see see the window 'flash'(go away with withdraw() and comeback with deiconify()). I solved this with drawing the window twice in the same location so that the flash is not noticabel but I am wondering if there is a better way to do it without having to create the same window twice. – William Scantlin Jun 12 '18 at 02:49
1

Question: Keep from moving focus to window

On X11, you can set the -type attribute:

self.wm_attributes("-type", 'dock')

'dock' will working for me not to grab the focus, but are not supported by all window managers.


Reference:

  • wm attributes

    Communicate with window manager

  • A list of types

    -type
    Requests that the window should be interpreted by the window manager as being of the specified type(s). This may cause the window to be decorated in a different way or otherwise managed differently, though exactly what happens is entirely up to the window manager.

    'dock'
    indicates a dock/panel feature,


import tkinter as tk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.wm_attributes("-type", 'dock')

        for n in range(64, 68):
            btn = tk.Button(self, text=chr(n), takefocus=0)
            btn.bind("<Button-1>", self.on_click)
            btn.pack()

    def on_click(self, event):
        w = event.widget
        print(w['text'])


if __name__ == '__main__':
    App().mainloop()
stovfl
  • 14,998
  • 7
  • 24
  • 51