marticliment / win32mica Goto Github PK
View Code? Open in Web Editor NEWWin32mica: a simple module to add the Mica effect on legacy python windows.
Home Page: https://pypi.org/project/win32mica
License: MIT License
Win32mica: a simple module to add the Mica effect on legacy python windows.
Home Page: https://pypi.org/project/win32mica
License: MIT License
Edition Windows 11 Home
Version 23H2
Installed on 11/13/2023
OS build 22631.3007
Experience Windows Feature Experience Pack 1000.22681.1000.0
Python 3.11.7 (tags/v3.11.7:fa7a6f2, Dec 4 2023, 19:24:49) [MSC v.1937 64 bit (AMD64)] on win32
Not sure what problem it is (the window will show like normal only when screenshot tool is used)
Test code:
pip install customtkinter, win32mica
import customtkinter
import os
from win32mica import ApplyMica, MicaTheme, MicaStyle
from ctypes import windll
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
self.hwnd = windll.user32.GetParent(self.winfo_id())
# mode = MicaTheme.DARK # Dark mode mica effect
# mode = MicaTheme.LIGHT # Light mode mica effect
mode = MicaTheme.AUTO # Apply system theme, and change it if system theme changes
style = MicaStyle.DEFAULT # Default backdrop effect
# style = MicaStyle.ALT # Alt backdrop effect
def callbackFunction(NewTheme):
if NewTheme == MicaTheme.DARK:
print("Theme has changed to dark!")
else:
print("Theme has changed to light!")
ApplyMica(HWND=self.hwnd, Theme=mode, Style=style, OnThemeChange=callbackFunction)
self.title("image_example.py")
self.geometry("700x450")
# set grid layout 1x2
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(1, weight=1)
# create navigation frame
self.navigation_frame = customtkinter.CTkFrame(self, corner_radius=0)
self.navigation_frame.grid(row=0, column=0, sticky="nsew")
self.navigation_frame.grid_rowconfigure(4, weight=1)
self.navigation_frame_label = customtkinter.CTkLabel(self.navigation_frame, text=" Image Example", compound="left", font=customtkinter.CTkFont(size=15, weight="bold"))
self.navigation_frame_label.grid(row=0, column=0, padx=20, pady=20)
self.home_button = customtkinter.CTkButton(self.navigation_frame, corner_radius=0, height=40, border_spacing=10, text="Home", fg_color="transparent", text_color=("gray10", "gray90"), hover_color=("gray70", "gray30"), anchor="w", command=self.home_button_event)
self.home_button.grid(row=1, column=0, sticky="ew")
self.frame_2_button = customtkinter.CTkButton(self.navigation_frame, corner_radius=0, height=40, border_spacing=10, text="Frame 2", fg_color="transparent", text_color=("gray10", "gray90"), hover_color=("gray70", "gray30"), anchor="w", command=self.frame_2_button_event)
self.frame_2_button.grid(row=2, column=0, sticky="ew")
self.frame_3_button = customtkinter.CTkButton(self.navigation_frame, corner_radius=0, height=40, border_spacing=10, text="Frame 3", fg_color="transparent", text_color=("gray10", "gray90"), hover_color=("gray70", "gray30"), anchor="w", command=self.frame_3_button_event)
self.frame_3_button.grid(row=3, column=0, sticky="ew")
self.appearance_mode_menu = customtkinter.CTkOptionMenu(self.navigation_frame, values=["Light", "Dark", "System"],
command=self.change_appearance_mode_event)
self.appearance_mode_menu.grid(row=6, column=0, padx=20, pady=20, sticky="s")
# create home frame
self.home_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color="transparent")
self.home_frame.grid_columnconfigure(0, weight=1)
self.home_frame_large_image_label = customtkinter.CTkLabel(self.home_frame, text="")
self.home_frame_large_image_label.grid(row=0, column=0, padx=20, pady=10)
self.home_frame_button_1 = customtkinter.CTkButton(self.home_frame, text="")
self.home_frame_button_1.grid(row=1, column=0, padx=20, pady=10)
self.home_frame_button_2 = customtkinter.CTkButton(self.home_frame, text="CTkButton", compound="right")
self.home_frame_button_2.grid(row=2, column=0, padx=20, pady=10)
self.home_frame_button_3 = customtkinter.CTkButton(self.home_frame, text="CTkButton", compound="top")
self.home_frame_button_3.grid(row=3, column=0, padx=20, pady=10)
self.home_frame_button_4 = customtkinter.CTkButton(self.home_frame, text="CTkButton", anchor="w")
self.home_frame_button_4.grid(row=4, column=0, padx=20, pady=10)
# create second frame
self.second_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color="transparent")
# create third frame
self.third_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color="transparent")
# select default frame
self.select_frame_by_name("home")
def select_frame_by_name(self, name):
# set button color for selected button
self.home_button.configure(fg_color=("gray75", "gray25") if name == "home" else "transparent")
self.frame_2_button.configure(fg_color=("gray75", "gray25") if name == "frame_2" else "transparent")
self.frame_3_button.configure(fg_color=("gray75", "gray25") if name == "frame_3" else "transparent")
# show selected frame
if name == "home":
self.home_frame.grid(row=0, column=1, sticky="nsew")
else:
self.home_frame.grid_forget()
if name == "frame_2":
self.second_frame.grid(row=0, column=1, sticky="nsew")
else:
self.second_frame.grid_forget()
if name == "frame_3":
self.third_frame.grid(row=0, column=1, sticky="nsew")
else:
self.third_frame.grid_forget()
def home_button_event(self):
self.select_frame_by_name("home")
def frame_2_button_event(self):
self.select_frame_by_name("frame_2")
def frame_3_button_event(self):
self.select_frame_by_name("frame_3")
def change_appearance_mode_event(self, new_appearance_mode):
customtkinter.set_appearance_mode(new_appearance_mode)
if __name__ == "__main__":
app = App()
app.mainloop()
Hi! Reallay awsome project!
I get this error when running on tkinter window:
Code:
ApplyMica(HWND=windll.user32.GetParent(app.winfo_id()), ColorMode=True)
Error:
File "D:\Coding\TimerX\main.py", line 783, in makeWindowsBlur
ApplyMica(HWND=windll.user32.GetParent(app.winfo_id()), ColorMode=True)
File "C:\Users\suchi\AppData\Local\Programs\Python\Python310\lib\site-packages\win32mica\__init__.py", line 54, in ApplyMica
SetWindowCompositionAttribute(HWND, Wca)
ctypes.ArgumentError: argument 2: <class 'TypeError'>: expected WINDOWCOMPOSITIONATTRIBDATA instance instead of WindowCompositionAttribute
Hello! I am using win32mica in a program, and when running the program from the .py file, everything works flawlessly. But when i compile the program to an execuetable using cx_Freeze, win32mica throws the following error:
Win32Mica Error: win32 version 9200 is not supported
What does that mean and how do i fix it?
How I can control blur size with tk? I wanna make animation how it blurred from 0 to 1 (for example)
Not sure if I did something wrong
My code:
def Light():
app.tk.call("ttk::style", "theme", "use") == "sun-valley-dark"
app.tk.call("set_theme", "light")
bg_color = ttk.Style().lookup(".", "background")
app.wm_attributes("-transparent", bg_color)
HWND=windll.user32.GetParent(app.winfo_id())
ApplyMica(HWND, ColorMode=MICAMODE.LIGHT)
app.update()
def Dark():
app.tk.call("ttk::style", "theme", "use") == "sun-valley-dark"
app.tk.call("set_theme", "dark")
bg_color = ttk.Style().lookup(".", "background")
app.wm_attributes("-transparent", bg_color)
HWND=windll.user32.GetParent(app.winfo_id())
ApplyMica(HWND, ColorMode=MICAMODE.DARK)
app.update()
Result:
By clicking on the part where mica is used, the program minimizes. If the topmost attribute is applied, the window loses focus, and the mica effect is no longer visible.
is there a way to stop the background color on the label of the window, i kinda resorted to just leaving the title bar empty because it was annoying.
error while import win32mica
Win32mica: Failed to DwmExtendFrameIntoClientArea
os: windows 11 22000.588
python: 3.10
DeprecationWarning: 'exec_' will be removed in the future. Use 'exec' instead.
root.exec_()
How can I unblur a PySide6 window?
My code is:
import tkinter
from tkinter import ttk
import sv_ttk
root = tkinter.Tk()
root.geometry('400x100+200+200') # set new geometry
from win32mica import MICAMODE, ApplyMica
import darkdetect # You can pass the darkdetect return value directly, since the ColorMode accepts bool values (True -> dark, False -> light)
mode = darkdetect.isDark()
hwnd = root.frame() # On a tkinter window
ApplyMica(hwnd, mode)
button = ttk.Button(root, text="Click me!")
button.pack()
# This is where the magic happens
if darkdetect.isDark():
sv_ttk.set_theme("dark")
else:
sv_ttk.set_theme("light")
root.mainloop()
Applying Win32Mica messed it up.
Is it a bug in Win32Mica, or a bug in my code?
EDIT: Here is the backlog appearing when running the script:
Traceback (most recent call last):
File "C:\Users\tyler\fluent-tkinter.py", line 12, in <module>
ApplyMica(hwnd, mode)
File "C:\Users\tyler\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\win32mica\__init__.py", line 18, in ApplyMica
HWND = int(HWND)
ValueError: invalid literal for int() with base 10: '0xa20856'
Tips:I'm using translation software
I will submit my code as an attachment. It contains the simplest way to achieve both effects and was tested on my computer. Unfortunately, it still has a lot of bugs, but I don't have time to fix it. So I'm giving it to you, hoping to provide you with some reference and inspiration.
TEST7.zip
This is the simplest way to call code:
TEST7.WindowEffect().setAeroEffect(HWND)
It is important to note that these effects are not directly achieved by a single line of code. I also adjusted the color of the controls. In the ground glass effect, the closer the color is to black, the higher the transparency.
At the same time, you can also see that some controls are still white. Also, the buttons in the title bar have become transparent.
It is puzzling that this effect has a huge lag in the Windows 10 environment, but not in the Windows 11 environment
I made a tkinter application which uses this beautiful mica style, and everything worked. The mica applies fine, there are no errors related to HWND, but there is this one issue. When I restore a minimised window, there are no hitboxes for any buttons on the titlebar. This isssue is related to #7 where OP was not able to focus his window. The same thing happens here, but the only clickable buttons are the ones I made by myself in python, the close, maximise and minimise buttons don't really work
From #7 , I found out that this issue was a tkinter one, is it the case here too?
From this gif, I was trying to click the minimise button again, but the click is registered on the desktop instead of the app itself
while using this i noticed that when you want to change the mica effect whilst the app was open, you could only change from light to dark, but not back.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.