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.