آموزش پایتون | هوش مصنوعی | 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
آموزش پایتون | هوش مصنوعی | voidcompile
⏳ تایمر گرافیکی با پایتون (Python GUI Timer) اگر به دنبال یک پروژهی جذاب برای یادگیری برنامهنویسی پایتون هستی، ساخت تایمر گرافیکی (Graphical Timer) با استفاده از کتابخانهی Tkinter یکی از بهترین انتخابهاست! 🔹 در این پروژه یاد میگیری: ✅ طراحی رابط کاربری…
🚀 ۵ ترفند پایتون که باید بلد باشی!
اینجا ۵ تا ترفند کاربردی پایتون داری که سرعت و تمیزی کدنویسیت رو میبره بالا:
1. List Comprehension
2. zip برای ترکیب لیستها
3. Unpacking
4. Lambda Functions
5. معکوس کردن رشته با Slice
#python #LearnPython@voidcompile
💻@voidcompile
اینجا ۵ تا ترفند کاربردی پایتون داری که سرعت و تمیزی کدنویسیت رو میبره بالا:
1. List Comprehension
2. zip برای ترکیب لیستها
3. Unpacking
4. Lambda Functions
5. معکوس کردن رشته با Slice
# 🔹 ۵ ترفند خفن پایتون 🔹
# 1) List Comprehension: ساخت سریع لیست
squares = [x**2 for x in range(1, 6)]
print(squares) # [1, 4, 9, 16, 25]
# 2) zip: ترکیب چند لیست همزمان
names = ["Ali", "Sara", "Reza"]
scores = [95, 88, 76]
for n, s in zip(names, scores):
print(n, s)
# 3) Unpacking: بازکردن عناصر به چند متغیر
a, b, *rest = [1, 2, 3, 4, 5]
print(a, b, rest) # 1 2 [3, 4, 5]
# 4) Lambda: تابع جمع کوتاه
add = lambda x, y: x + y
print(add(5, 7)) # 12
# 5) Slice معکوس رشته
text = "Python"
print(text[::-1]) # nohtyP
#python #LearnPython@voidcompile
💻@voidcompile
1🏆21❤18🔥18👍11🤩11💯10