Menu

sub2srt-python-GUI.py source

Bojan
#-------------------------------------------------------------------------------
# Name:        sub2srt-python-GUI
# Purpose:
#
# Author:      Bojan
#
# Created:     8:03 AM 7/7/2012
# Licence:     bsd
# Version:     1.0b
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import os
import subprocess
#suproces needed for open file, os for os.*
from tkinter import *
from tkinter import ttk
from tkinter import filedialog

#functions callable
def convert(*args):
    try:
        command_line = "python.exe sub2srt-python-command-line.py " + source_edit_variable.get() + " " + dest_edit_variable.get() + " " + fps_combobox_variable.get()
        if os.system(command_line) == 0:
            status['text'] = 'status = command completed succesfully'
        else:
            status['text'] = 'status = failed, sorry'
    except ValueError:
        pass

def find_sub_file():
    subloc = filedialog.askopenfilename( initialdir=os.path.expanduser('~'),filetypes=[('MicroDVD subtitle','*.sub')] )
    subloc = '"' + subloc +'"'
    source_edit_variable.set(subloc)
    dest_edit_variable.set(subloc.replace('.sub','.srt'))
    #set width. although different ending srt/sub, length is the same
    source_edit['width'] = len(subloc)
    dest_edit['width'] = len(subloc)

def save_as_srt_file():
    srtloc = filedialog.asksaveasfilename(initialdir=os.path.expanduser('~'),filetypes=[('Subrip subtitle','*.srt')]      )
    srtloc = '"' + srtloc +'"'
    dest_edit_variable.set(srtloc)
    dest_edit['width'] = len(srtloc)

def preview_movie():
    previewloc = source_edit_variable.get()
    previewloc = previewloc.replace('.sub','.avi')
    #os.startfile(previewloc)
    #start file win only
    if sys.platform.startswith('darwin'):
        subprocess.call(('open', previewloc))
    elif os.name == 'nt':
        os.startfile(previewloc)
    elif os.name == 'posix':
        subprocess.call(('xdg-open', previewloc))

#appwindow init
root = Tk()
root.title("Sub2srt-python-GUI")
#appwindow will expand
root.columnconfigure(0,weight=1)
root.rowconfigure(1,weight=1)

#frame init
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N,W,E,S))

#only middle frame.column will expand.
mainframe.columnconfigure(0, weight=0)
mainframe.columnconfigure(1, weight=1)
#mainframe.columnconfigure(1,minsize=330 )
mainframe.columnconfigure(2, weight=0)
mainframe.columnconfigure(3, weight=0)
mainframe.columnconfigure(4, weight=0)
#mainframe.rowconfigure(0, weight=1)

#for test
#mainframe["relief"]="sunken"

#elements init
ttk.Label(mainframe, text='Source (.sub file):').grid(column=0, row=1, sticky=W)
ttk.Label(mainframe, text='Destination (.srt file, auto name):').grid(column=0, row=2, sticky=W)
ttk.Label(mainframe, text='FPS:').grid(column=0, row=3, sticky=(W))

#we hold widget state in special variable, gui gets updated automatically. use get/set to read/write from it
source_edit_variable = StringVar()
#you can add width or height here:
source_edit = ttk.Entry(mainframe,  textvariable=source_edit_variable)
#if we use myedit = ttk.entru(..).grid(), myedit[width]=.. will show error. so we use separately, like next line
source_edit.grid(column=1, row=1, sticky=EW )

dest_edit_variable = StringVar()
dest_edit = ttk.Entry(mainframe,  textvariable=dest_edit_variable)
dest_edit.grid(column=1, row=2, sticky=EW )

fps_combobox_variable = StringVar()
#set init value
fps_combobox_variable.set('25')
#add combobox values, 25,23.976
fps_combobox = ttk.Combobox(mainframe,  textvariable=fps_combobox_variable, values=(25,23.976))
fps_combobox.grid(column=1, row=3, sticky=EW )

ttk.Button(mainframe, text="Find .sub file ...", command=find_sub_file).grid(column=2, row=1, sticky=E)
ttk.Button(mainframe, text="Save as .srt file ...", command=save_as_srt_file).grid(column=2, row=2, sticky=E)
ttk.Button(mainframe, text="Preview movie (to see fps)", command=preview_movie).grid(column=2, row=3, sticky=E)

ttk.Button(mainframe, text="Convert!", command=convert, padding =(40,10)).grid(column=2, row=4, sticky=E)

status = Label(mainframe, text="status = OK")
status.grid(column=0, row=5,sticky=SW,columnspan=3 )
#status["relief"]="sunken"

#neat it up
for child in mainframe.winfo_children(): child.grid_configure(padx=4, pady=4)

root.mainloop()

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.