Python3 Script to convert seconds to hours

  • Post author:
  • Post category:Python

script to convert seconds to hours using Python 3 simple command just copy and paste in your terminal and run it.


#("Times", "24", "bold italic")
#("Helvetica", "16")
import tkinter
 
class Converter():
    def __init__(self):
 
        self._a = tkinter.Tk()
        self._a.title('Seconds Coneverter')
        tkinter.Label(master = self._a, text = 'Seconds: ').grid(row = 0,column =0,sticky = tkinter.W)
        tkinter.Label(master = self._a, text = 'Converted Time:\n (H:M:S) ').grid(row = 1,column =0,sticky = tkinter.W)
 
        tkinter.Button(master = self._a,text = 'Calculate',
                       font= ('Helvetic',15),command = self.ptext).grid(row = 2,column =0,
                                                                       sticky = tkinter.SW)
        tkinter.Button(master = self._a,text = 'Quit',
                       font= ('Helvetic',15),command = self.destroy).grid(row = 2,column =2,
                                                                       sticky = tkinter.SE)
        self._l = tkinter.Label(master = self._a, textvariable = '')
        self._l.grid(row = 1,column =1)
 
 
        
        self._s = tkinter.StringVar()
        e = tkinter.Entry(master = self._a , textvariable = self._s)
        e.grid(row=0,column=1)
      
        
        
    def run(self):
        self._a.mainloop()
 
 
    def ptext(self):
        self._s2 = tkinter.StringVar()
        self._l.destroy()
        try:
            self._text = int(self._s.get())
            self._hour = self._text //(60**2)
            self._minute = self._text%3600 //(60)
            self._second = self._text%3600%60
            self._s2.set(str(self._hour).zfill(2)+':'+
                         str(self._minute).zfill(2)+':'+
                         str(self._second).zfill(2))
        except:
            self._s2.set('Invalid Input')
        finally:
            self._l = tkinter.Label(master = self._a, textvariable = self._s2)
            self._l.grid(row = 1,column =1)
 
    def destroy(self):
        return self._a.destroy()
 
Converter().run()