一个与PySimpleGUI tkinter端口配合工作的系统托盘图标。使用pystray提供系统托盘。在Windows下运行良好。
项目描述
psgtray
一个PySimpleGUI应用程序
添加使用PySimpleGUI tkinter端口在系统托盘运行程序的能力
功能
- 添加系统托盘图标
- 添加在系统托盘显示消息的能力
- 从系统托盘图标显示和获取菜单项
- 提供通常仅在PySimpleGUI的Qt和Wx端口中可用的功能
- 与pystray Python包集成
安装
使用PyPI上的PIP
PySimpleGUI产品的最新官方版本可在PyPI上找到。要从PyPI安装演示应用程序,请使用以下命令
如果您使用计算机上的python
命令来调用Python(Windows)
python -m pip install --upgrade psgtray
如果您使用计算机上的python3
命令来调用Python(Linux,Mac)
python3 -m pip install --upgrade psgtray
使用GitHub上的PIP
您还可以从PySimpleGUI GitHub账户中pip安装PySimpleGUI应用程序。GitHub版本包含已修复的bug和尚未发布到PyPI的新程序/功能。要从该仓库直接pip安装
如果您使用计算机上的python
命令来调用Python(Windows)
python -m pip install --upgrade https://github.com/PySimpleGUI/psgtray/zipball/main
如果您使用计算机上的python3
命令来调用Python(Linux,Mac)
python3 -m pip install --upgrade https://github.com/PySimpleGUI/psgtray/zipball/main
添加到您的PySimpleGUI程序
这是可以在PySimpleGUI项目演示程序文件夹中找到的演示程序的副本。
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=sg.DEFAULT_BASE64_ICON)
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()
限制
Windows实现运行良好。Linux GTK版本则不尽然。
在初始创建后更新菜单尚不支持。
需求
为了使用此软件包,您还需要以下软件包
- PySimpleGUI
- pystray(许可协议为LGPL3)
目前仅支持pystray的版本<= 0.18.0
许可 & 版权
版权所有 2023-2024 PySimpleSoft,Inc. 及/或其许可人。
这是一个免费使用的“实用工具”,并许可在PySimpleGUI许可协议下,该协议的副本包含在license.txt文件中,也可在https://pysimplegui.com/eula上找到。
有关此实用工具的使用,请参阅许可协议的第1.2节,有关任何问题,请参阅https://pysimplegui.com/faq。
贡献
我们很高兴接受描述错误报告和功能请求的问题!如果您的错误报告与安全漏洞有关,请不要提交公开问题,而是请通过issues@PySimpleGUI.com与我们联系。
我们不接受(也不希望收到)用户创建的或第三方代码的贡献,包括补丁、拉取请求或包含在提交问题中的代码片段。请不要向我们发送此类代码!错误报告和功能请求不应包含任何源代码。
如果您仍然向我们提交任何用户创建的或第三方代码,(1) 您将把与代码相关的所有权利和所有权转让给我们;并且(2) 在任何此类转让不充分有效的情况下,您特此授予我们一项免费的、永久的、不可撤销的、全球性的、无限期的、可转让的、可再许可的许可证,根据其中体现或与之相关的所有知识产权,以我们选择的方式利用该代码,包括将其合并到PySimpleGUI中,并以我们自行决定的任何条款重新分发。
项目详情
下载文件
下载适用于您的平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。