将文本从函数打印到tkinter标签中

问题描述 投票:-1回答:1

我正在尝试从os.listdir命令获取文件列表以打印到tkinter标签中。我尝试了一些变通办法,但对我来说都没有任何变通办法。

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')

我正在寻找的是能够在单击按钮时有效运行该功能,并将结果打印在标签上。

print_label = tk.Label(print_frame, text="this works if I want to type soemthing in" bg='gray', font=('courier new',18))

但是,如果我要使用这样的东西:

print_label = tk.Label(print_frame, text=test_function, bg='gray', font=('courier new',18))

或:

print_label = tk.Label(print_frame, text=test_function(), bg='gray', font=('courier new',18))

我无法获得这些函数的结果以打印到为标签创建的预定义区域中。

我已经定义了这些功能,这是一个带有几个窗口的GUI菜单,所有的按钮单击等都可以正常工作-我只是想让我的信息显示出来,而似乎无法在那里显示它。

例如,我想在某个目录中列出文件名,所以我定义了一个listdir函数,该函数将打印给定目录中的文件。

单击按钮后,在python窗口中打印文件就很好了,但是一旦我尝试将这些结果打印在标签窗口中,它将无法正常工作。

我尝试使用get命令,但是,这可能是由于我未正确使用该命令导致的。因为它说该函数没有get属性。

我已经进行了更新,试图将结果显示在消息框中,就像在进一步研究后一样-建议使用显示此结果的列表:

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')

因此,我可以使用按钮来更改消息框中的文本。我要执行的操作是将print_filenames()的结果显示在行的消息框行中。如果结果不适合屏幕显示,则可以滚动。

python tkinter label
1个回答
0
投票

您必须从os.listdir()中获取字符串并连接成一个文本,然后放入LabelMessage

    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

最小工作示例

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()
© www.soinside.com 2019 - 2024. All rights reserved.