CustomTkinter/Readme.md
Tom Schimansky dfb50b8c95 readme wiki
2021-03-05 00:05:34 +01:00

6.7 KiB

CustomTkinter

With CustomTkinter you can create modern looking user interfaces in python with tkinter. CustomTkinter is a tkinter extension which provides extra ui-elements like the CTkButton, which can be used like a normal tkinter.Button, but can be customized with a border and corner_radius.

CustomTkinter also supports a light and dark theme, which can either be set manually or get controlled by the system appearance mode (only macOS).

Example program (simple button):

import tkinter
import customtkinter

root_tk = tkinter.Tk()
root_tk.geometry("400x240")
root_tk.title("CustomTkinter Test")

def button_function():
    print("button pressed")

button = customtkinter.CTkButton(master=root_tk, corner_radius=10, command=button_function)
button.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)

root_tk.mainloop()

which gives the following:

How to use macOS dark mode?

If you have a python version with Tcl/Tk >= 8.6.9, then you can enable the macOS darkmode. Currently only the anaconda python versions have Tcl/Tk >= 8.6.9.

import tkinter
import customtkinter

customtkinter.enable_macos_darkmode()
customtkinter.set_appearance_mode("System")

... the program ...

customtkinter.disable_macos_darkmode()

Ui-Elements

CTkButton

Examle Code:

def button_event():
    print("button pressed")

button = customtkinter.CTkButton(master=root_tk,
                                 text="CTkButton",
                                 command=button_event,
                                 width=120,
                                 height=32,
                                 border_width=0,
                                 corner_radius=8)
button.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
Show all arguments:
argument value
master root, tkinter.Frame or CTkFrame
text string
command callback function
width button width in px
height button height in px
corner_radius corner radius in px
border_width button border width in px
fg_color forground color, tuple: (light_color, dark_color) or single color
bg_color background color, tuple: (light_color, dark_color) or single color
border_color border color, tuple: (light_color, dark_color) or single color
hover_color hover color, tuple: (light_color, dark_color) or single color
text_color text color, tuple: (light_color, dark_color) or single color
text_font button text font, tuple: (font_name, size)
hover enable/disable hover effect: True, False

CTkLabel

Example Code:

button = customtkinter.CTkButton(master=root_tk,
                                 text="CTkLabel",
                                 width=120,
                                 height=25,
                                 corner_radius=8)
button.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
Show all arguments:
argument value
master root, tkinter.Frame or CTkFrame
text string
width label width in px
height label height in px
corner_radius corner radius in px
fg_color forground color, tuple: (light_color, dark_color) or single color
bg_color background color, tuple: (light_color, dark_color) or single color
text_color label text color, tuple: (light_color, dark_color) or single color
text_font label text font, tuple: (font_name, size)

CTkEntry

Example Code:

entry = customtkinter.CTkEntry(master=root_tk,
                               width=120,
                               height=25,
                               corner_radius=10)
entry.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)

text = entry.get()
Show all arguments:
argument value
master root, tkinter.Frame or CTkFrame
width entry width in px
height entry height in px
corner_radius corner radius in px
fg_color forground color, tuple: (light_color, dark_color) or single color
bg_color background color, tuple: (light_color, dark_color) or single color
text_color entry text color, tuple: (light_color, dark_color) or single color
text_font entry text font, tuple: (font_name, size)

CTkSlider

Example Code:

def slider_event(value):
    print(value)

slider = customtkinter.CTkSlider(master=root_tk,
                                 width=160,
                                 height=16,
                                 border_width=5.5,
                                 command=slider_event)
slider.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
Show all arguments:
argument value
master root, tkinter.Frame or CTkFrame
command callback function, gest called when slider gets changed
width slider width in px
height slider height in px
border_width space around the slider rail in px
fg_color forground color, tuple: (light_color, dark_color) or single color
bg_color background color, tuple: (light_color, dark_color) or single color
border_color slider border color, normally transparent (None)
button_color color of the slider button, tuple: (light_color, dark_color) or single color
button_hover_color hover color, tuple: (light_color, dark_color) or single color

CTkProgressBar

Example Code:

progressbar = customtkinter.CTkSlider(master=root_tk,
                                      width=160,
                                      height=20,
                                      border_width=5)
progressbar.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)

progressbar.set(value)
Show all arguments:
argument value
master root, tkinter.Frame or CTkFrame
width slider width in px
height slider height in px
border_width border width in px
fg_color forground color, tuple: (light_color, dark_color) or single color
bg_color background color, tuple: (light_color, dark_color) or single color
border_color slider border color, tuple: (light_color, dark_color) or single color
progress_color progress color, tuple: (light_color, dark_color) or single color

CTkFrame

Example Code:

frame = customtkinter.CTkSlider(master=root_tk,
                                width=200,
                                height=200,
                                corner_radius=10)
frame.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
Show all arguments:
argument value
master root, tkinter.Frame or CTkFrame
width slider width in px
height slider height in px
fg_color forground color, tuple: (light_color, dark_color) or single color
bg_color background color, tuple: (light_color, dark_color) or single color