将函数中的文本打印到tkinter lab中

2024-09-25 02:34:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从一个操作系统列表目录命令打印到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窗口中打印得很好,但只要我尝试在label窗口中打印这些结果,就行不通了。你知道吗

我试过使用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')

这样我就可以得到按钮来更改messagebox中的文本。我想做的是让print\u filenames()的结果显示在messagebox中的一行一行。可滚动,如果结果不适合在屏幕上。你知道吗


Tags: texttestplacebuttonwindowwidthframetk
1条回答
网友
1楼 · 发布于 2024-09-25 02:34:22

您必须从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()

相关问题 更多 >