1,105,214 Community Members

Tkinter Time Converter

Member Avatar
Lyled93
Newbie Poster
1 post since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello all, trying to get my program working. The program is to take input from the user in a hh:mmx format (x being p or a, standing for PM and AM) and tell the user how many minutes from midnight that time is. I was able to make it so it would work in the Shell using the following code snippet:

def timedif(x):
    h = int(x[0:2])
    m = int(x[3:5])
    ampm = n[-1]
    if h != 12 and ampm == 'p':
        h = h + 12
    time = h*60 + m
    print time
n = raw_input('Input a time in hh:mmx format: ')
timedif(n)

Now I am trying to make it work in a Tkinter format. The following snippet is what I have so far. Wondering if you all could help me figure out how to get it working. Thanks!

import Tkinter

win = Tkinter.Tk()
win.title('Time Clock')
class Converter():
    def __init__ (self,x):
        self.x = x
    def convert(self,x):
        h = int(x[0:2])
        m = int(x[3:5])
        ampm = n[-1]
        if h != 12 and ampm == 'p':
            h = h + 12
        diff = h*60 + m
conv = Converter(5)    



label = Tkinter.Label(win,text="Calculate Elapsed Time in Minutes",font=('Courier New',32,'bold'))
label.pack()

Row3 = Tkinter.Frame(win)
mLabel = Tkinter.Label(Row3,text = "Enter Time (hh:mmx) With x = a or p",font=('Courier New',30))
mEntry = Tkinter.Entry(Row3,width=6,font=('Courier New',30))
mLabel.pack(side='left')
mEntry.pack(side='right')
Row3.pack()

Row4 = Tkinter.Frame(win)
vLabel = Tkinter.Label(Row4,text="Elapsed Minutes Since Midnight",font=('Courier New',30))
vLabel.pack()
Row4.pack()

Row5 = Tkinter.Frame(win)
qb = Tkinter.Button(Row5,text='Quit',command=win.destroy,font=('Courier New',30))
cb = Tkinter.Button(Row5,text='Convert',font=('Courier New',30))
qb.pack(side='left')
cb.pack(side='right')
Row5.pack()
win.mainloop()
Member Avatar
Schol-R-LEA
Nearly a Posting Virtuoso
1,384 posts since Oct 2010
Reputation Points: 635 [?]
Q&As Helped to Solve: 225 [?]
Skill Endorsements: 28 [?]
 
0
 

I was wondering why you weren't using the Time class out of the datetime module. It correctly handles a lot of what you are doing manually right now.

Member Avatar
woooee
Posting Maven
2,792 posts since Dec 2006
Reputation Points: 783 [?]
Q&As Helped to Solve: 836 [?]
Skill Endorsements: 12 [?]
 
0
 

You should send the time value entered to the class, so this statement should be in a function that is called when the "Convert" button is pressed
conv = Converter(5)
and should send the value from the Entry instead of "5" (links to using get to read an entry and callbacks for a button click

Next the class uses "n" which it doesn't have access to
ampm = n[-1]
You should be using "x" or even better self.x

The "Elapsed Minutes Since Midnight" should actually show something which requires a Tkinter variable

Finally you should allow for 2:2P instead of 02:02P which means you should split on the colon instead of the [0:2] and [3:5] slices

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article