Python Forum
Is there a way to call and focus any popup window outside of the main window app?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there a way to call and focus any popup window outside of the main window app?
#5
(Sep-30-2023, 01:59 PM)deanhystad Wrote: I looked back at some of your other posts. I think you switched over to using the keyboard module to catch keystrokes. I modified my earlier code to use the same module and to fit better with the code you have posted. While doing this I noticed there is no reason for the popup to capture keystrokes. The KeyListener knows when the popup is visible, and it knows what keys are pressed. It can forward key presses to the popup when visible. That fixes your focus problem.
import tkinter as tk
import string
import keyboard


class Popup(tk.Tk):
    """Popup window for displaying option buttons."""


It worked!!! Thanks so much!!! **dance** 

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.withdraw()
        self.command = None
        self.shurtcuts = {}
        self.title("Popup")
        self.protocol("WM_DELETE_WINDOW", lambda: self.select(None))

    def configure(self, options=None, command=None, **kwargs):
        """Adds options and command to configure."""
        super().configure(**kwargs)
        self.command = command or self.command
        if options:
            for widget in self.winfo_children():
                widget.destroy()
            self.shortcuts = dict(zip(string.ascii_lowercase, options))
            for key, op in self.shortcuts.items():
                button = tk.Button(
                    self, text=f"{key}: {op}", command=lambda x=op: self.select(x)
                )
                button.pack(expand=True, fill=tk.X)

    def open(self):
        """Draw window."""
        self.deiconify()

    def shortcut(self, key):
        """Check if key is shortcut."""
        if key in self.shortcuts:
            self.select(self.shortcuts[key])

    def select(self, option):
        """Button press callback."""
        self.withdraw()
        self.command(option)


class KeyListener:
    """Listens to key presses.  Executes command if key event is mapped."""

    keychars = {"space": " ", "tab": "\t", "enter": "\n"}

    def __init__(self):
        self.word = []
        self.popup = Popup()
        self.commands = {
            "Do this": lambda: print("Doing this"),
            "Do that": lambda: print("Doing that"),
            "Clear": self.word.clear,
            "Quit": self.popup.destroy
        }
        self.popup.configure(
            command=self.popup_callback, options=list(self.commands)
        )
        keyboard.on_press(self.on_press)
        self.popup.mainloop()

    def popup_callback(self, option):
        """Popup window callback."""
        self.commands[option]()

    def on_press(self, key):
        """Called when keyboard key is pressed."""
        key = self.keychars.get(key.name, key.name)
        if self.popup.state() == "normal":
            # Redirect to popup.
            self.popup.shortcut(key)
        elif key == "esc":
            # Open command popup.
            self.popup.open()
        else:
            # Do some input processing thing.
            if key in string.printable:
                self.word.append(key)
                print("".join(self.word))


KeyListener()
Not having to run the keyboard listener in another threads simplifies things. You don't have to create a thread, and you don't have to stop the thread. Other changes are asking the popup window if it is visible instead of using a variable to keep track and using a dictionary to hold the popup commands
Reply


Messages In This Thread
RE: Is there a way to call and focus any popup window outside of the main window app? - by Valjean - Oct-01-2023, 02:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Custom Window Decorations on all Windows Beenman11 3 529 Feb-11-2025, 04:51 PM
Last Post: deanhystad
  Unique Tkinter Window kucingkembar 11 2,058 Aug-11-2024, 01:49 PM
Last Post: deanhystad
  Hide CMD call window tester_V 8 2,501 Apr-16-2024, 08:26 PM
Last Post: deanhystad
  Open files in an existing window instead of new Kostov 2 1,456 Apr-13-2024, 07:22 AM
Last Post: Kostov
  How to Minimize ADB window OomKoos 0 886 Dec-29-2023, 12:41 PM
Last Post: OomKoos
  add entries and labels to the window tkinter jacksfrustration 3 2,257 Oct-10-2023, 06:41 PM
Last Post: buran
  Can't stop keyboard listener to grab chars typed inside CTk window Valjean 9 3,469 Sep-25-2023, 08:07 PM
Last Post: deanhystad
  read active document name - other than from the window title ineuw 0 945 Sep-11-2023, 09:06 AM
Last Post: ineuw
  how to open a popup window in tkinter with entry,label and button lunacy90 1 3,178 Sep-01-2023, 12:07 AM
Last Post: lunacy90
Bug tkinter.TclError: bad window path name "!button" V1ber 2 2,218 Aug-14-2023, 02:46 PM
Last Post: V1ber

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020