python 系统托盘_python3 tkinter写出来的程序最小化到系统托盘
其实就是windowsApi的一层封装。
#!/usr/bin/env python
# Module : SysTrayIcon.py
# Synopsis : Windows System tray icon.
# Programmer : Simon Brunning - simon@brunningonline.net
# Date : 11 April 2005
# Notes : Based on (i.e. ripped off from) Mark Hammond's
# win32gui_taskbar.py and win32gui_menu.py demos from PyWin32
'''TODO
For now, the demo at the bottom shows how to use it...'''
import os
import sys
import win32api
import win32con
import win32gui_struct
try:
import winxpgui as win32gui
except ImportError:
import win32gui
class SysTrayIcon(object):
'''TODO'''
QUIT = 'QUIT'
SPECIAL_ACTIONS = [QUIT]
FIRST_ID = 1023
def __init__(self,
icon,
hover_text,
menu_options,
on_quit=None,
default_menu_index=None,
window_class_name=None,):
self.icon = icon
self.hover_text = hover_text
self.on_quit = on_quit
menu_options = menu_options + (('Quit', None, self.QUIT),)
self._next_action_id = self.FIRST_ID
self.menu_actions_by_id = set()
self.menu_options = self._add_ids_to_menu_options(list(menu_options))
self.menu_actions_by_id = dict(self.menu_actions_by_id)
del self._next_action_id
self.default_menu_index = (default_menu_index or 0)
self.window_class_name = window_class_name or "SysTrayIconPy"
message_map = {win32gui.RegisterWindowMessage("TaskbarCreated"): self.restart,
win32con.WM_DESTROY: self.destroy,
win32con.WM_COMMAND: self.command,
win32con.WM_USER+20 : self.notify,}
# Register the Window class.
window_class = win32gui.WNDCLASS()
hinst = window_class.hInstance = win32gui.GetModuleHandle(None)
window_class.lpszClassName = self.window_class_name
window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW;
window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
window_class.hbrBackground = win32con.COLOR_WINDOW
window_class.lpfnWndProc = message_map # could also specify a wndproc.
classAtom = win32gui.RegisterClass(window_class)
# Create the Window.
style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
self.hwnd = win32gui.CreateWindow(classAtom,
self.window_class_name,
style,
0,
0,
win32con.CW_USEDEFAULT,
win32con.CW_USEDEFAULT,
0,
0,
hinst,
None)
win32gui.UpdateWindow(self.hwnd)
self.notify_id = None
self.refresh_icon()
win32gui.PumpMessages()
def _add_ids_to_menu_options(self, menu_options):
result = []
for menu_option in menu_options:
option_text, option_icon, option_action = menu_option
if callable(option_action) or option_action in self.SPECIAL_ACTIONS:
self.menu_actions_by_id.add((self._next_action_id, option_action))
result.append(menu_option + (self._next_action_id,))
elif non_string_iterable(option_action):
result.append((option_text,
option_icon,
self._add_ids_to_menu_options(option_action),
self._next_action_id))
else:
print 'Unknown item', option_text, option_icon, option_action
self._next_action_id += 1
return result
def refresh_icon(self):
# Try and find a custom icon
hinst = win32gui.GetModuleHandle(None)
if os.path.isfile(self.icon):
icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
hicon = win32gui.LoadImage(hinst,
self.icon,
win32con.IMAGE_ICON,
0,
0,
icon_flags)
else:
print "Can't find icon file - using default."
hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
if self.notify_id: message = win32gui.NIM_MODIFY
else: message = win32gui.NIM_ADD
self.notify_id = (self.hwnd,
0,
win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP,
win32con.WM_USER+20,
hicon,
self.hover_text)
win32gui.Shell_NotifyIcon(message, self.notify_id)
def restart(self, hwnd, msg, wparam, lparam):
self.refresh_icon()
def destroy(self, hwnd, msg, wparam, lparam):
if self.on_quit: self.on_quit(self)
nid = (self.hwnd, 0)
win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid)
win32gui.PostQuitMessage(0) # Terminate the app.
def notify(self, hwnd, msg, wparam, lparam):
if lparam==win32con.WM_LBUTTONDBLCLK:
self.execute_menu_option(self.default_menu_index + self.FIRST_ID)
elif lparam==win32con.WM_RBUTTONUP:
self.show_menu()
elif lparam==win32con.WM_LBUTTONUP:
pass
return True
def show_menu(self):
menu = win32gui.CreatePopupMenu()
self.create_menu(menu, self.menu_options)
#win32gui.SetMenuDefaultItem(menu, 1000, 0)
pos = win32gui.GetCursorPos()
# See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/menus_0hdi.asp
win32gui.SetForegroundWindow(self.hwnd)
win32gui.TrackPopupMenu(menu,
win32con.TPM_LEFTALIGN,
pos[0],
pos[1],
0,
self.hwnd,
None)
win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0)
def create_menu(self, menu, menu_options):
for option_text, option_icon, option_action, option_id in menu_options[::-1]:
if option_icon:
option_icon = self.prep_menu_icon(option_icon)
if option_id in self.menu_actions_by_id:
item, extras = win32gui_struct.PackMENUITEMINFO(text=option_text,
hbmpItem=option_icon,
wID=option_id)
win32gui.InsertMenuItem(menu, 0, 1, item)
else:
submenu = win32gui.CreatePopupMenu()
self.create_menu(submenu, option_action)
item, extras = win32gui_struct.PackMENUITEMINFO(text=option_text,
hbmpItem=option_icon,
hSubMenu=submenu)
win32gui.InsertMenuItem(menu, 0, 1, item)
def prep_menu_icon(self, icon):
# First load the icon.
ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON)
ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON)
hicon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, ico_x, ico_y, win32con.LR_LOADFROMFILE)
hdcBitmap = win32gui.CreateCompatibleDC(0)
hdcScreen = win32gui.GetDC(0)
hbm = win32gui.CreateCompatibleBitmap(hdcScreen, ico_x, ico_y)
hbmOld = win32gui.SelectObject(hdcBitmap, hbm)
# Fill the background.
brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU)
win32gui.FillRect(hdcBitmap, (0, 0, 16, 16), brush)
# unclear if brush needs to be feed. Best clue I can find is:
# "GetSysColorBrush returns a cached brush instead of allocating a new
# one." - implies no DeleteObject
# draw the icon
win32gui.DrawIconEx(hdcBitmap, 0, 0, hicon, ico_x, ico_y, 0, 0, win32con.DI_NORMAL)
win32gui.SelectObject(hdcBitmap, hbmOld)
win32gui.DeleteDC(hdcBitmap)
return hbm
def command(self, hwnd, msg, wparam, lparam):
id = win32gui.LOWORD(wparam)
self.execute_menu_option(id)
def execute_menu_option(self, id):
menu_action = self.menu_actions_by_id[id]
if menu_action == self.QUIT:
win32gui.DestroyWindow(self.hwnd)
else:
menu_action(self)
def non_string_iterable(obj):
try:
iter(obj)
except TypeError:
return False
else:
return not isinstance(obj, basestring)
# Minimal self test. You'll need a bunch of ICO files in the current working
# directory in order for this to work...
if __name__ == '__main__':
import itertools, glob
icons = itertools.cycle(glob.glob('*.ico'))
hover_text = "SysTrayIcon.py Demo"
def hello(sysTrayIcon): print "Hello World."
def simon(sysTrayIcon): print "Hello Simon."
def switch_icon(sysTrayIcon):
sysTrayIcon.icon = icons.next()
sysTrayIcon.refresh_icon()
menu_options = (('Say Hello', icons.next(), hello),
('Switch Icon', None, switch_icon),
('A sub-menu', icons.next(), (('Say Hello to Simon', icons.next(), simon),
('Switch Icon', icons.next(), switch_icon),
))
)
def bye(sysTrayIcon): print 'Bye, then.'
SysTrayIcon(icons.next(), hover_text, menu_options, on_quit=bye, default_menu_index=1)
python 系统托盘_python3 tkinter写出来的程序最小化到系统托盘相关推荐
- C#实现winform软件开机自动启动并最小化到系统托盘
一.开机自动启动: 拖一个CheckBox 1.软件启动时给CheckBox重置状态: RegistryKey R_local = Registry.LocalMachine; ...
- VC++:如何将程序最小化到托盘
转自:http://qzone.qq.com/blog/412405871-1211101395 一.托盘简介 所谓的"托盘",在Windows系统界面中,指的就是下面任务条右侧, ...
- VC实现将程序最小化到托盘
一.托盘简介 所谓的"托盘",在Windows系统 界面中,指的就是下面任务条右侧,有系统时间等等的标志的那一部分.在程序最小化或挂起时,但有不希望 ...
- SysTrayIcon 改的 python tkinter 最小化至系统托盘
网上的SysTrayIcon改的,Tk页面最小化至托盘,托盘图标左键单击恢复Tk界面 1.点击最小化隐藏至托盘 2.托盘图标右键菜单展示,左键返回Tk界面. 托盘图标可以自定义,修改了SysTrayI ...
- MFC最小化到系统托盘
在VC++中,想实现将MFC最小化到系统托盘,需要调用NOTIFYICONDATA类,并注册相应的消息,以下详细讲解如何实现: 第一步,声明一个NOTIFYICONDATA类,也就是NOTIFYICO ...
- 将 VMware 最小化到系统托盘
1, 下载 Trayconizer 官网地址: http://www.whitsoftdev.com/trayconizer/ 下载地址: http://www.whitsoftdev.com/fil ...
- Delphi 7下最小化到系统托盘
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 在Del ...
- MFC中将窗口最小化到系统托盘和还原
通过以下步骤可以在MFC程序中将窗口最小化到系统托盘,和还原成窗口,附加代码中如有特殊说明则会用红色标出. 1.添加一个ICON图标,其ID为IDI_ICON_TESTICON,在VS2008坐标的R ...
- VC 将程序最小化到托盘
这篇文章内容比较基础,最近看到觉得有用,顺便翻译一下 有空可以写一个自己的TrayIcon类,化简这些原始的操作. Introduction 这篇文章解析了 Shell_NotifyIcon 这个函数 ...
最新文章
- VIM编辑器使用技巧
- 用开源组件构建属于你的 PHP 框架
- 【数据竞赛】数据竞赛中最贵的四个特征
- SAE 助力「海底小纵队学英语」全面拥抱 Serverless,节省 25% 以上成本
- 爬取豆瓣TOP250并将数据保存为txt文件和csv文件并存入MySQL数据库
- VMware 创建开启虚拟机时候报错的解决方式
- php通知多有人,PHP通知抑制;只有某些情况/方法
- 英语数字的 android,英语数字听力学霸APP
- 【xargs使用】查询包含某字符串的所有文件
- HDU-简易版之最短距离(最短路)
- mysql使用文件排序_Mysql排序FileSort的问题
- win11任务栏怎么隐藏 Windows11隐藏任务栏的设置方法
- MAC下的环境变量配置
- 动态规划相关知识点总结
- 2013年最新黑马程序员全套视频-.net视频40G免费下
- mysql的语句大全_mysql语句大全
- FFmpeg —— Linux下使用ffmpeg硬件cuda解码mp4,并加入简单cv处理,sdl渲染窗口(附源码)
- 关于做PDF的FAQ(一)~(四)
- 图普科技李麟|当新零售遇上人工智能
- JetBrain 系列软件快捷键集合