如何更改 python tkinter 中另一个文件的文本标签?
How can I change a text label from another file in python tkinter?
我希望你能帮助我,因为我被困在这个问题上。
我在 tkinter 上有一个应用程序,基本上我需要在按下按钮时执行另一个 python 文件中的函数并更改标签的文本。
让我解释一下:
我的前端文件名为 ODF_principal.py
,我的后端文件名为 ODF_second.py
,在后端文件中我有函数。
我在 ODF_principal
上将 ODF_second.py
导入为 OFU
以在按钮属性中设置 command
,如下所示:
btn_Open = Button(frame_menu, text="Open file", command=OFU.open_file)
btn_Open
属于前端
嗯,当我运行这个功能的时候,我需要在一个文件打开或关闭时,更改属于前端文件的标签的文本。
lbl_file = Label(frame_menu)
我尝试将前端文件导入后端,但出现循环导入错误,所以这是我需要的函数,它能够更改 lbl_file
的文本
opened_file_path=""
def open_file():
global opened_file_path
opened_file_path=filedialog.askopenfilename(initialdir = "/",
title = "Select files",filetypes = (("TXT files","*.txt"),
("All files","*.*")))
if not opened_file_path:
opened_file_path = ""
**OFR.lbl_file.config(text=opened_file_path)**
感谢您的宝贵时间。
您可以使用 lambda 函数实现此目的:
btn_Open = Button(frame_menu, text="Open file", command= lambda: OFU.open_file(lbl_file))
然后
def open_file(label):
global opened_file_path
opened_file_path=filedialog.askopenfilename(initialdir = "/",
title = "Select files",filetypes = (("TXT files","*.txt"),
("All files","*.*")))
if not opened_file_path:
opened_file_path = ""
label.config(text=opened_file_path)
lambda 函数允许您将对标签的引用传递给您在其他文件中的函数。
您可以通过 open_file
到 return 文件路径并更改前端中的标签来实现此目的。
def button_press():
file_open = OFU.open_file
my_label.configure(text=file_open)
btn_Open = Button(frame_menu, text="Open file", command=button_press)
和
def open_file():
opened_file_path=filedialog.askopenfilename(initialdir = "/",
title = "Select files",filetypes = (("TXT files","*.txt"),
("All files","*.*")))
if not opened_file_path:
opened_file_path = ""
return opened_file_path
这是@Henry 答案的替代方案,这意味着您的后端不必处理任何 GUI
我希望你能帮助我,因为我被困在这个问题上。 我在 tkinter 上有一个应用程序,基本上我需要在按下按钮时执行另一个 python 文件中的函数并更改标签的文本。
让我解释一下:
我的前端文件名为 ODF_principal.py
,我的后端文件名为 ODF_second.py
,在后端文件中我有函数。
我在 ODF_principal
上将 ODF_second.py
导入为 OFU
以在按钮属性中设置 command
,如下所示:
btn_Open = Button(frame_menu, text="Open file", command=OFU.open_file)
btn_Open
属于前端
嗯,当我运行这个功能的时候,我需要在一个文件打开或关闭时,更改属于前端文件的标签的文本。
lbl_file = Label(frame_menu)
我尝试将前端文件导入后端,但出现循环导入错误,所以这是我需要的函数,它能够更改 lbl_file
的文本
opened_file_path=""
def open_file():
global opened_file_path
opened_file_path=filedialog.askopenfilename(initialdir = "/",
title = "Select files",filetypes = (("TXT files","*.txt"),
("All files","*.*")))
if not opened_file_path:
opened_file_path = ""
**OFR.lbl_file.config(text=opened_file_path)**
感谢您的宝贵时间。
您可以使用 lambda 函数实现此目的:
btn_Open = Button(frame_menu, text="Open file", command= lambda: OFU.open_file(lbl_file))
然后
def open_file(label):
global opened_file_path
opened_file_path=filedialog.askopenfilename(initialdir = "/",
title = "Select files",filetypes = (("TXT files","*.txt"),
("All files","*.*")))
if not opened_file_path:
opened_file_path = ""
label.config(text=opened_file_path)
lambda 函数允许您将对标签的引用传递给您在其他文件中的函数。
您可以通过 open_file
到 return 文件路径并更改前端中的标签来实现此目的。
def button_press():
file_open = OFU.open_file
my_label.configure(text=file_open)
btn_Open = Button(frame_menu, text="Open file", command=button_press)
和
def open_file():
opened_file_path=filedialog.askopenfilename(initialdir = "/",
title = "Select files",filetypes = (("TXT files","*.txt"),
("All files","*.*")))
if not opened_file_path:
opened_file_path = ""
return opened_file_path
这是@Henry 答案的替代方案,这意味着您的后端不必处理任何 GUI