【问题标题】:Tkinter add icon to custom title-bar (overridden)Tkinter 将图标添加到自定义标题栏(覆盖)
【发布时间】:2021-12-19 20:59:42
【问题描述】:

我想创建一个自定义标题栏。但是我的图标有问题。 我希望它被留下,但它只是在窗口中......

我的模板来自这里:https://github.com/Terranova-Python/Tkinter-Menu-Bar/blob/main/main.py

我尝试添加图片的代码:

from PIL import Image, ImageTk

img  = Image.open("M.ico") 
photo=ImageTk.PhotoImage(img)

close_button = Button(title_bar, text='  ×  ', command=root.destroy,bg=RGRAY,padx=2,pady=2,font=("calibri", 13),bd=0,fg='white',highlightthickness=0)
expand_button = Button(title_bar, text=' ???? ', command=maximize_me,bg=RGRAY,padx=2,pady=2,bd=0,fg='white',font=("calibri", 13),highlightthickness=0)
minimize_button = Button(title_bar, text=' ???? ',command=minimize_me,bg=RGRAY,padx=2,pady=2,bd=0,fg='white',font=("calibri", 13),highlightthickness=0)
title_bar_image = Label(image=photo,bg=RGRAY,padx=2,pady=2,bd=0)
title_bar_title = Label(title_bar, text=tk_title, bg=RGRAY,bd=0,fg='white',font=("helvetica", 10),highlightthickness=0)

我希望有人可以帮助我:)

【问题讨论】:

  • 我没完全明白你在问什么,你想设置窗口图标,对吧?
  • 通常它看起来像这样:https://i.imgur.com/WfBmgDe.png 但是我覆盖了标题栏,所以我可以创建自己的,但我不知道如何将图像留给标题“App”
  • 是不是因为你需要把图片作为父(锚)到title_bar?
  • Ups 是的,谢谢 xD title_bar_image = Label(title_bar, image=photo, width=25, height=25, bg=RGRAY, bd=0) 修复它

标签: python tkinter customization titlebar


【解决方案1】:

要获得官方答案,问题是title_bar_image 没有锚定到title_bar。修改后的代码 sn -p 现在是

from PIL import Image, ImageTk

img  = Image.open("M.ico") 
photo=ImageTk.PhotoImage(img)

close_button = Button(title_bar, text='  ×  ', command=root.destroy,bg=RGRAY,padx=2,pady=2,font=("calibri", 13),bd=0,fg='white',highlightthickness=0)
expand_button = Button(title_bar, text=' ? ', command=maximize_me,bg=RGRAY,padx=2,pady=2,bd=0,fg='white',font=("calibri", 13),highlightthickness=0)
minimize_button = Button(title_bar, text=' ? ',command=minimize_me,bg=RGRAY,padx=2,pady=2,bd=0,fg='white',font=("calibri", 13),highlightthickness=0)
title_bar_image = Label(title_bar, image=photo,bg=RGRAY,padx=2,pady=2,bd=0)
title_bar_title = Label(title_bar, text=tk_title, bg=RGRAY,bd=0,fg='white',font=("helvetica", 10),highlightthickness=0)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多