-1

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.

Graham
  • 17
  • 1
  • 7
  • what do you have in `test_function()` ? Do you use `return "your_text"` in `test_function()` ? And if you want to replace text in existing `Label` then use `print_label["text"] = "your_text"` – furas Oct 14 '19 at 07:25
  • Hi @furas - Thanks for the response. I've worked through some of it and i'm getting close. What I have area list of filenames which have been generated from a defined directory with listdir. I'm wanting to reformat those filenames and show the changes in the label that WILL be made do those filenames when the test button is clicked. I was using the test_function to at least try to get text in there - I've now been able to get one filename to show using the textvariable and StringVar, however it won't show the list of all the files. – Graham Oct 14 '19 at 08:59
  • Are you trying to say that you need a multi-line `Label` widget? If so, have you tried looking at the `Message` widget? https://stackoverflow.com/questions/33568082/tkinter-form-with-multi-line-labels – Ethan Field Oct 14 '19 at 14:03
  • if you have problem to display it then show code which you use for this. – furas Oct 14 '19 at 16:13
  • I say again: what do you have in `test_function()` ? Do you use `return "your_text"` in `test_function()` ? Show `test_function()` code in question - it is more important element in your problem. – furas Oct 14 '19 at 16:26
  • thanks @EthanField. I have updated the post looking at messagebox with StringVar. – Graham Oct 15 '19 at 05:21
  • @furas Thanks - I took a look at what Ethan had mentioned and can get the buttons to change what is displayed, however I cannot get the printed values to display in the message box. My code has been updated. – Graham Oct 15 '19 at 05:22
  • `print()` can display only on screen in console/terminal - so it useless for you. You have to get strings from `os.listdir()` and concatenate in one string which you put in variable using ie. `print_filename_test.set()` – furas Oct 15 '19 at 05:33

1 Answers1

0

You have to get strings from os.listdir() and concatenate in one text and then put in Label or Message

    filenames = sorted(os.listdir(ImageDirBT))
    text = "\n".join(filenames)
    #label['text'] = text # change text in Label
    print_filename_test.set(text) # change text in Message

Minimal working example

import os
import tkinter as tk

def get_filenames():
    filenames = sorted(os.listdir('.'))
    text = "\n".join(filenames)
    label['text'] = text # change text in label


root = tk.Tk()

label = tk.Label(root) # empty label 
label.pack()

tk.Button(root, text="OK", command=get_filenames).pack()

root.mainloop()
furas
  • 134,197
  • 12
  • 106
  • 148
  • further to this, I am basically looking to from command line to GUI and it seems there are a lot of changes and things I need to learn. I'm finding where I used splitext to rearrange filenames, search for parts of those filenames in a CSV and rename the file in a convention that i need, doesn't seem possible once these files are in a list. Could I trouble you to let me know the module I should read up on/practice for altering strings within a list? – Graham Oct 16 '19 at 07:12
  • you don't need any module but `for`-loop to get string from list, standard string functions to change this strings and `append()` to put string in new list with strings. – furas Oct 16 '19 at 13:03