gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
Ahoj, napiš mi program v pythonu . Analogové hodiny.
- User
Ahoj, napiš mi program v pythonu . Analogové hodiny.
- Assistant
Ahoj, napiš mi program v pythonu . Analogové hodiny.
Ahoj! Rád ti pomůžu s programem v Pythonu, který vykreslí analogové hodiny. Nejjednodušší způsob, jak toho dosáhnout, je použít knihovnu tkinter pro grafické rozhraní spolu s moduly math a time.
Zde máš jednoduchý příklad, který vykreslí analogové hodiny s ručičkami hodin, minut a sekund:
import tkinter as tk
import time
import math
class AnalogClock(tk.Canvas):
def __init__(self, parent, size=400):
super().__init__(parent, width=size, height=size, bg='white')
self.size = size
self.center = size / 2
self.radius = size * 0.45
self.pack()
self.update_clock()
def update_clock(self):
self.delete("all")
# Vykreslení ciferníku
self.create_oval(self.center - self.radius, self.center - self.radius,
self.center + self.radius, self.center + self.radius,
outline="black", width=4, fill='ivory')
# Čísla na ciferníku
for hour in range(1, 13):
angle = math.pi / 6 * (hour - 3) # Úhel pro čísla (posun o 3, aby 12 bylo nahoře)
x = self.center + math.cos(angle) * self.radius * 0.85
y = self.center + math.sin(angle) * self.radius * 0.85
self.create_text(x, y, text=str(hour), font=("Helvetica", int(self.size/15), "bold"))
# Získání aktuálního času
now = time.localtime()
hours = now.tm_hour % 12
minutes = now.tm_min
seconds = now.tm_sec
# Výpočet úhlů ručiček
angle_hour = (hours + minutes / 60) * (2 * math.pi / 12) - math.pi / 2
angle_min = (minutes + seconds / 60) * (2 * math.pi / 60) - math.pi / 2
angle_sec = seconds * (2 * math.pi / 60) - math.pi / 2
# Délky ručiček
hour_hand_length = self.radius * 0.5
min_hand_length = self.radius * 0.7
sec_hand_length = self.radius * 0.8
# Vykreslení hodinové ručičky
self.create_line(self.center, self.center,
self.center + hour_hand_length * math.cos(angle_hour),
self.center + hour_hand_length * math.sin(angle_hour),
width=6, fill='black')
# Vykreslení minutové ručičky
self.create_line(self.center, self.center,
self.center + min_hand_length * math.cos(angle_min),
self.center + min_hand_length * math.sin(angle_min),
width=4, fill='blue')
# Vykreslení sekundové ručičky
self.create_line(self.center, self.center,
self.center + sec_hand_length * math.cos(angle_sec),
self.center + sec_hand_length * math.sin(angle_sec),
width=2, fill='red')
# Vykreslení středového bodu
self.create_oval(self.center - 8, self.center - 8,
self.center + 8, self.center + 8,
fill='black')
# Aktualizuj každou sekundu
self.after(1000, self.update_clock)
if __name__ == "__main__":
root = tk.Tk()
root.title("Analogové hodiny")
clock = AnalogClock(root, 400)
root.mainloop()
Canvas).Stačí spustit, a hodiny se zobrazí a budou běžet.
Pokud chceš, můžu ti program upravit, přidat další funkce nebo vysvětlit podrobnosti. Stačí říct!
Not yet included in a published release.