Create an icon in memory with win32 in python

What's a good way to generate an icon in-memory in python? Right now I'm forced to use pygame to draw the icon, then I save it to disk as an .ico file, and then I load it from disk as an ICO resource...

Something like this:

    if os.path.isfile(self.icon):
        icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
        hicon = win32gui.LoadImage(hinst,

...where self.icon is the filename of the icon I created.

Is there any way to do this in memory? EDIT: All I want to do is create an icon with a 2-digit number displayed on it (weather-taskbar style.

Asked by: Stella528 | Posted: 24-09-2021

Answer 1

You can use wxPython for this.

from wx import EmptyIcon
icon = EmptyIcon()

The wxBitmap can be generated in memory using wxMemoryDC, look here for operations you can do on a DC.

This icon can then be applied to a wxFrame (a window) or a wxTaskBarIcon using:


Answered by: Grace758 | Posted: 25-10-2021

Answer 2

You can probably create a object that mimics the python file-object interface.

Answered by: Ada954 | Posted: 25-10-2021

Answer 3

This is working for me and doesn't require wx.

from ctypes import *
from ctypes.wintypes import *

CreateIconFromResourceEx = windll.user32.CreateIconFromResourceEx
size_x, size_y = 32, 32

with open("my32x32.png", "rb") as f:
    png =
hicon = CreateIconFromResourceEx(png, len(png), 1, 0x30000, size_x, size_y, LR_DEFAULTCOLOR)

Answered by: Robert410 | Posted: 25-10-2021

Similar questions

Create IPv6 socket in Windows XP, using python

The following piece of code works on Windows7 and Linux, but gives an Error in Windows XP: import socket print(socket.has_ipv6) print("INET=",socket.AF_INET,"\nINET6=",socket.AF_INET6,"\nDGRAM=",socket.SOCK_DGRAM) s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s6 = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) #Error in XP This is the output in Windows XP: Py...

vba - How to create Excel or Windows Msg Box using COM in Python?

Is there a way to get pywin32 to generate a VBA-style MsgBox in Excel, if not, perhaps the Windows equivalent? I tried: from win32com.client import Dispatch excel = Dispatch('Excel.Application') excel.MsgBox('foo') but it gives the error below: AttributeError: Excel.Application.MsgBox

macos - How to create OS X app with Python on Windows

I need to automate a cross-platform application build. Entire build runs on Windows machine. Part of it is written in Python and compiles for OS X. Currently this part of build is done manually on OS X. I tried pyinstaller but it looks like it only building for the platform that it is running on. I also tried py2app but it did not install on Windows. Are there any tools to compile Python script to OS X app...

Create python 3.5 exe on Windows

I'm helping my 10yo go through the book "Python for Kids" by Jason Briggs, on Windows 7. He already knows Scratch. He's making great progress in the book, but I'm surprised that it has no information about creating an exe; it assumes Python will be installed on any computer the kids' programs run on. That isn't going to work when the kid wants to give his programs to his friends. Helping him with this book is my own first ...

Create a Mac single file app on Windows using python

PyInstaller is not a cross-compiler, so to make a Mac OS X app it should be compiled on Mac OS X. However, I don't have access to device running Mac OS X. Is it an option to create an virtual machine on python to emulate Mac and than ran py2app? (I have never done anything like that and don't have much idea on where to start.)

windows - How to create UI in Python 3

I need to know if it's possible to create a UI for my Python application. I created a very simple programm that you can see here and i would like to create a UI, so the user can use it like a "real program", not only in CMD/Python Shell. And another thing: to the user be able to use the program without downloadi...

How to create Python event that should invoke on before windows sleep mode

I need Python event that should log present time into a file when windows receive sleep request. Below is my main code, import subprocess # Here code required to log the time at which system going to sleep command = '' # Some usefull stuff if, shell=True): print 'Success' else: print 'Fail' Below is '' code, import os os.syst...

How to create Windows event log with Python

I have an issue with writing event log to Windows Event Viewer. I want to register a custom Event Log under Applications and Services Logs menu but I have no idea to do it in Python (I am using PyWin32 in this case). Basically we can register a custom log with Powershell's New-EventLog but I want to do it in Python. These are some attemps I have done: Create an event log with P...

how to create video player in pyqt5 for windows to play mp4 using python code?

I tried to create video player for mp4 in windows 10 os. it is always showing error. i tried multiple method. but always failing I tried code from this location QMediaPlayer error:

windows - Create direct file object in python

I have images in 100 folders and the search results are slow, so I want to access those images, so maybe I wanna do it with python(if it is faster), in the way that when we select all the files, and drag and drop them in windows. then I realized that drag and drop in windows uses Component Object Model this s...

windows xp - Process size in XP from Python

I have a python script that can approach the 2 GB process limit under Windows XP. On a machine with 2 GB physical memory, that can pretty much lock up the machine, even if the Python script is running at below normal priority. Is there a way in Python to find out my own process size? Thanks, Gerry

How to find out if there is data to be read from stdin on Windows in Python?

This code[sys.stdin], [], [], 1.0) does exactly what I want on Linux, but not in Windows. I've used kbhit() in msvcrt before to see if data is available on stdin for reading, but in this case it always returns 0. Additionally msvcrt.getch() returns '\xff' whereas returns ...

How can I read system information in Python on Windows?

Following from this OS-agnostic question, specifically this response, similar to data available from the likes of /proc/meminfo on Linux, how can I read system information from Windows using Python (including, but...

In the windows python console, how to make Tab = four spaces?

Hello I would like that when I am in the python console tabbing will give me four spaces. Any ideas?

windows xp - Cygwin and Python 2.6

New to python (and programming). What exactly do I need from Cygwin? I'm running python 2.6 on winxp. Can I safely download the complete Cygwin? It just seems like a huge bundle of stuff. Well, I keep running into modules and functionality (i.e. piping output) which suggest downloading various cygwin components. Will cygwin change or modify any other os functionality or have any other side effects?

windows - Using DPAPI with Python?

Is there a way to use the DPAPI (Data Protection Application Programming Interface) on Windows XP with Python? I would prefer to use an existing module if there is one that can do it. Unfortunately I haven't been able to find a way with Google or Stack Overflow. EDIT: I've taken the example code pointed to by "dF" and tweaked it into a standalone library which can be simply used at a high ...

windows - How do I create a webpage with buttons that invoke various Python scripts on the system serving the webpage?

I'm a hobbyist (and fairly new) programmer who has written several useful (to me) scripts in python to handle various system automation tasks that involve copying, renaming, and downloading files amongst other sundry activities. I'd like to create a web page served from one of my systems that would merely present a few buttons which would allow me to initiate these scripts remotely. The problem is that I do...

How do I create a windows service with Python

Python script - SCP on windows

How is it possible to do secure copy using python (windows native install - ActivePython). Unfortunately pexpect module is for unix only and we don't want cygwin locally. I wrote a script that based on pscp.exe win tool - but always stops at first execution becuse of fingerprint host id. and haven't found option to switch this off. the remote hosts are running ssh-server on cygwin (win 2003 servers). Thanks

windows - Example of how to use msilib to create a .msi file from a python module

Can anyone give me an example of how to use python's msilib standard library module to create a msi file from a custom python module? For example, let's say I have a custom module called with the following code class Cool(object): def print_cool(self): print "cool" and I wa...

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python