WELCOME
Tutorials for Beginers
(Part 1)
scroll down for more "easy codes" !
(Part 1)
scroll down for more "easy codes" !
from tkinter import *
def draw_circle(canvas, x, y, r):
"""Draw a circle on the given canvas at (x, y) with radius r."""
canvas.create_oval(x - r, y - r, x + r, y + r, outline="blue", fill="blue")
# Create the main window
window = Tk()
window.title("Draw a Circle")
# Create a canvas
canvas = Canvas(window, width=400, height=400)
canvas.pack()
# Draw a circle at (200, 200) with radius 50
draw_circle(canvas, 200, 200, 50)
window.mainloop()
from tkinter import *
def draw_rectangle(canvas, x1, y1, x2, y2):
"""Draw a rectangle on the given canvas from (x1, y1) to (x2, y2)."""
canvas.create_rectangle(x1, y1, x2, y2, outline="red", fill="red")
# Create the main window
window = Tk()
window.title("Draw Rectangles")
# Create a canvas
canvas = Canvas(window, width=500, height=500)
canvas.pack()
# Draw multiple rectangles
draw_rectangle(canvas, 50, 50, 100, 100)
draw_rectangle(canvas, 150, 150, 200, 250)
draw_rectangle(canvas, 300, 100, 400, 200)
window.mainloop()
from tkinter import *
def draw_grid(canvas, rows, cols, width, height):
"""Draw a grid on the given canvas with the specified number of rows and columns."""
cell_width = width // cols
cell_height = height // rows
for row in range(rows):
for col in range(cols):
x1 = col * cell_width
y1 = row * cell_height
x2 = x1 + cell_width
y2 = y1 + cell_height
canvas.create_rectangle(x1, y1, x2, y2, outline="black")
# Create the main window
window = Tk()
window.title("Draw a Grid")
# Create a canvas
canvas = Canvas(window, width=400, height=400)
canvas.pack()
# Draw a grid pattern with 10 rows and 10 columns
draw_grid(canvas, 10, 10, 400, 400)
window.mainloop()
from tkinter import *
import random
def draw_lines(canvas, num_points):
"""Draw lines connecting random points on the canvas."""
points = [(random.randint(0, 400), random.randint(0, 400)) for _ in range(num_points)]
for i in range(num_points - 1):
x1, y1 = points[i]
x2, y2 = points[i + 1]
canvas.create_line(x1, y1, x2, y2, fill="green", width=2)
# Create the main window
window = Tk()
window.title("Random Lines")
# Create a canvas
canvas = Canvas(window, width=400, height=400)
canvas.pack()
# Draw lines connecting random points
draw_lines(canvas, 10)
window.mainloop()