【发布时间】:2019-10-13 13:46:16
【问题描述】:
我正在尝试从 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(): 的结果出现在消息框行中。如果结果不适合屏幕,则可滚动。
【问题讨论】:
-
test_function()中有什么?你在test_function()中使用return "your_text"吗?如果您想替换现有Label中的文本,请使用print_label["text"] = "your_text" -
嗨@furas - 感谢您的回复。我已经完成了一些工作,我已经接近了。我有从使用 listdir 定义的目录生成的文件名区域列表。我想重新格式化这些文件名,并在单击测试按钮时显示将要对这些文件名进行的标签中的更改。我使用 test_function 至少尝试在其中获取文本 - 我现在已经能够使用 textvariable 和 StringVar 获取一个文件名来显示,但是它不会显示所有文件的列表。
-
您是想说您需要一个多行的
Label小部件吗?如果是这样,您是否尝试过查看Message小部件? stackoverflow.com/questions/33568082/… -
如果显示有问题,请显示用于此目的的代码。
-
我再说一遍:
test_function()你有什么?你在test_function()中使用return "your_text"吗?显示有问题的test_function()代码 - 这是您的问题中更重要的元素。