본문 바로가기
프로그램

[파이썬] 문제 (tkinter) : 랜덤한 크기,색,위치의 원 그리기

by 오디세이99 2024. 5. 20.
728x90
반응형

import tkinter as tk
import random

def random_color():                                        # 랜덤 색상 생성 함수
    return "#{:06x}".format(random.randint(0, 0xFFFFFF))

def create_random_circles(canvas, num_circles):            # 랜덤 원 생성 함수
    canvas_width = int(canvas['width'])
    canvas_height = int(canvas['height'])
    
    for _ in range(num_circles):
        diameter = random.randint(10, 100)
        x1 = random.randint(0, canvas_width - diameter)
        y1 = random.randint(0, canvas_height - diameter)
        x2 = x1 + diameter
        y2 = y1 + diameter
        color = random_color()
        canvas.create_oval(x1, y1, x2, y2, fill=color, outline='black')

# 메인 윈도우 설정
root = tk.Tk()
root.title("Random Circles")

# 윈도우 크기 고정
root.geometry("800x600")
root.resizable(False, False)

# 캔버스 위젯 생성
canvas = tk.Canvas(root, width=800, height=600)
canvas.pack()

# 초기 랜덤 원 생성 (윈도우가 완전히 초기화된 후)
root.after(100, create_random_circles, canvas, 30)

# 애플리케이션 실행
root.mainloop()

728x90
반응형

댓글