0

I am currently working on a windows application that will consist of a window that will start hidden in the system tray, which is done using pystray library and then the program will keep running in the background and will pop up on top when a shortcut like 'ctrl + alt + a' will be pressed. I was using the keyboard library to do the same but was ending up on errors or either the shortcut wasn`t responding.

Here is the Code...

# Import the required libraries
from tkinter import *
from pystray import MenuItem as item
import pystray
from PIL import Image
import keyboard

class App:
   def __init__(self, root):
      self.root = root
      self.hide_window()
   
   def quit_window(self, icon, item):
      icon.stop()
      root.destroy()

   # Define a function to show the window again
   def show_window(self, icon, item):
      icon.stop()
      root.after(0,root.deiconify())

   # Hide the window and show on the system taskbar
   def hide_window(self):
      root.withdraw()
      image=Image.open("icon.png")
      menu=(item('Quit', self.quit_window), item('Show', self.show_window))
      icon=pystray.Icon("name", image, "My System Tray Icon", menu)
      icon.run()


root = Tk()
root.title("System Tray Application")
root.geometry("700x350")
app = App(root)
root.mainloop()


# # Define a function for quit the window
# keyboard.add_hotkey("ctrl + alt + a", callback=show_window)

How would that window which is minimized in the system tray will pop on top through a shortcut? Any helps?

Thanks.

  • You should call the ```keyboard.add_hotkey()``` before calling the ```root.mainloop()```. Also the callback argument should be the ```app.show_window```. – relent95 Apr 06 '23 at 01:54

0 Answers0