3 метода рисования круга с использованием Tkinter в Python

Чтобы нарисовать круг с помощью Tkinter, вы можете использовать следующие методы:

Метод 1: использование Canvas

import tkinter as tk
# Create a window
window = tk.Tk()
# Create a canvas
canvas = tk.Canvas(window, width=400, height=400)
canvas.pack()
# Draw a circle
circle = canvas.create_oval(100, 100, 300, 300, outline='black', fill='red')
# Run the window's main loop
window.mainloop()

Метод 2: использование пользовательского виджета

import tkinter as tk
# Create a custom widget
class CircleWidget(tk.Canvas):
    def __init__(self, parent, radius, kwargs):
        super().__init__(parent, kwargs)
        self.radius = radius
    def draw_circle(self):
        x0 = y0 = self.radius
        x1 = y1 = self.winfo_width() - self.radius
        self.create_oval(x0, y0, x1, y1, outline='black', fill='red')
# Create a window
window = tk.Tk()
# Create the custom widget
circle_widget = CircleWidget(window, radius=100)
circle_widget.pack()
# Draw the circle
circle_widget.draw_circle()
# Run the window's main loop
window.mainloop()

Метод 3: использование PIL (библиотеки изображений Python)

import tkinter as tk
from PIL import Image, ImageTk, ImageDraw
# Create a window
window = tk.Tk()
# Create a canvas
canvas = tk.Canvas(window, width=400, height=400)
canvas.pack()
# Create an image
image = Image.new('RGB', (400, 400), 'white')
draw = ImageDraw.Draw(image)
# Draw a circle
draw.ellipse((100, 100, 300, 300), outline='black', fill='red')
# Convert the image to Tkinter format
image_tk = ImageTk.PhotoImage(image)
# Display the image on the canvas
canvas.create_image(0, 0, image=image_tk, anchor='nw')
# Run the window's main loop
window.mainloop()