Python紅綠燈模擬程式
此程式由AI輔助生成
import tkinter as tk
from tkinter import ttk
class TrafficLightSimulator:
def __init__(self, master):
self.master = master
self.master.title("交通燈模擬器")
self.master.geometry("300x500")
self.master.configure(bg="#f0f0f0")
self.current_color = None
self.remaining_time = 0
self.create_ui_elements()
self.draw_traffic_light()
def create_ui_elements(self):
self.canvas = tk.Canvas(self.master, width=200, height=400, bg="#f0f0f0", highlightthickness=0)
self.canvas.pack(pady=20)
self.label = ttk.Label(self.master, text="剩餘秒數:", font=("微軟正黑體", 12))
self.label.pack()
self.timer_label = ttk.Label(self.master, text="0", font=("微軟正黑體", 24))
self.timer_label.pack()
button_frame = ttk.Frame(self.master)
button_frame.pack(pady=10)
style = ttk.Style()
style.configure("TButton", font=("微軟正黑體", 12))
self.start_button = ttk.Button(button_frame, text="開始", command=self.start_simulation, style="TButton")
self.start_button.pack(side=tk.LEFT, padx=10)
self.stop_button = ttk.Button(button_frame, text="停止", command=self.stop_simulation, style="TButton")
self.stop_button.pack(side=tk.LEFT, padx=10)
self.reset_button = ttk.Button(button_frame, text="重置", command=self.reset_simulation, style="TButton")
self.reset_button.pack(side=tk.LEFT, padx=10)
self.light_timer = None
def draw_traffic_light(self):
self.red_light = self.canvas.create_oval(50, 30, 150, 130, outline="black", fill="grey", width=2)
self.yellow_light = self.canvas.create_oval(50, 150, 150, 250, outline="black", fill="grey", width=2)
self.green_light = self.canvas.create_oval(50, 270, 150, 370, outline="black", fill="grey", width=2)
def update_traffic_light(self, color):
self.canvas.itemconfig(self.red_light, fill="grey")
self.canvas.itemconfig(self.yellow_light, fill="grey")
self.canvas.itemconfig(self.green_light, fill="grey")
if color == "red":
self.canvas.itemconfig(self.red_light, fill="red")
elif color == "yellow":
self.canvas.itemconfig(self.yellow_light, fill="yellow")
elif color == "green":
self.canvas.itemconfig(self.green_light, fill="green")
def start_simulation(self):
self.stop_simulation() # 確保停止之前的計時器
self.current_color = "green"
self.remaining_time = 5 # 從綠燈開始,持續5秒
self.update_traffic_light(self.current_color)
self.timer_label.config(text=str(self.remaining_time))
self.light_timer = self.master.after(1000, self.update_timer)
def stop_simulation(self):
if self.light_timer:
self.master.after_cancel(self.light_timer)
self.light_timer = None
def reset_simulation(self):
self.stop_simulation()
self.update_traffic_light("grey")
self.current_color = None
self.remaining_time = 0
self.timer_label.config(text="0")
def update_timer(self):
self.remaining_time -= 1
self.timer_label.config(text=str(self.remaining_time))
if self.remaining_time == 0:
if self.current_color == "green":
self.current_color = "yellow"
self.remaining_time = 1 # 黃燈持續1秒
elif self.current_color == "yellow":
self.current_color = "red"
self.remaining_time = 5 # 紅燈持續5秒
elif self.current_color == "red":
self.current_color = "green"
self.remaining_time = 5 # 綠燈持續5秒
self.update_traffic_light(self.current_color)
self.light_timer = self.master.after(1000, self.update_timer)
if __name__ == "__main__":
root = tk.Tk()
root.configure(bg="#f0f0f0")
app = TrafficLightSimulator(root)
root.mainloop()
exe下載連結:https://o365oid-my.sharepoint.com/:f:/g/personal/st110040_ms_edu_tw/EpmpR-D43uFLqvLE-MNrwkoB_sx5xiP_fpk9mJbHtRMY-w (請將程式本體與 _internal 放在桌面執行)
熱門評論