我如何制作带有 system-tray/taskbar 图标的跨平台 Kivy 应用程序,即使应用程序不是 focused/in 任务栏模式,它也会检测键盘输入
How can I make cross-platform Kivy app with system-tray/taskbar icon which would detect keyboard input even when app is not focused/in taskbar mode
我想创建带有 taskbar/system-tray 图标的应用程序。跨平台。 OSX、Windows、Linux(Ubuntu/Centos/Mint/Manjaro 和其他流行的发行版)。
此应用应检测键盘输入并对其作出反应。基本上对特定键执行一些操作。示例:用户想要播放一些音乐,按 shift+ctrl+p。音乐开始播放。
我知道Kivy可以检测
对于“跨平台系统托盘”这个东西——我认为如果没有像 Elektron 之类的真正的跨平台框架,这是完全不可能的,它可以提供使用 system-tray/taskbar 功能的能力.
即使在 window 未聚焦时也能检测鼠标 - 将 pynput 与 asyncio 结合使用(kivy + asyncio 在其官方存储库中的示例)。基本上:即使应用程序没有使用 asyncio 专注于后台工作代码,您也可以检测到键盘事件。然后它会检测每个击键。
import PySimpleGUI as sg
from psgtray import SystemTray
"""
A System Tray Icon courtesy of pystray and your friends at PySimpleGUI
Import the SystemTray object with this line of code:
from psgtray import SystemTray
Key for the system tray icon is:
tray = SystemTray()
tray.key
values[key] contains the menu item chosen.
One trick employed here is to change the window's event to be the event from the System Tray.
Copyright PySimpleGUI 2021
"""
def main():
menu = ['',
['Show Window', 'Hide Window', '---', '!Disabled Item', 'Change Icon', ['Happy', 'Sad', 'Plain'], 'Exit']]
tooltip = 'Tooltip'
layout = [[sg.Text('My PySimpleGUI Celebration Window - X will minimize to tray')],
[sg.T('Double clip icon to restore or right click and choose Show Window')],
[sg.T('Icon Tooltip:'), sg.Input(tooltip, key='-IN-', s=(20, 1)), sg.B('Change Tooltip')],
[sg.Multiline(size=(60, 10), reroute_stdout=False, reroute_cprint=True, write_only=True, key='-OUT-')],
[sg.Button('Go'), sg.B('Hide Icon'), sg.B('Show Icon'), sg.B('Hide Window'), sg.Button('Exit')]]
window = sg.Window('Window Title', layout, finalize=True, enable_close_attempted_event=True)
tray = SystemTray(menu, single_click_events=False, window=window, tooltip=tooltip, icon='cogent_logo.png')
tray.show_message('System Tray', 'System Tray Icon Started!')
sg.cprint(sg.get_versions())
while True:
event, values = window.read()
# IMPORTANT step. It's not required, but convenient. Set event to value from tray
# if it's a tray event, change the event variable to be whatever the tray sent
if event == tray.key:
sg.cprint(f'System Tray Event = ', values[event], c='white on red')
event = values[event] # use the System Tray's event as if was from the window
if event in (sg.WIN_CLOSED, 'Exit'):
break
sg.cprint(event, values)
tray.show_message(title=event, message=values)
if event in ('Show Window', sg.EVENT_SYSTEM_TRAY_ICON_DOUBLE_CLICKED):
window.un_hide()
window.bring_to_front()
elif event in ('Hide Window', sg.WIN_CLOSE_ATTEMPTED_EVENT):
window.hide()
tray.show_icon() # if hiding window, better make sure the icon is visible
# tray.notify('System Tray Item Chosen', f'You chose {event}')
elif event == 'Happy':
tray.change_icon(sg.EMOJI_BASE64_HAPPY_JOY)
elif event == 'Sad':
tray.change_icon(sg.EMOJI_BASE64_FRUSTRATED)
elif event == 'Plain':
tray.change_icon(sg.DEFAULT_BASE64_ICON)
elif event == 'Hide Icon':
tray.hide_icon()
elif event == 'Show Icon':
tray.show_icon()
elif event == 'Change Tooltip':
tray.set_tooltip(values['-IN-'])
tray.close() # optional but without a close, the icon may "linger" until moused over
window.close()
if __name__ == '__main__':
main()
如果您愿意离开 Kivy 并使用其他框架,当 运行 tkinter 版本。 PySimpleGUIQt 端口具有更“官方”的系统托盘功能。
GitHub Repo PySimpleHotkey 是如何使用 psgtray 包制作热键程序的一个示例。
我想创建带有 taskbar/system-tray 图标的应用程序。跨平台。 OSX、Windows、Linux(Ubuntu/Centos/Mint/Manjaro 和其他流行的发行版)。
此应用应检测键盘输入并对其作出反应。基本上对特定键执行一些操作。示例:用户想要播放一些音乐,按 shift+ctrl+p。音乐开始播放。
我知道Kivy可以检测
对于“跨平台系统托盘”这个东西——我认为如果没有像 Elektron 之类的真正的跨平台框架,这是完全不可能的,它可以提供使用 system-tray/taskbar 功能的能力.
即使在 window 未聚焦时也能检测鼠标 - 将 pynput 与 asyncio 结合使用(kivy + asyncio 在其官方存储库中的示例)。基本上:即使应用程序没有使用 asyncio 专注于后台工作代码,您也可以检测到键盘事件。然后它会检测每个击键。
import PySimpleGUI as sg
from psgtray import SystemTray
"""
A System Tray Icon courtesy of pystray and your friends at PySimpleGUI
Import the SystemTray object with this line of code:
from psgtray import SystemTray
Key for the system tray icon is:
tray = SystemTray()
tray.key
values[key] contains the menu item chosen.
One trick employed here is to change the window's event to be the event from the System Tray.
Copyright PySimpleGUI 2021
"""
def main():
menu = ['',
['Show Window', 'Hide Window', '---', '!Disabled Item', 'Change Icon', ['Happy', 'Sad', 'Plain'], 'Exit']]
tooltip = 'Tooltip'
layout = [[sg.Text('My PySimpleGUI Celebration Window - X will minimize to tray')],
[sg.T('Double clip icon to restore or right click and choose Show Window')],
[sg.T('Icon Tooltip:'), sg.Input(tooltip, key='-IN-', s=(20, 1)), sg.B('Change Tooltip')],
[sg.Multiline(size=(60, 10), reroute_stdout=False, reroute_cprint=True, write_only=True, key='-OUT-')],
[sg.Button('Go'), sg.B('Hide Icon'), sg.B('Show Icon'), sg.B('Hide Window'), sg.Button('Exit')]]
window = sg.Window('Window Title', layout, finalize=True, enable_close_attempted_event=True)
tray = SystemTray(menu, single_click_events=False, window=window, tooltip=tooltip, icon='cogent_logo.png')
tray.show_message('System Tray', 'System Tray Icon Started!')
sg.cprint(sg.get_versions())
while True:
event, values = window.read()
# IMPORTANT step. It's not required, but convenient. Set event to value from tray
# if it's a tray event, change the event variable to be whatever the tray sent
if event == tray.key:
sg.cprint(f'System Tray Event = ', values[event], c='white on red')
event = values[event] # use the System Tray's event as if was from the window
if event in (sg.WIN_CLOSED, 'Exit'):
break
sg.cprint(event, values)
tray.show_message(title=event, message=values)
if event in ('Show Window', sg.EVENT_SYSTEM_TRAY_ICON_DOUBLE_CLICKED):
window.un_hide()
window.bring_to_front()
elif event in ('Hide Window', sg.WIN_CLOSE_ATTEMPTED_EVENT):
window.hide()
tray.show_icon() # if hiding window, better make sure the icon is visible
# tray.notify('System Tray Item Chosen', f'You chose {event}')
elif event == 'Happy':
tray.change_icon(sg.EMOJI_BASE64_HAPPY_JOY)
elif event == 'Sad':
tray.change_icon(sg.EMOJI_BASE64_FRUSTRATED)
elif event == 'Plain':
tray.change_icon(sg.DEFAULT_BASE64_ICON)
elif event == 'Hide Icon':
tray.hide_icon()
elif event == 'Show Icon':
tray.show_icon()
elif event == 'Change Tooltip':
tray.set_tooltip(values['-IN-'])
tray.close() # optional but without a close, the icon may "linger" until moused over
window.close()
if __name__ == '__main__':
main()
如果您愿意离开 Kivy 并使用其他框架,当 运行 tkinter 版本。 PySimpleGUIQt 端口具有更“官方”的系统托盘功能。
GitHub Repo PySimpleHotkey 是如何使用 psgtray 包制作热键程序的一个示例。