We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,737 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Tkinter Time Converter

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()
3
Contributors
2
Replies
7 Hours
Discussion Span
6 Months Ago
Last Updated
3
Views
Lyled93
Newbie Poster
1 post since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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.

Schol-R-LEA
Veteran Poster
1,020 posts since Oct 2010
Reputation Points: 414
Solved Threads: 163
Skill Endorsements: 10

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

woooee
Posting Maven
2,703 posts since Dec 2006
Reputation Points: 827
Solved Threads: 779
Skill Endorsements: 9

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0606 seconds using 2.7MB