CustomTkinter/Readme.md

504 lines
17 KiB
Markdown
Raw Normal View History

2021-05-08 13:52:44 +03:00
![PyPI](https://img.shields.io/pypi/v/customtkinter)
2021-05-09 12:31:01 +03:00
![PyPI - Downloads](https://img.shields.io/pypi/dm/customtkinter?color=green&label=pip%20downloads)
2021-05-08 13:52:44 +03:00
![PyPI - License](https://img.shields.io/pypi/l/customtkinter)
2021-11-07 17:12:09 +03:00
![Total lines](https://img.shields.io/tokei/lines/github.com/tomschimansky/customtkinter?color=green&label=total%20lines)
2021-05-08 13:52:44 +03:00
2022-01-22 21:16:58 +03:00
# CustomTkinter library
![](documentation_images/tkinter_customtkinter_comparison.jpg)
2021-03-04 20:27:46 +03:00
2021-03-04 21:10:12 +03:00
With CustomTkinter you can create modern looking user
interfaces in python with tkinter. CustomTkinter is a
2021-03-05 01:39:09 +03:00
tkinter extension which provides extra ui-elements like
2021-03-04 21:10:12 +03:00
the CTkButton, which can be used like a normal tkinter.Button,
2021-03-05 02:44:00 +03:00
but can be customized with a border and round edges.
2021-03-04 21:10:12 +03:00
CustomTkinter also supports a light and dark theme,
2021-03-04 21:54:01 +03:00
which can either be set manually or get controlled by
2021-07-13 12:39:52 +03:00
the system appearance mode.
2021-03-04 21:10:12 +03:00
2021-05-08 13:36:36 +03:00
### Installation
2021-05-08 13:35:32 +03:00
2021-03-05 18:21:25 +03:00
To use CustomTkinter, just place the /customtkinter folder from this repository
2021-05-08 13:35:32 +03:00
next to your program, or **install the module with pip**:
```
2021-08-25 18:33:44 +03:00
pip3 install customtkinter
2021-05-08 13:35:32 +03:00
```
2021-10-15 14:57:38 +03:00
**Update existing installation:** ```pip3 install customtkinter --upgrade```\
(from time to time bugs are getting fixed and new features are added)
2021-05-08 17:40:59 +03:00
PyPI: https://pypi.org/project/customtkinter/
2021-05-08 13:35:32 +03:00
### Example program (simple button):
To test customtkinter you can try this simple example with only a single button:
2021-03-05 02:05:34 +03:00
```python
import tkinter
2021-03-05 02:32:17 +03:00
import customtkinter # <- import the CustomTkinter module
2022-01-22 18:15:01 +03:00
root_tk = customtkinter.CTk() # create CTk window like you do with the Tk window
2021-03-05 02:05:34 +03:00
root_tk.geometry("400x240")
root_tk.title("CustomTkinter Test")
def button_function():
print("button pressed")
2021-03-05 02:32:17 +03:00
# Use CTkButton instead of tkinter Button
2021-03-05 02:05:34 +03:00
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:
2021-03-05 02:32:17 +03:00
2022-01-22 21:16:58 +03:00
![](documentation_images/macOS_button_light.png)
2021-03-05 02:05:34 +03:00
2021-03-05 02:32:17 +03:00
### Use custom colors and shapes:
2022-01-22 18:15:01 +03:00
If you don't specify any colors, customtkinter uses the standard blue color theme in the light mode.
2022-01-22 18:17:35 +03:00
You can change the appearance mode to dark by calling
2021-03-05 02:32:17 +03:00
```customtkinter.set_appearance_mode("Dark")```.
2022-01-22 18:17:35 +03:00
If you specify custom colors for CustomTkinter elements, then you can either use a
2021-03-05 02:32:17 +03:00
tuple in the form: (light_color, dark_color). Or you can set a single color
2022-01-22 18:17:35 +03:00
which will be used in light and dark appearance mode.
2021-03-05 02:36:39 +03:00
```python
2021-07-13 12:45:46 +03:00
customtkinter.set_appearance_mode("Dark") # Other: "Light", "System"
2021-03-05 18:21:25 +03:00
2021-03-05 02:36:39 +03:00
button = customtkinter.CTkButton(master=root_tk,
fg_color=("black", "lightgray"), # <- tuple color for light and dark theme
text="CTkButton",
command=button_event)
button.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
```
2021-03-05 02:32:17 +03:00
### Dark mode and dark title-bar on macOS
If you have a python version with Tcl/Tk >= 8.6.9, then you automatically get
a dark title bar with macOS dark-mode on, if you use the `customtkinter.Ctk` class to create
the window instead of the normal `tkinterTk` class. Currently, only the anaconda python versions have Tcl/Tk >= 8.6.9.
So if you want a dark window title-bar, you have to install anaconda python version
2021-03-06 00:23:53 +03:00
or miniconda.
2021-03-05 02:05:34 +03:00
```python
import tkinter
import customtkinter
customtkinter.set_appearance_mode("System")
root_tk = customtkinter.CTk()
2021-03-05 02:05:34 +03:00
... the program ...
root_tk.mainloop()
2021-03-05 02:05:34 +03:00
```
2021-03-05 02:32:17 +03:00
2021-03-14 17:45:02 +03:00
If you set the appearance mode to "System", it should change with
the System mode:
2022-01-22 21:16:58 +03:00
![](documentation_images/appearance_mode_switch.gif)
2021-03-14 17:45:02 +03:00
2021-03-05 22:27:34 +03:00
### Advanced example with multiple CTkFrames
Here I used the ``customtkinter.CTk()`` class to create the main window with two CTkFrame's and
set the appearance mode to `System`. It has some
2021-03-05 22:27:34 +03:00
kind of a menu on the left side, and I used all CustomTkinter elements
2022-01-22 22:28:59 +03:00
there are at the moment. Maybe this is a good reference if you want to
2021-03-05 22:27:34 +03:00
create your own application with this library.
2022-01-22 22:28:59 +03:00
(Code: `examples/complex_example.py`)
2021-03-05 22:27:34 +03:00
With the green theme or the blue theme it looks like this:
2021-03-05 22:27:34 +03:00
![](documentation_images/macOS_light_dark_comparison.jpg)
2021-03-05 22:27:34 +03:00
2022-01-08 02:50:31 +03:00
### Default color themes
If you don't set any colors at all you will get the standard blue
2022-01-22 18:17:35 +03:00
color theme. But you can also change the standard color theme to
2022-01-08 02:50:31 +03:00
green or dark-blue with the following command before you create
the main window:
```python
customtkinter.set_appearance_mode("System")
customtkinter.set_default_color_theme("dark-blue") # Themes: "blue" (standard), "green", "dark-blue"
```
The color themes look like the following in light and dark mode:
2022-01-22 21:16:58 +03:00
![](documentation_images/theme_comparison.jpg)
2022-01-08 02:50:31 +03:00
2021-12-04 14:54:57 +03:00
### CustomTkinter on Windows/Linux
2021-11-11 01:18:04 +03:00
All elements of Customtkinter are drawn on the ```tkinter.Canvas```.
2022-01-30 01:55:48 +03:00
But the Tkinter canvas supports antialiasing only on macOS (provided by the system), so on Windows
2021-11-11 01:18:04 +03:00
and Linux the elements are rendered in a much worse quality. So you have
2022-01-22 18:15:01 +03:00
to experiment with the ```corner_radius``` and decide when the rounded corners
2021-11-11 01:18:04 +03:00
look best. I tried to design the too complex example programs so that they
2022-01-22 18:15:01 +03:00
also look acceptable on Windows too. Maybe you can use the parameters for
2021-11-11 01:18:04 +03:00
```corner_radius``` and ```width``` for your program as well.
2022-01-22 18:15:01 +03:00
Example 1: ```examples/complex_example.py``` (light and dark mode)
2021-11-11 01:18:04 +03:00
2022-01-22 21:16:58 +03:00
![](documentation_images/Windows_complex_bg.jpg)
2021-11-11 01:18:04 +03:00
2022-01-22 22:28:59 +03:00
In the following example I customized the elements with new colors, chnaged the corner_radius
and added a border to the button.
2022-01-22 18:15:01 +03:00
Example 2: ```examples/complex_example_custom_colors.py``` (dark mode)
2021-11-11 01:18:04 +03:00
2022-01-22 21:16:58 +03:00
![](documentation_images/Windows_complex_other_style_bg.jpg)
2021-11-11 01:18:04 +03:00
2021-03-08 20:17:36 +03:00
### CTkButton with images
It's also possible to put an image on a CTkButton. You just have to
pass a PhotoImage object to the CTkButton with the argument ``image``.
2021-11-06 16:21:28 +03:00
If you want no text at all you have to set ``text=""`` or with the ``compound``
option you can specify how to position both the text and image at once.
2021-03-08 20:17:36 +03:00
You can find an example program ( /simple_test_images.py ), where I
created two buttons with a bell and a settings image on them:
2022-01-22 21:16:58 +03:00
![](documentation_images/macOS_button_images.png)
2021-03-08 20:17:36 +03:00
2022-01-02 17:29:50 +03:00
### Integration of TkinterMapView widget
In the following example I used a TkinterMapView which integrates
well with a CustomTkinter program. It's a tile based map widget which displays
OpenStreetMap or other tile based maps:
2022-01-22 21:16:58 +03:00
![](documentation_images/tkintermapview_example.gif)
2021-03-05 23:05:53 +03:00
2022-01-02 17:29:50 +03:00
You can find the TkinterMapView library and the example program here:
https://github.com/TomSchimansky/TkinterMapView
# Documentation - CustomTkinter Elements
2021-03-05 01:39:09 +03:00
### CTk
2022-01-02 00:40:20 +03:00
You can use the normal ``tkinter.Tk`` class to create the root window,
but if you want a background color that changes with the appearance mode and a dark title-bar on macOS,
you should use the `customtkinter.CTk` class which behaves exactly like the normal Tk
class, except that you can also set a tuple color as bg color.
Example Code:
```python
root_tk = customtkinter.CTk()
... program ...
root_tk.mainloop()
```
<details>
<summary>Show all arguments and methods:</summary>
argument | value
--- | ---
bg_color or bg | tuple: (light_color, dark_color) or single color
CTk Methods:
```python
root_tk = customtkinter.CTk()
# configure bg color with single or tuple color
root_tk.configure(bg_color="gray20")
root_tk.configure(bg_color=(<light-mode color>, <dark-mode color>))
```
2022-01-02 00:39:17 +03:00
</details>
2022-02-23 00:38:40 +03:00
### CTkToplevel
CTkToplevel works exactly like the `Tkinter.Toplevel`, and should be used to create more than one window.
<details>
<summary>Show all arguments and methods:</summary>
argument | value
--- | ---
bg_color or bg | tuple: (light_color, dark_color) or single color
</details>
2022-01-02 17:29:50 +03:00
### CTkFrame
Example Code:
```python
frame = customtkinter.CTkFrame(master=root_tk,
width=200,
height=200,
corner_radius=10)
frame.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
```
<details>
<summary>Show all arguments and methods:</summary>
2022-01-02 17:29:50 +03:00
argument | value
--- | ---
master | root, tkinter.Frame or CTkFrame
width | slider width in px
height | slider height in px
fg_color | foreground color, tuple: (light_color, dark_color) or single color
2022-01-02 17:29:50 +03:00
bg_color | background color, tuple: (light_color, dark_color) or single color
</details>
2021-03-05 01:39:09 +03:00
### CTkButton
2022-01-22 21:16:58 +03:00
Example Code:
2021-03-05 01:39:09 +03:00
```python
def button_event():
print("button pressed")
button = customtkinter.CTkButton(master=root_tk,
width=120,
height=32,
border_width=0,
corner_radius=8,
text="CTkButton",
command=button_event)
2021-03-05 01:39:09 +03:00
button.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
```
<details>
<summary>Show all arguments and methods:</summary>
2021-03-05 01:39:09 +03:00
argument | value
--- | ---
master | root, tkinter.Frame or CTkFrame
command | callback function
textvariable | tkinter.StringVar object to change text of button
text | string
2021-03-05 01:39:09 +03:00
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
2021-03-08 20:28:40 +03:00
image | put an image on the button, removes the text, must be class PhotoImage
compound | set image orientation if image and text are given ("top", "left", "bottom", "right")
2021-07-23 01:44:13 +03:00
state | tkinter.NORMAL (standard) or tkinter.DISABLED (not clickable, darker color)
2021-03-08 20:28:40 +03:00
CTkButton Methods:
2021-03-08 20:28:40 +03:00
```python
CTkButton.set_text(new_text)
CTkButton.set_image(new_image)
CTkButton.configure(text=new_text)
CTkButton.configure(bg_color=new_bg_color,
fg_color=new_fg_color,
hover_color=new_hover_color,
text_color=new_text_color)
2021-07-23 01:44:13 +03:00
CTkButton.configure(state=tkinter.DISABLED)
CTkButton.configure(state=tkinter.NORMAL)
button_state = CTkButton.state
2021-03-08 20:28:40 +03:00
```
2021-03-05 01:39:09 +03:00
</details>
### CTkLabel
Example Code:
```python
2021-03-05 22:27:34 +03:00
label = customtkinter.CTkLabel(master=root_tk,
text="CTkLabel",
width=120,
height=25,
corner_radius=8)
2021-03-05 02:44:00 +03:00
label.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
2021-03-05 01:39:09 +03:00
```
<details>
<summary>Show all arguments and methods:</summary>
2021-03-05 01:39:09 +03:00
argument | value
--- | ---
master | root, tkinter.Frame or CTkFrame
variable | tkinter.StringVar object
2021-03-05 01:39:09 +03:00
text | string
width | label width in px
height | label height in px
corner_radius | corner radius in px
fg_color | foreground color, tuple: (light_color, dark_color) or single color
bg_color | background color, tuple: (light_color, dark_color) or single color, None for transparent bg
2021-03-05 01:39:09 +03:00
text_color | label text color, tuple: (light_color, dark_color) or single color
text_font | label text font, tuple: (font_name, size)
2021-12-04 14:54:57 +03:00
CTkLabel Methods:
```python
CTkLabel.configure(text=new_text)
CTkLabel.configure(fg_color=new_fg_color,
bg_color=new_bg_color,
text_color=new_text_color)
```
2021-03-05 01:39:09 +03:00
</details>
### CTkEntry
Example Code:
```python
entry = customtkinter.CTkEntry(master=root_tk,
placeholder_text="CTkEntry",
2021-03-05 01:39:09 +03:00
width=120,
height=25,
corner_radius=10)
entry.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
text = entry.get()
```
<details>
<summary>Show all arguments and methods:</summary>
2021-03-05 01:39:09 +03:00
argument | value
--- | ---
master | root, tkinter.Frame or CTkFrame
variable | tkinter.StringVar object
2021-03-05 01:39:09 +03:00
width | entry width in px
height | entry height in px
corner_radius | corner radius in px
fg_color | foreground color, tuple: (light_color, dark_color) or single color
2021-03-05 01:39:09 +03:00
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
2022-02-05 00:55:27 +03:00
placeholder_text_color | tuple: (light_color, dark_color) or single color
placeholder_text | hint on the entry input (disappears when selected), default is None
2021-03-05 01:39:09 +03:00
text_font | entry text font, tuple: (font_name, size)
2021-12-04 14:54:57 +03:00
2022-02-05 00:55:27 +03:00
2021-12-04 14:54:57 +03:00
CTkEntry Methods:
```python
CTkEntry.delete(...) # like the standard tkinter Entry...
2021-12-04 14:54:57 +03:00
CTkEntry.insert(...)
text = CTkEntry.get()
```
2021-03-05 01:39:09 +03:00
</details>
2021-08-25 18:02:16 +03:00
### CTkCheckBox
Examle Code:
```python
checkbox = customtkinter.CTkCheckBox(master=root_tk,
text="CTkCheckBox")
checkbox.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
```
<details>
<summary>Show all arguments and methods:</summary>
2021-08-25 18:02:16 +03:00
argument | value
--- | ---
master | root, tkinter.Frame or CTkFrame
text | string
width | box width in px
height | box height in px
corner_radius | corner radius in px
border_width | box border width in px
fg_color | foreground (inside) color, tuple: (light_color, dark_color) or single color
2021-08-25 18:02:16 +03:00
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
state | tkinter.NORMAL (standard) or tkinter.DISABLED (not clickable, darker color)
2022-02-23 00:38:40 +03:00
command | function will be called when the checkbox is clicked
2021-08-25 18:02:16 +03:00
CTkCheckBox Methods:
```python
CTkCheckBox.get() # 1 or 0 (checked or not checked)
CTkCheckBox.select() # turns on checkbox
CTkCheckBox.deselect() # turns off checkbox
CTkCheckBox.toggle() # change check state of checkbox
CTkCheckBox.configure(text=new_text)
CTkCheckBox.configure(bg_color=new_bg_color,
fg_color=new_fg_color,
hover_color=new_hover_color,
text_color=new_text_color)
2021-08-25 18:02:16 +03:00
CTkCheckBox.configure(state=tkinter.DISABLED)
CTkCheckBox.configure(state=tkinter.NORMAL)
checkbox_state = CTkCheckBox.state
```
</details>
2021-03-05 01:39:09 +03:00
### CTkSlider
Example Code:
```python
def slider_event(value):
print(value)
slider = customtkinter.CTkSlider(master=root_tk,
width=160,
height=16,
border_width=5.5,
2021-09-13 15:27:29 +03:00
from_=0,
to=100,
2021-03-05 01:39:09 +03:00
command=slider_event)
slider.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
```
<details>
<summary>Show all arguments and methods:</summary>
2021-03-05 01:39:09 +03:00
argument | value
--- | ---
master | root, tkinter.Frame or CTkFrame
command | callback function, gest called when slider gets changed
variable | tkinter.IntVar or tkinter.DoubleVar object
2021-03-05 01:39:09 +03:00
width | slider width in px
height | slider height in px
2021-09-13 15:27:29 +03:00
from_ | lower slider value
to | upper slider value
number_of_steps | number of steps in which the slider can be positioned
2021-03-05 01:39:09 +03:00
border_width | space around the slider rail in px
fg_color | foreground color, tuple: (light_color, dark_color) or single color
2021-12-14 00:02:59 +03:00
progress_color | tuple: (light_color, dark_color) or single color, colors the slider line before the round button and is set to fg_color by default
2021-03-05 01:39:09 +03:00
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
2021-12-04 14:54:57 +03:00
CTkSlider Methods:
```python
value = CTkSlider.get()
CTkSlider.set(value)
```
2021-03-05 01:39:09 +03:00
</details>
### CTkProgressBar
Example Code:
```python
2021-03-05 22:27:34 +03:00
progressbar = customtkinter.CTkProgressBar(master=root_tk,
width=160,
height=20,
border_width=5)
2021-03-05 01:39:09 +03:00
progressbar.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
progressbar.set(value)
```
<details>
<summary>Show all arguments and methods:</summary>
2021-03-05 01:39:09 +03:00
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 | foreground color, tuple: (light_color, dark_color) or single color
2021-03-05 01:39:09 +03:00
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
</details>
2021-03-05 22:40:25 +03:00
### Special commands
2021-03-06 01:09:14 +03:00
Change appearance mode:
2021-03-05 22:40:25 +03:00
```python
2021-03-06 01:09:14 +03:00
customtkinter.set_appearance_mode("Light")
customtkinter.set_appearance_mode("Dark")
2021-07-13 12:39:52 +03:00
customtkinter.set_appearance_mode("System")
2021-03-06 01:09:14 +03:00
print(customtkinter.get_appearance_mode())
```
2021-03-05 22:40:25 +03:00
2022-01-08 02:50:31 +03:00
Set default color theme:
```python
customtkinter.set_default_color_theme("dark-blue") # Themes: "blue" (standard), "green", "dark-blue"
```
2022-01-02 17:29:50 +03:00
Use macOS darkmode window style without using the `customtkinter.Ctk` class:
2021-03-06 01:09:14 +03:00
```python
2021-03-05 22:40:25 +03:00
customtkinter.enable_macos_darkmode() # get darkmode window style
... program ...
customtkinter.disable_macos_darkmode() # disable darkmode (very important!)
2021-03-05 22:40:25 +03:00
```