Search code examples
pythontkintercustomtkinter

Controlling Customtkinter from a Thread


I want to deactivate and activate CustomTkinter buttons from the thread. But the buttons are deformed (image artifacts appear). Why does this happen and how to solve the problem?

Screen

enter image description here

I expected smooth buttons without deformations

from tkinter import *
import customtkinter
import threading

def function():
    tabview.configure(state='disabled')
    # here is other code.....
    tabview.configure(state='normal')
    root.update

customtkinter.set_appearance_mode("dark")
customtkinter.set_default_color_theme("blue")
root = customtkinter.CTk()
root.geometry("465x235+300+200")
root.resizable(False, False)

tabview = customtkinter.CTkTabview(root, fg_color='#242424')
tabview.pack(fill=BOTH, expand=1)

tab_1 = tabview.add("Tab_____1")
tab_2 = tabview.add("Tab_____2")
tab_3 = tabview.add("Tab_____3")
tab_4 = tabview.add("Tab_____4")

thread_ops_directory_read = threading.Thread(target=function)
thread_ops_directory_read.start()

root.mainloop()

Solution

  • As you already find out that the deformation issue is due to the update is performed in a child thread, so you can make the update in the main thread instead by using .after() inside the threaded function to schedule the update in the main thread.

    Also if function() will be called when starting the program, you can disable the tabview initially instead of inside function():

    ...
    
    def function():
        # here is other code
        ...
        # schedule the task to be executed in the main thread later
        tabview.after(1, lambda: tabview.configure(state='normal'))
    
    ...
    
    tabview = customtkinter.CTkTabview(..., state='disabled') # disable initially
    ...