آموزش پایتون | هوش مصنوعی | voidcompile
⏳ تایمر گرافیکی با پایتون (Python GUI Timer) اگر به دنبال یک پروژهی جذاب برای یادگیری برنامهنویسی پایتون هستی، ساخت تایمر گرافیکی (Graphical Timer) با استفاده از کتابخانهی Tkinter یکی از بهترین انتخابهاست! 🔹 در این پروژه یاد میگیری: ✅ طراحی رابط کاربری…
کد تایمر گرافیکی ساده
#code #python
#LearnPython@voidcompile
💻@voidcompile
import tkinter as tk
class TimerApp:
def __init__(self, root):
self.root = root
self.root.title("⏳ Timer")
self.root.geometry("300x200")
self.root.configure(bg="black")
self.time_left = 60 # ⏱ زمان اولیه تایمر (به ثانیه)
# 🎨 برچسب نمایش زمان
self.label = tk.Label(
root,
text=self.format_time(self.time_left),
font=("Helvetica", 48),
fg="cyan",
bg="black"
)
self.label.pack(expand=True)
# ▶ دکمه شروع تایمر
self.start_button = tk.Button(root, text="▶ Start", command=self.start_timer, bg="green", fg="white")
self.start_button.pack(side="left", expand=True, fill="both")
# 🔄 دکمه ریست تایمر
self.reset_button = tk.Button(root, text="🔄 Reset", command=self.reset_timer, bg="red", fg="white")
self.reset_button.pack(side="right", expand=True, fill="both")
self.running = False # 📌 وضعیت تایمر (در حال اجرا یا متوقف)
def format_time(self, seconds):
# ⏱ تبدیل ثانیه به دقیقه:ثانیه (mm:ss)
mins = seconds // 60
secs = seconds % 60
return f"{mins:02}:{secs:02}"
def update_timer(self):
# ⏳ کاهش یک ثانیه و آپدیت صفحه
if self.running and self.time_left > 0:
self.time_left -= 1
self.label.config(text=self.format_time(self.time_left))
self.root.after(1000, self.update_timer) # هر ۱۰۰۰ میلیثانیه (۱ ثانیه) اجرا بشه
elif self.time_left == 0:
self.label.config(text="⏰ Time's up!") # وقتی تایمر تموم شد
def start_timer(self):
# ▶ شروع تایمر
if not self.running:
self.running = True
self.update_timer()
def reset_timer(self):
# 🔄 ریست تایمر به مقدار اولیه
self.running = False
self.time_left = 60
self.label.config(text=self.format_time(self.time_left))
if __name__ == "__main__":
root = tk.Tk()
app = TimerApp(root)
root.mainloop()
ری اکشن یادتون نره رفقا
#code #python
#LearnPython@voidcompile
💻@voidcompile
1🔥17👍13❤9💯8🤩5👨💻3