You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Tkinter窗口左上角与标题同行添加Logo图片?

问题解决与实现方案

错误原因

Tkinter原生的PhotoImage仅支持PNG、GIF格式,若你的图片是JPG/JPEG等其他格式,或文件损坏,就会触发加载失败的错误。另外,若图片对象未被类属性引用,Python垃圾回收机制会自动回收它,导致图片无法显示。

解决方案

情况1:图片为PNG/GIF格式

直接调整代码,将图片绑定为类属性,并通过Frame布局实现Logo与标题左对齐同行:

def __init__(self, root):
    self.root = root
    self.root.title("Data Wizard: Automated Data Preprocessing")
    self.root.geometry("800x600")
    self.root.configure(bg='#005792')
    
    # 顶部布局:用Frame包裹Logo和标题,实现左对齐同行
    self.top_frame = tk.Frame(self.root, bg='#005792')
    self.top_frame.pack(anchor='w', pady=20, padx=20)  # anchor='w' 实现靠左对齐
    
    # 加载Logo并绑定为类属性,避免被垃圾回收
    self.logo_img = tk.PhotoImage(file="C:/Users/pdhin/Minor 2/Logo_New.png")
    # 创建Logo标签
    self.logo_label = tk.Label(self.top_frame, image=self.logo_img, bg='#005792')
    self.logo_label.pack(side=tk.LEFT, padx=(0, 10))  # 与标题保持10像素间距
    
    # 创建标题标签
    self.title_label = tk.Label(self.top_frame, text="Data Wizard: Automated Data Preprocessing", 
                                font=("Times New Roman", 22), bg='#005792', fg='#7dd8c5')
    self.title_label.pack(side=tk.LEFT, fill=tk.Y, expand=True)  # 标题垂直居中
    
    # 以下为原有代码,无需修改
    # Middle layout with table frame
    self.table_frame = tk.Frame(self.root, bd=2, relief=tk.GROOVE)
    self.table_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=10)

    # Create a treeview widget to display table
    self.tree = ttk.Treeview(self.table_frame, selectmode='browse')
    self.tree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

    # Add scrollbar to the treeview widget
    scrollbar = ttk.Scrollbar(self.table_frame, orient="vertical", command=self.tree.yview)
    scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
    self.tree.configure(yscrollcommand=scrollbar.set)

    # Bottom layout with Upload CSV file label and button
    self.bottom_frame = tk.Frame(self.root, bd=2, relief=tk.GROOVE)
    self.bottom_frame.pack(fill=tk.X, padx=20, pady=10)
    self.bottom_frame.configure(bg='#C3C3C3')

    self.upload_label = tk.Label(self.bottom_frame, text="Upload CSV File", font=("Arial", 12))
    self.upload_label.pack(side=tk.LEFT, padx=10, pady=10)
    self.upload_label.configure(bg='#C3C3C3', fg='#0b1956')

    self.upload_button = tk.Button(self.bottom_frame, text="Browse", command=self.upload_csv)
    self.upload_button.pack(side=tk.RIGHT, padx=10, pady=10)

    self.new_frame = tk.Frame(self.root, bd=0, relief=tk.GROOVE)
    self.new_frame.pack(fill=tk.X, padx=20, pady=10)
    self.new_frame.configure(bg='#005792')
    
    # Add button to open a new window
    self.new_window_button = tk.Button(self.new_frame, text="Next", command=self.open_new_window)
    self.new_window_button.pack(side=tk.RIGHT, padx=10, pady=10)

情况2:图片为JPG/JPEG等其他格式

需要借助PIL库(先执行pip install pillow安装),将图片转换为Tkinter支持的格式:

from PIL import Image, ImageTk

def __init__(self, root):
    self.root = root
    self.root.title("Data Wizard: Automated Data Preprocessing")
    self.root.geometry("800x600")
    self.root.configure(bg='#005792')
    
    # 顶部布局:用Frame包裹Logo和标题,实现左对齐同行
    self.top_frame = tk.Frame(self.root, bg='#005792')
    self.top_frame.pack(anchor='w', pady=20, padx=20)
    
    # 用PIL加载图片并转换为Tkinter格式,绑定为类属性
    img = Image.open("C:/Users/pdhin/Minor 2/Logo_New.png")
    # 若Logo尺寸过大,可添加resize调整:img = img.resize((50, 50), Image.Resampling.LANCZOS)
    self.logo_img = ImageTk.PhotoImage(img)
    # 创建Logo标签
    self.logo_label = tk.Label(self.top_frame, image=self.logo_img, bg='#005792')
    self.logo_label.pack(side=tk.LEFT, padx=(0, 10))
    
    # 创建标题标签
    self.title_label = tk.Label(self.top_frame, text="Data Wizard: Automated Data Preprocessing", 
                                font=("Times New Roman", 22), bg='#005792', fg='#7dd8c5')
    self.title_label.pack(side=tk.LEFT, fill=tk.Y, expand=True)
    
    # 以下原有代码保持不变...

关键说明

  • 用Frame包裹Logo和标题,通过anchor='w'让整个顶部布局靠左对齐,实现左上角的展示效果。
  • 将图片对象赋值给self.logo_img(类属性),避免被Python垃圾回收机制回收,确保图片正常显示。
  • 若Logo尺寸不合适,可通过PIL的resize方法调整大小后再转换格式。

内容的提问来源于stack exchange,提问作者Param Dhingana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 00:57:00