如何让按钮点击结果显示在 FrameMain 而非 FrameSelect 中?
将 main.py 中创建的 frame_main 传给 FrameSelect,存为实例属性。在按钮回调中调用 self.frame_main.write_info_on_screen(self.file_path),结果控件就会以目标 FrameMain 为父容器。
为什么结果出现在 FrameSelect 中?
原来的调用是:
FrameMain.write_info_on_screen(self, file_path)
这里传入的 self 是 FrameSelect 实例。通过类名调用普通实例方法时,显式传入的第一个参数会成为方法中的 self。类名 FrameMain 不会自动找到 main.py 中创建的 frame_main 实例。
后续调用继续传递同一个对象:
FrameMain.count_number_of_files(self, file_path)
因此,执行 tk.Label(self, text=file_name) 时,父容器仍然是 FrameSelect,标签就出现在按钮所在的 Frame 中。
两个 Frame 都以 window 为父容器,是同级控件。master 指向父容器,super() 用于访问继承关系中的方法,二者都不能直接代替目标 frame_main 实例。
如何传递已有的 FrameMain 实例?
先创建并布局 frame_main,再将它传给 FrameSelect,在 FrameSelect.__init__() 中接收并保存为 self.frame_main。
问题描述中的代码缺少 Python 必需的缩进,下面的示例已补齐。
main.py
from customtkinter import CTk
from include.frame_select import FrameSelect
from include.frame_main import FrameMain
window = CTk()
window.geometry("600x800+100+100")
window.resizable(1, 1)
frame_main = FrameMain(window)
frame_main.grid(row=0, column=1, sticky="nw")
frame_select = FrameSelect(window, frame_main)
frame_select.grid(row=0, column=0, sticky="nw")
window.mainloop()
include/frame_select.py
from tkinter import filedialog
from customtkinter import CTkFrame, CTkLabel, CTkButton
class FrameSelect(CTkFrame):
def __init__(self, master, frame_main):
super().__init__(master)
self.configure(width=200)
self.frame_main = frame_main
self.file_path = ()
id_label = CTkLabel(
self,
text="Source file(s)",
font=("Arial", 18, "bold"),
)
id_label.grid(row=0, column=0)
button_select = CTkButton(
self,
text="Select",
command=self.select_file,
)
button_select.grid(row=1, column=0)
button_check = CTkButton(
self,
text="Check",
command=self.check_files,
)
button_check.grid(row=2, column=0)
def select_file(self):
self.file_path = filedialog.askopenfilenames(
parent=self.winfo_toplevel(),
title="Select a File",
initialdir=".",
filetypes=[("All files", "*.*")],
)
if self.file_path:
print(list(self.file_path))
def check_files(self):
if self.file_path:
self.frame_main.write_info_on_screen(self.file_path)
调用 self.frame_main.write_info_on_screen(...) 时,Python 会自动将目标实例 self.frame_main 作为方法的 self,因此只需传入文件路径。
文件选择结果保存在 self.file_path 中,可以去掉 global file_path。它的初始值是空元组,check_files() 会先判断它是否为空,因此尚未选择文件时点击 Check 不会调用显示方法。
include/frame_main.py
import tkinter as tk
from customtkinter import CTkFrame
class FrameMain(CTkFrame):
def __init__(self, master):
super().__init__(master)
self.configure(width=300)
def count_number_of_files(self, list_of_files):
for i, file_name in enumerate(list_of_files):
label = tk.Label(self, text=file_name)
label.grid(row=i, column=0, padx=10, pady=10, sticky="w")
def write_info_on_screen(self, file_path):
for widget in self.winfo_children():
widget.destroy()
self.count_number_of_files(file_path)
这里保留了原来的方法名,但 count_number_of_files() 实际上逐项显示文件路径。示例将每个路径放在单独一行,更新前会清除旧标签,避免重复点击时叠加控件。
修改时需要注意什么?
FrameSelect保存的是已有frame_main的引用,无需再创建一个FrameMain。新建的实例不会自动替换窗口中已经显示的实例。command=self.check_files传入的是回调函数。写成command=self.check_files()会在创建按钮时立即调用它。grid()返回None。如果之后需要访问按钮或标签,应像示例一样,将创建控件和调用grid()分成两条语句。- 示例中的
FrameMain只用于显示结果,因此更新时清除了全部子控件。如果以后加入固定标题或其他按钮,应单独管理结果标签,避免一起删除。
备注:内容仅供参考。
