Create a simple drawing app using the Python library "TKEasyGUI"
table of contents [非表示]
Hello
Interested and Curious
is Kawaii from the Volatile Systems Solutions Department.
It's already December this year.
The other day, I was playing around with a Python TKEasyGUI , and I heard that you can easily create desktop applications, so I decided to create a drawing application.
What is TKEasyGUI?
It seems to have been developed in response to the
PySimpleGUI For more information: https://github.com/kujirahand/tkeasygui-python
Preparation
Now, let's install it and play with it.
■Verification environment
1 | Ubuntu 24.04 LTS Python 3.12.3 |
This time we will use venv. You can do anything you want in this area.
1 | $ python3 -m venv env $ source env /bin/activate |
TKEasyGUI and tkinter are also required, so install them.
1 | ( env )$ pip install TKEasyGUI ( env )$ sudo apt install python3-tk |
That's all you need to prepare. It's super easy.
Try the sample app
Now, let's run the sample app posted on github.
>> "How to use - popup dialogs"
1 | import TkEasyGUI as eg name = eg. input ( "What is your name?" ) eg. print (f "Hello, {name}." ) |
1 | ( env )$ python3 question.py |
You will be asked for your name, so enter it.
They will greet you.
It's easy because you only have to write two documents except for import.
Next, let's create a simple drawing app.
drawing app.py
Anything is fine for now, so I'll make it possible to save it as a PNG, draw it easily, and select the color.
The following Canvas elements can be used for depiction.
https://github.com/kujirahand/tkeasygui-python/blob/main/docs/TkEasyGUI/widgets-py.md#canvas
the full code below
, if you want to run it, please try running it.
1 | import TkEasyGUI as eg from tkinter import filedialog, colorchooser from PIL import Image, ImageDraw # Drawing settings drawing_color = "black" # Initial color last_x, last_y = None, None # Record last position # File save function def save_canvas_as_image(): file_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG file", "*.png")]) if file_path: canvas_image.save(file_path) eg.popup("Save completed", "Image saved.") # Color selection function def choose_color(): global drawing_color color_code = colorchooser.askcolor(title="Choose color") if color_code: drawing_color = color_code[1] # Get the hex code of the selected color # Draw function def start_draw(event): global last_x, last_y last_x, last_y = event.x, event.y def draw(event): global last_x, last_y x, y = event .x, event.y if last_x is not None and last_y is not None: # Draw a line on the canvas canvas.get().create_line(last_x, last_y, x, y, fill=drawing_color, width=2) # Also draw on PIL Image draw_on_image.line([last_x, last_y, x, y], fill=drawing_color, width=2) last_x, last_y = x, y # Clear function def clear_canvas(): canvas.clear() draw_on_image.rectangle((0, 0, 400, 400), fill="white") # Also reset the image # Layout definition layout = [ [eg.Text("Drawing app")], [eg.Canvas(key="-CANVAS-", size=(400, 400), background_color="white")], [eg.Button("Change color", key="-COLOR-"), eg.Button("Clear"), eg.Button("Save"), eg.Button("End")] ] # Create a PIL image and get the drawing object canvas_image = Image.new("RGB", (400, 400), "white") draw_on_image = ImageDraw.Draw(canvas_image) # Create a window with eg.Window("Drawing app", layout) as window: canvas = window["-CANVAS-"] # Bind mouse events canvas.get().bind("<Button-1> ", start_draw) canvas.get().bind("<B1-Motion> ", draw) # Event loop for event, values in window.event_iter(): if event == "-COLOR-": choose_color() elif event == "Clear": clear_canvas() elif event == "Save": save_canvas_as_image() elif event == "End" or event is None: break |
I'll try starting it.
1 | ( env )$ python oekaki.py |
A white canvas was displayed.
I'll try drawing a cat.
I'll try saving it.
You can also change the color.
Looks good,
I'll try making it again when I have time. Well then.
complete