import tkinter as tk class SquareWindow: def __init__(self): # Create the main window self.root = tk.Tk() self.root.title("Square Window") # Set window size to 1000x1000 pixels self.window_size = 1000 self.root.geometry(f"{self.window_size}x{self.window_size}") # Create a canvas to draw on self.canvas = tk.Canvas(self.root, width=self.window_size, height=self.window_size, bg="white") self.canvas.pack(fill=tk.BOTH, expand=True) # Draw a square that is 90% of the window size square_size = int(self.window_size * 0.9) # 90% of 1000 = 900 # Calculate the position to center the square margin = (self.window_size - square_size) // 2 # (1000 - 900) / 2 = 50 # Coordinates for the square (centered in the window) x1, y1 = margin, margin x2, y2 = margin + square_size, margin + square_size # Draw the square self.canvas.create_rectangle(x1, y1, x2, y2, outline="black", width=2) # Determine how many squares to draw based on the desired number desired_number = 79 # You can change this to any number you want to draw # Find the closest perfect square that is >= desired_number import math n = math.ceil(math.sqrt(desired_number)) # For 19, sqrt(19) ≈ 4.36, ceil(4.36) = 5, so n=5 actual_number = n * n # For n=5, actual_number = 25 print(f"You wanted {desired_number} squares, but we're drawing {actual_number} squares in a {n}x{n} grid.") # Calculate the size of each small square (1/n of the main square's side minus 5 pixels) small_square_size = (square_size // n) - 5 # (900 / 5) - 5 = 175 pixels # Draw only the required number of squares (up to desired_number) in an n x n grid square_number = 1 # Start numbering from 1 # Calculate spacing to distribute squares evenly with gaps total_used_space = n * small_square_size # Total space occupied by squares remaining_space = square_size - total_used_space # Remaining space for gaps gap_per_row = remaining_space // (n + 1) # Gap between squares and margins for i in range(n): # Rows for j in range(n): # Columns if square_number > desired_number: break # Stop drawing if we've reached the desired number # Calculate position for each small square with proper spacing small_x = x1 + gap_per_row + j * (small_square_size + gap_per_row) small_y = y1 + gap_per_row + i * (small_square_size + gap_per_row) # Draw the small square self.canvas.create_rectangle( small_x, small_y, small_x + small_square_size, small_y + small_square_size, outline="blue", width=1 ) # Add number in the center of each small square center_x = small_x + small_square_size // 2 center_y = small_y + small_square_size // 2 self.canvas.create_text(center_x, center_y, text=str(square_number), fill="red", font=("Arial", 10)) square_number += 1 if square_number > desired_number: break # Break outer loop as well if we've reached the desired number def run(self): self.root.mainloop() if __name__ == "__main__": app = SquareWindow() app.run()