yyy.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import tkinter as tk
  2. class SquareWindow:
  3. def __init__(self):
  4. # Create the main window
  5. self.root = tk.Tk()
  6. self.root.title("Square Window")
  7. # Set window size to 1000x1000 pixels
  8. self.window_size = 1000
  9. self.root.geometry(f"{self.window_size}x{self.window_size}")
  10. # Create a canvas to draw on
  11. self.canvas = tk.Canvas(self.root, width=self.window_size, height=self.window_size, bg="white")
  12. self.canvas.pack(fill=tk.BOTH, expand=True)
  13. # Draw a square that is 90% of the window size
  14. square_size = int(self.window_size * 0.9) # 90% of 1000 = 900
  15. # Calculate the position to center the square
  16. margin = (self.window_size - square_size) // 2 # (1000 - 900) / 2 = 50
  17. # Coordinates for the square (centered in the window)
  18. x1, y1 = margin, margin
  19. x2, y2 = margin + square_size, margin + square_size
  20. # Draw the square
  21. self.canvas.create_rectangle(x1, y1, x2, y2, outline="black", width=2)
  22. # Determine how many squares to draw based on the desired number
  23. desired_number = 79 # You can change this to any number you want to draw
  24. # Find the closest perfect square that is >= desired_number
  25. import math
  26. n = math.ceil(math.sqrt(desired_number)) # For 19, sqrt(19) ≈ 4.36, ceil(4.36) = 5, so n=5
  27. actual_number = n * n # For n=5, actual_number = 25
  28. print(f"You wanted {desired_number} squares, but we're drawing {actual_number} squares in a {n}x{n} grid.")
  29. # Calculate the size of each small square (1/n of the main square's side minus 5 pixels)
  30. small_square_size = (square_size // n) - 5 # (900 / 5) - 5 = 175 pixels
  31. # Draw only the required number of squares (up to desired_number) in an n x n grid
  32. square_number = 1 # Start numbering from 1
  33. # Calculate spacing to distribute squares evenly with gaps
  34. total_used_space = n * small_square_size # Total space occupied by squares
  35. remaining_space = square_size - total_used_space # Remaining space for gaps
  36. gap_per_row = remaining_space // (n + 1) # Gap between squares and margins
  37. for i in range(n): # Rows
  38. for j in range(n): # Columns
  39. if square_number > desired_number:
  40. break # Stop drawing if we've reached the desired number
  41. # Calculate position for each small square with proper spacing
  42. small_x = x1 + gap_per_row + j * (small_square_size + gap_per_row)
  43. small_y = y1 + gap_per_row + i * (small_square_size + gap_per_row)
  44. # Draw the small square
  45. self.canvas.create_rectangle(
  46. small_x, small_y,
  47. small_x + small_square_size, small_y + small_square_size,
  48. outline="blue", width=1
  49. )
  50. # Add number in the center of each small square
  51. center_x = small_x + small_square_size // 2
  52. center_y = small_y + small_square_size // 2
  53. self.canvas.create_text(center_x, center_y, text=str(square_number), fill="red", font=("Arial", 10))
  54. square_number += 1
  55. if square_number > desired_number:
  56. break # Break outer loop as well if we've reached the desired number
  57. def run(self):
  58. self.root.mainloop()
  59. if __name__ == "__main__":
  60. app = SquareWindow()
  61. app.run()