首页 > 解决方案 > 将函数中的文本打印到 tkinter 标签中

问题描述

我正在尝试从 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(): 的结果出现在消息框行中。如果结果不适合屏幕,则可滚动。

标签: pythontkinterlabel

解决方案


您必须从一个文本中获取字符串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()

推荐阅读