Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Changing Tkinter label while app is still running

Could not find exact answer to my question in other posts. What I am looking for is the way to update my label while my other function is running. I tried to first change the label and then call the my_function(), but still label is not updating however my_function() is running and printing results in terminal. I am totally new to Tkinter and as far as I understood, while we do not hit window.mainloop() my label would not update. Is there any methods to update the label while other function is running?

import os
import tkinter as tk
from tkinter import END, Label, Scrollbar, Text, filedialog

def my_function(directory: str) -> None:
    for item in os.scandir(directory):
        if item.is_file:
            # print(f'File name: {item.name}')
            text_area.insert(END, f'File name: {item.name}\n')


def select_folder() -> None:
    '''
    Tkinter function for button,
    user can select folder.
    '''

    path = filedialog.askdirectory()
    status.config(text='Status: Work in progress, please wait!')
    text_area.insert(END, 'Visited folders:\n')
    my_function(path)
    status.config(text='Status: Done!')


# Start the app window
window = tk.Tk()
window.title('PDF maker')
window.geometry('400x400')

# Status Label
status = Label(window, text='Status: Select the folder')
status.pack()

# Button for selecting folder
button = tk.Button(window, text='Select Folder', command=select_folder)
button.pack(side='bottom', pady=30)

# Horizontal and Vertical Scrollbars
v_s = Scrollbar(window)
v_s.pack(side='right', fill='y')

h_s = Scrollbar(window, orient='horizontal')
h_s.pack(side='bottom', fill='x')

# Text area for result output
text_area = Text(
    window,
    wrap='none',
    font=('Times New Roman', 13),
    yscrollcommand=v_s.set,
    xscrollcommand=h_s.set
)
text_area.pack(padx=10, pady=10, expand=True, fill='both')

# Adding scrollability to text
v_s.config(command=text_area.yview)
h_s.config(command=text_area.xview)

window.mainloop()

Update

Or should I create 2 functions for one button? First function will change the label and text, and second function will run my_func().

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

As so often, the answer is very simple.

Try this:

#...
def select_folder() -> None:
    """
    Tkinter function for button,
    user can select folder.
    """
    status.config(text="Status: Work in progress, please wait!") # switched lines here
    path = filedialog.askdirectory()
    text_area.insert(END, "Visited folders:\n")
    my_function(path)
    status.config(text="Status: Done!")

#...

Previously it did not work as intended, because filedialog.askdirectory() blocks the program-flow ( similarly to input() ).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading