0

I'm trying to make an app that will provide a pane of non-standard characters (specifically, the Greek alphabet) that you can click to input in whatever text field you're working in. The issue is when you click a button on the pane, it focuses the tk window, and then doesn't paste the character into the window you were in before. Is there a way to keep the window from being focused? I've seen a lot of stuff about forcing the window to focus, but not the other way around. This is my code so far:

from tkinter import *
import math
import pyautogui


class App(Tk):
    def __init__(self):
        super().__init__()
        self.rows = 4
        self.alphabet = ["Αα", "Ββ", "Γγ", "Δδ", "Εε", "Ζζ", "Ηη", "Θθ", "Ιι", "Κκ", "Λλ", "Μμ", "Νν", "Ξξ", "Οο", "Ππ",
                         "Ρρ", "Σσ", "Ττ", "Υυ", "Φφ", "Χχ", "Ψψ", "Ωω"]
        self.buttons = [Button(text=i[1],
                               command=(lambda: self.paste(i)),
                               font=('Times New Roman', 16, 'bold'),
                               takefocus=0) for i in self.alphabet]

        blen = math.ceil(len(self.buttons) / self.rows)
        for i in range(0, self.rows):
            self.grid_rowconfigure(i, weight=1)
            for j in range(0, blen):
                self.grid_columnconfigure(j, weight=1)
                if i * blen + j < len(self.buttons):
                    self.buttons[i * blen + j].grid(row=i, column=j, ipadx=4, ipady=4, sticky="nesw")

    def paste(self, string): # probably need to do this differently
        self.clipboard_clear()
        self.clipboard_append(string[1])
        pyautogui.hotkey("ctrl", "v")


if __name__ == "__main__":
    app = App()
    app.attributes('-topmost', True)
    app.mainloop()

I had tried adding takefocus=0 to the Button elements, but that's apparently only for keyboard-based navigation.

  • 1
    There is no cross-platform way to do this as the behavior you are after is controlled by the underlying display server (e.g. Xorg-X11) or simply the desktop environment (e.g. Windows) - for X you will need [override redirect flag](https://unix.stackexchange.com/questions/669224/how-to-set-the-override-redirect-flag-on-existing-window) set, and for Windows look into the [`WS_EX_NOACTIVATE`](https://stackoverflow.com/questions/7750517/notification-window-preventing-the-window-from-ever-getting-focus) flag. – metatoaster Apr 12 '23 at 23:25
  • The above techniques are quite difficult to do correctly. For example, in Windows you need to hook the Tk DLL for the ```CreateWindowEx()```. In short, it's nearly impossible to do that with Tkinter. – relent95 Apr 16 '23 at 09:51

0 Answers0