(May-30-2024, 09:10 AM)Gribouillis Wrote: It is a pretty common task. Can you post the code that currently reads the user input where you want to catch and correct the error?
The following code takes the user input only as HH:MM otherwise an error occurs and the program stops.
I would like to allow the user to be able to re-enter the input without the program terminating.
import tkinter as tk from tkinter import ttk from tkinter.messagebox import showinfo # root window root = tk.Tk() root.geometry("300x200") root.resizable(False, False) root.title('Time Clock') # store StartofWork address and EndofWork StartofWork = tk.StringVar() EndofWork = tk.StringVar() def login_clicked(): """ callback when the login button clicked """ WorkStart = StartofWork.get() tdelta = timediff() """ msg = f'Start of Shift: {StartofWork.get()} and End of Shift: {EndofWork.get()}' """ msg = f'Total Hours: {tdelta}' showinfo( title='Time Calculated', message=msg ) from datetime import datetime def timediff(): WorkStart = StartofWork.get() """StartTime = input("Enter Start Time in HH:MM\n") """ print("This is WorkStart", WorkStart) print("This is EndofWork", EndofWork.get()) FMT = '%H:%M' tdelta = datetime.strptime(EndofWork.get(), FMT) - datetime.strptime(StartofWork.get(), FMT) return tdelta # Sign in frame signin = ttk.Frame(root) signin.pack(padx=10, pady=10, fill='x', expand=True) # StartofWork StartofWork_label = ttk.Label(signin, text="Clock In:") StartofWork_label.pack(fill='x', expand=True) StartofWork_entry = ttk.Entry(signin, textvariable=StartofWork) StartofWork_entry.pack(fill='x', expand=True) StartofWork_entry.focus() # EndofWork password_label = ttk.Label(signin, text="Clock Out:") password_label.pack(fill='x', expand=True) password_entry = ttk.Entry(signin, textvariable=EndofWork) password_entry.pack(fill='x', expand=True) # login button login_button = ttk.Button(signin, text="Calculate Time", command=login_clicked) login_button.pack(fill='x', expand=True, pady=10) root.mainloop()
Larz60+ write Jun-07-2024, 11:25 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Tags have been added this post. Please use BBCode tags on future posts.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Tags have been added this post. Please use BBCode tags on future posts.