0

I have created a simple app that looks into a certain folder and concatenates all files dependent on the user input (start; date1 and end date; date2). Here is a snippet of the code below;

root = Tk()

# main function to create and save a csv file

def csv(date1, date2, path_1, path_2):

    extension = 'txt'
    daterange = pd.date_range(date1, date2).tolist()
    daterange_list = [date.strftime('%d-%b-%Y') for date in daterange]

    progress['value'] = 5
    root.update_idletasks()
    time.sleep(5)

    all_filenames = []
    for i in range (len (daterange_list)):
        for j in glob.glob('{}2*{}*.{}'.format(path_1, daterange_list[i], extension)):
            all_filenames.append(j)

    progress['value'] = 30
    root.update_idletasks()
    time.sleep(5)

    csv_file = pd.concat([pd.read_csv(f, delimiter='\t') for f in all_filenames])

    progress['value'] = 60
    root.update_idletasks()
    time.sleep(5)

    csv_file.to_csv(os.path.join(path_2, '{}{}{}to{}{}{}.csv'.format(date1[0:2], date1[3:6], date1[-4:], date2[0:2], date2[3:6], date2[-4:])), index = False)

    progress['value'] = 100
    root.update_idletasks()
    time.sleep(5)

# select directory that contain all the text files to concatenate

def mydir():
    global work_dir
    work_dir = askdirectory(title='Select Working Directory Folder')
    myLabel = Label(root, text = work_dir)
    myLabel.grid(row = 0, column = 1)

# select directory to save the file
    
def mydir2():
    global down_dir
    down_dir = askdirectory(title='Select Download Folder')
    myLabel = Label(root, text = down_dir)
    myLabel.grid(row = 1, column = 1)

global progress

# create buttons and user input areas

button_dir = Button(root, text = "Select Battery Directory:", command = mydir)
button_down = Button(root, text = "Select Download Directory:", command = mydir2)
start_date = Entry(root, width = 20)
end_date = Entry(root, width = 20)
submit_button = Button(root, text = "Submit", command = lambda: csv(start_date.get(), end_date.get(), work_dir, down_dir))
progress = Progressbar(root, orient = HORIZONTAL, length=100, mode='determinate')

# create button layout

.........

root.mainloop()

The gui works perfectly for small amount of text files to concatenate, however if the number of files exceeds 30 files, the gui becomes unresponsive however the code still runs in the background.

I was thinking about implementing threads but i dont think it would help in this case? Especially if there are no background processes to be run.

Is there something that can be done to resolve this issue? Any help would be heaps appreciated as I am quite new to python.

heyooo678
  • 81
  • 1
  • 8
  • 1
    Calling `sleep()` or doing anything anything else time-consuming will interfere with the `mainloop()` and make your GUI freeze at least temporarily. See [Freezing/Hanging tkinter Gui in waiting for the thread to complete](https://stackoverflow.com/questions/53696888/freezing-hanging-tkinter-gui-in-waiting-for-the-thread-to-complete) for a workaround. – martineau Mar 10 '21 at 03:26
  • 1
    You really want separate threads or processes if you have significant work to do and want to still have a responsive GUI – RufusVS Mar 10 '21 at 03:27
  • 2
    You're adding 20 seconds of sleep. When the app is sleeping it can't do anything else, including refreshing the window. – Bryan Oakley Mar 10 '21 at 03:33
  • @BryanOakley, thanks for your comment. I have removed the sleep timers and the gui is still unresponsive for large number of files – heyooo678 Mar 10 '21 at 03:44
  • RufusVS and matineau, thanks for your comment, would threading work in this scenario considering that there are no background processes? – heyooo678 Mar 10 '21 at 03:45
  • 1
    Try using the Debugger for the program. For some of my projects running it by Debugger worked. – Bhavyadeep Yadav Mar 11 '21 at 05:29
  • 1
    @ARJ: Yes, as long as the other threads don't make any `tkinter` related calls. BTW I didn't see you comment until just now (because I looked on my own) because you didn't prefix my username with an `@` and I wasn't notified. – martineau Mar 12 '21 at 01:33
  • my apologies @martineau, considering i am a little new to threading in Python, are you able to link to a few more examples (that are a little easier to follow) so that i can convert my code? – heyooo678 Mar 12 '21 at 02:17
  • Here's a good example: [Trying to fix tkinter GUI freeze-ups (using threads)](https://stackoverflow.com/questions/53525746/trying-to-fix-tkinter-gui-freeze-ups-using-threads). – martineau Mar 12 '21 at 02:24

0 Answers0