geekcomputers/Python · warning · ValueError

Format must be HH:MM:SS

Error message

Format must be HH:MM:SS

What it means

This ValueError is raised by the Tkinter calculate callback in this clock-difference tool when one of the two time strings is not exactly in HH:MM:SS form: the string must be 8 characters long with ':' at positions 2 and 5 (e.g., '09:30:00'). It is a strict shape check performed before the hour/minute/second components are parsed, and the caught exception is shown to the user via messagebox.showerror.

Source

Thrown at Timetable_Operations.py:42

    sec1 = h1 * 3600 + m1 * 60 + s1
    sec2 = h2 * 3600 + m2 * 60 + s2
    diff = sec2 - sec1
    if diff < 0:
        diff += 24 * 3600
    h = diff // 3600
    m = (diff % 3600) // 60
    s = diff % 60
    return f"{h:02}:{m:02}:{s:02}"


def calculate() -> None:
    """Tkinter callback to calculate and display clock difference."""
    t1 = entry_t1.get().strip()
    t2 = entry_t2.get().strip()
    try:
        for t in [t1, t2]:
            if len(t) != 8 or t[2] != ":" or t[5] != ":":
                raise ValueError("Format must be HH:MM:SS")
            h, m, s = int(t[0:2]), int(t[3:5]), int(t[6:8])
            if not (0 <= h < 24 and 0 <= m < 60 and 0 <= s < 60):
                raise ValueError("Time out of range")
        result = clock_diff(t1, t2)
        label_result.config(text=f"Difference: {result}")
    except Exception as e:
        messagebox.showerror("Error", f"Invalid input!\n{e}")


root = tk.Tk()
root.title("Clock Difference Calculator")
root.geometry("300x200")

tk.Label(root, text="Init schedule (HH:MM:SS):").pack(pady=5)
entry_t1 = tk.Entry(root)
entry_t1.pack()

tk.Label(root, text="Final schedule (HH:MM:SS):").pack(pady=5)

View on GitHub (pinned to 40f4cd2652)

Solutions

  1. Enter both times in exact HH:MM:SS format with leading zeros, e.g., 09:30:00
  2. Normalize input before submission: pad components with zfill(2) and validate with a regex like ^\d{2}:\d{2}:\d{2}$
  3. Do not paste times with extra characters or spaces

Example fix

# before
entry_t1.get() == '9:30:00'  # triggers error

# after
import re
raw = entry_t1.get().strip()
parts = raw.split(':')
raw = ':'.join(p.zfill(2) for p in parts)
assert re.fullmatch(r'\d{2}:\d{2}:\d{2}', raw)
Defensive patterns

Strategy: validation

Validate before calling

import re
TIME_RE = re.compile(r'^\d{2}:\d{2}:\d{2}$')
t1, t2 = entry_t1.get().strip(), entry_t2.get().strip()
if all(TIME_RE.fullmatch(t) for t in (t1, t2)):
    calculate()

Type guard

def is_hhmmss(t: str) -> bool:
    import re
    return bool(re.fullmatch(r'\d{2}:\d{2}:\d{2}', t))

Try / catch

try:
    calculate()
except ValueError as e:
    messagebox.showerror('Invalid input', str(e))

Prevention

When it happens

Trigger: Entering '9:30:00' (missing leading zero), '09:30' (no seconds), '09-30-00', an empty field, or extra whitespace beyond what .strip() removes (e.g., '09:30:00 '). Any input where len(t) != 8 or the colons are misplaced triggers it.

Common situations: Users typing times without zero-padding, pasting times from other apps with different formats, omitting seconds, or leaving a field blank; also locales that produce '9:5:0'.

Related errors


AI-assisted analysis of geekcomputers/Python@40f4cd2652 (2026-08-27). Data as JSON: /api/errors/41a925e6989d056d. Report an issue: GitHub.