0
from tkinter import filedialog, font
from  tkinter import *
from tkinter import ttk
root = Tk()
root.geometry("300x100")
root.title("SRT")
root.resizable(False, False)
frame = Frame(root)
frame.grid(column=1, row =0)
def open():
    b.destroy()
    filename1 =  filedialog.askopenfilename(filetypes=[("SRT files(*.srt)", "*.srt")],initialdir = "/", title = "Select file")
    root.geometry("775x300")
    k=Label(frame,text="SELECTED SUBTITLE",font=("Times New Roman", 15)).grid(column=3, row =0)
    y=Label(frame,text=filename1,font=("Times New Roman", 12)).grid(column=3, row =1)
    seconds=Label(root, font=("Times New Roman", 15),text="Seconds").grid(column=2,row=2)
    minutes=Label(root, font=("Times New Roman", 15),text="Minutes").grid(column=0,row=2,ipadx=20)
    minuteselect = StringVar()
    minutes = ttk.Combobox(root, textvariable=minuteselect)
    minutes['values']=tuple([i for i in range(1,61)])
    minutes['state'] = 'readonly' 
    minutes.grid(column=0,row=4,padx=10)
    secondselect = StringVar()
    seconds = ttk.Combobox(root, textvariable=secondselect)
    seconds['values']=tuple([i for i in range(1,61)])
    seconds['state'] = 'readonly' 
    seconds.grid(column=2,row=4)
    plus =Button(root,text="DECREASE", width='10', height='1').place(x=400,y=150)
    minus =Button(root,text="INCREASE", width='10', height='1').place(x=280,y=150)
b =Button(frame,text="Select The Subtitle", width='30', height='1',command=open)
b.grid(row=1, column=1,padx=41,pady=30)
b.rowconfigure(1, weight=1)
b.columnconfigure(1, weight=1)
root.mainloop()

I'm trying to build an app which synchronizes an srt file and creates a new file after syncing the time of the subtitle file from the users input. I am currently stuck at how to retrieve the data from the two Combobox and and process the file according to the input from the user. In my code i have tried to input a file directly and opened a second window which gives the options minutes and seconds using 2 comboboxes and two buttons INCREASE and DECREASE, If the increase is clicked it would redirect to a function which gets the input from the combobox and increases the time in the srt file using the fuctions provided py the module pysrt and vice versa if i click decrease. I am currently stuck at how to get the two inputs: (1)time in seconds and minutes and(2) Increase or Decrease Button and call the functions accordingly.

1 Answers1

0

First thing I would do is restructure the code you have. You are doing everything inside the open function you have, which complicates your life. Also, you remove the possibility for the user to change the .srt file used for the conversion.

I would use 1 form to contain all the controls.

  1. Button to select .srt file
  2. Combobox for the minutes
  3. Combobox for the seconds
  4. Save button

Then put the actual magic on the event that triggers when the Save button I suggested is pressed. Here you just get the values from the 2 comboboxes and use this to offset the subtitles based on these values.

Now, this doesn't actually properly answer your question, it's how I handled the exact same app long time ago. Sub timings can really s*ck donkeyballs :-|

To actually answer your question, if you want to use buttons and comboboxes, the required logic changes a bit. You will need to create global variables to hold the minutes and seconds, and take into account that if the seconds are set to 59 and the user presses the INCREASE button for seconds, the seconds should be reset to 0 and minutes should be increased by 1. Of course the inverse goes for the DECREASE. (1m0s --> 0m59s).

The logic for the buttons should go to separate functions, just as you did for the open button & function. Bind them just as you did for the open fucntion using the command keyword when instantiating them. Put logic in there to update the global minute & second variables, and use combobox.set(value) (docs) to update the combobox values.

You might also want to bind events to the comboboxes themselves being changed. See the pydocs and this excellent answer from nbro

Finally, you still need a Save button to actually update the values in the .srt file.

Some side-notes:

  • The range you use for the minutes & seconds seems incorrect to me. I would use range(0,60) as you need the option for 0 and you do not need the option for 60 (as this would become 1 hour or 1 minute respectively. Considering you wanna fix timings for subtitles, using hours seems preposturous. Using milliseconds would seem usefull though.
  • You should be able to handle negative values. Subtitles are either early or late, and with the current logic, you can only handle the situation where the subtitles are early.
Edo Akse
  • 4,051
  • 2
  • 10
  • 21