Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Input error correction.
#4
Please post code using the bbtags. Makes it easier to help.

Is this what you are trying for?
Added a try block

import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo, showerror
from datetime import datetime

# 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()

    if tdelta:
        """
        msg = f'Start of Shift: {StartofWork.get()} and End of Shift: {EndofWork.get()}'
        """
        msg = f'Total Hours: {tdelta}'
        showinfo(
        title='Time Calculated',
        message=msg
        )


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'
    try:
        tdelta = datetime.strptime(EndofWork.get(), FMT) - datetime.strptime(StartofWork.get(), FMT)
        return tdelta
    except ValueError:
        showerror('Error!', 'Time format is not correct.')

# 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() 
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Messages In This Thread
Input error correction. - by shakoun - May-30-2024, 08:51 AM
RE: Input error correction. - by Gribouillis - May-30-2024, 09:10 AM
RE: Input error correction. - by shakoun - Jun-07-2024, 10:00 AM
RE: Input error correction. - by menator01 - Jun-07-2024, 11:26 AM
RE: Input error correction. - by menator01 - Jun-07-2024, 12:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Error in Using INPUT statement gunwaba 1 2,250 Jul-03-2022, 10:22 PM
Last Post: deanhystad
Star I'm getting syntax error while using input function in def. yecktmpmbyrv 1 2,100 Oct-06-2021, 09:39 AM
Last Post: menator01
  output correction using print() function afefDXCTN 3 11,448 Sep-18-2021, 06:57 PM
Last Post: Sky_Mx
  Pyspark SQL Error - mismatched input 'FROM' expecting <EOF> Ariean 3 48,674 Nov-20-2020, 03:49 PM
Last Post: Ariean
  EOF error while taking input ShishirModi 1 2,698 Sep-27-2020, 11:28 AM
Last Post: jefsummers
  Input Error Dream 2 2,577 Jul-12-2020, 05:41 PM
Last Post: bowlofred
  Getting an error while using input function dcsethia 5 3,116 May-11-2020, 04:59 PM
Last Post: buran
  Spelling correction jareyesluengo 0 1,505 Apr-07-2020, 02:44 PM
Last Post: jareyesluengo
  catch input type error mcmxl22 5 3,271 Aug-11-2019, 07:33 AM
Last Post: wavic
  inserting input gives me error message RubberNuggets 3 2,733 Jan-15-2019, 06:17 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020