marticliment / win32mica

Win32mica: a simple module to add the Mica effect on legacy python windows.
https://pypi.org/project/win32mica
MIT License
95 stars 5 forks source link

Something wrong on tkinter window (Win11 23H2) #20

Open AI-M-BOT opened 9 months ago

AI-M-BOT commented 9 months ago
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

Should be like: image

But actually: image

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()
marticliment commented 9 months ago

To be honest, this looks as a tkinter bug/corruption in your system. Take note that tkinter has limited compatibility with changing window visual attributes, so I'd recommend you to switch to another, more mature, UI toolkit such as Qt (PySide/PyQt).

Regarding what the cause of the issue is, I have no idea

JiaPai12138 commented 5 months ago

To be honest, this looks as a tkinter bug/corruption in your system. Take note that tkinter has limited compatibility with changing window visual attributes, so I'd recommend you to switch to another, more mature, UI toolkit such as Qt (PySide/PyQt).

Regarding what the cause of the issue is, I have no idea

however it results the same in normal cmd window as well image all text will disappear when i am not taking screenshot

marticliment commented 5 months ago

In my machine the code provided works as expected, and the interface renders successfully. Could it be that there is an issue on your side, such as a corrupted system component or display driver?

JiaPai12138 commented 5 months ago

In my machine the code provided works as expected, and the interface renders successfully. Could it be that there is an issue on your side, such as a corrupted system component or display driver?

I double checked all environment details and nothing goes wrong even after sfc and dism command this situation still happens image