执行按钮命令后更新 tkinter 标签的函数调用是否是意外行为的原因?

Is the function call to update a tkinter label after a button command was executed reason for unexpected behavior?

首先让我先说我大约 6 周前开始自学编码,所以很有可能,该方法可能仍然一团糟。

我在 cli 中有一个程序 运行(对我来说很好),但我想让它对晕倒的人有用,当他们看到黑色背景上的纯白色文本时,他们必须在没有操作的情况下进行操作一只老鼠。

除了我的主要 window,它是 运行 在后台,我想有一条消息 window,如果所有必要的文件都显示在 select编辑。如下所示。

files_to_open = {'File_1':'', 'File_2':'', 'File_3':''}

def selectfiles_window():
    global message_window
    message_window = Tk()

    ...

    content = Label(message_window, text=get_open_file_status_txt(), **txt_status_general)
    content.pack(side='top')
    button_select_file1 = Button(message_window,text = 'File 1',font = txt_general['font'],command = lambda:(
select_file_action(name='File 1', filetypename='Excel workbook', extension='*.xlsx'),
content.configure(text=get_open_file_status_txt())))
    button_select_file1(side='bottom')
    message_window.mainloop()

def select_file_action(name, filetypename, extension):
    global files_to_open
    files_to_open[name] = filedialog.askopenfilename(title = f'Select {name}', filetypes=[(filetypename, extension)])


def get_open_file_status_txt():
    global files_to_open
    message =f'''
    [File_1] is {"NOT SET" if (files_to_open["File_1"] == "") else "SET"}'''
    return message

我预计文本会在文件对话框关闭后更新(部分按预期工作)。

现在我不明白的是:如果我第一次单击 select File_1 按钮并取消它,键 File_1 的值设置为 ( ).此后任何时候,如果我单击 select File_1 按钮并取消它,键 File_1 的值将设置为“”。如果我 select 文件路径被正确保存为值(也是在第一次尝试时)。如果我取消它再次设置为 ''。

有人知道为什么在第一次取消时将值设置为 () 但之后按预期运行吗?

如果我的方法完全不对,我也将不胜感激更新文本的不同解决方案。

谢谢你,并致以最诚挚的问候, 托马斯

事实证明,函数调用不是问题,而是 filedialog.askopenfilename 的(对我来说很奇怪,但可能是故意的)行为,如果取消是 returns 一个空元组在第一次调用时选择,但在以后每次取消调用时都是空字符串。