I'm trying to get a list of files from an os.listdir command to print into a tkinter label. I have tried a few workarounds, nothing of which has worked out for me.
def booth_bcode_test_window():
booth_bcode_test_window = tk.Toplevel(MainMenu, width=print_width, height=print_height)
booth_bcode_test_window.title("BARCODE TEST")
print_frame = tk.Frame(booth_bcode_test_window, bg='black', bd=5)
print_frame.place(relx=0.5, rely=0.1, relwidth=0.9, relheight=0.8, anchor='n')
print_label = tk.Label(print_frame, bg='gray', font=('courier new',18))
print_label.place(relwidth=1, relheight=1)
confirm_button = tk.Button(booth_bcode_test_window, text="Rename Files", width=10, command=test_click)
confirm_button.place(relx=0.5, rely=0.93, anchor='n')
cancel_button = tk.Button(booth_bcode_test_window, text="Cancel", width=10, command=booth_bcode_test_window.destroy)
cancel_button.place(relx=0.62, rely=0.93, anchor='n')
test_button_tbar = tk.Button(booth_bcode_test_window, text="Run Test", width=10, command=print_test)
test_button_tbar.place(relx=0.38, rely=0.93, anchor='n')
What I'm looking for is to be able to effectively have the function run when the button is clicked, printing the results in the label.
print_label = tk.Label(print_frame, text="this works if I want to type soemthing in" bg='gray', font=('courier new',18))
However, if I'm to use something like this:
print_label = tk.Label(print_frame, text=test_function, bg='gray', font=('courier new',18))
or:
print_label = tk.Label(print_frame, text=test_function(), bg='gray', font=('courier new',18))
I can't get the results of those functions to print into the predefined area I've created for the label.
I have defined those functions, it's a GUI menu with a few windows and all the button clicks etc work fine - I'm just trying to get my info displayed and can't seem to get it up there.
I would like, for example, I am trying to list filenames in a certain directory, so I have defined a listdir function that prints the files in the given directory.
When the button is clicked, the files print in the python window just fine, but as soon as I try to have those results printed in the label window, it won't work.
I have tried using the get command, however, it could be a result of me not using that properly. as it says the function has no get attribute.
I have updated trying to get the results to show in a message box, as after further research - to get a list to show this was the advised way:
def print_filenames():
for f in sorted(os.listdir(ImageDirBT)):
print(f)
def test_function():
print_filename_test.set("File list...")
def confirm_function():
print_filename_test.set("Renaming the files...")
def bcode_test_window():
bcode_test_window = tk.Toplevel(MainMenu, width=print_width,
height=print_height)
bcode_test_window.title("BARCODE TEST")
print_frame = tk.Frame(bcode_test_window, bg='black', bd=5)
print_frame.place(relx=0.5, rely=0.1, relwidth=0.9, relheight=0.8, anchor='n')
print_text = tk.Message(print_frame,
bg='gray',textvariable=print_filename_test, anchor='nw', font=('courier new',11), width=1100)
print_text.place(relwidth=1, relheight=1)
confirm_button = tk.Button(bcode_test_window, text="Rename Files", width=10, command=lambda: confirm_function())
confirm_button.place(relx=0.5, rely=0.93, anchor='n')
cancel_button = tk.Button(bcode_test_window, text="Cancel", width=10, command=bcode_test_window.destroy)
cancel_button.place(relx=0.62, rely=0.93, anchor='n')
test_button_tbar = tk.Button(bcode_test_window, text="Run Test", width=10, command=lambda: test_function())
test_button_tbar.place(relx=0.38, rely=0.93, anchor='n')
So I can get the buttons to change the text in the messagebox. What I'm looking to do is have the results of print_filenames(): appear in the messagebox line for line. Scrollable if the results don't fit on screen.