简体   繁体   中英

How to add an icon passed as function parameter to button in TKinter

I'm trying to build my first python GUI, and I tried to add an icons to buttons, it's works when I add it directly but it give me "image doesn't exist' error when I pass the path throw a function

This is the worked code:


## This Works
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
window = tk.Tk()
window.title=("Editor")
#Higher header
header = tk.Frame(master = window, relief = tk.RAISED, width = 900, height = 100,  bg = "red")
header.pack(expand = True, fill = tk.BOTH)

# Create Header Buttons
# Save
saveL = tk.Label(master=header, width = 30, height=30 )
saveL.grid(row=0, column=1)
save_icon = PhotoImage(file="icon/image_file_checked_28px.png")
save_btn = tk.Button(master=saveL,image = save_icon  ,relief=RAISED)
save_btn.pack()


# Get
openL = tk.Label(master=header, width = 30, height=30 )
openL.grid(row=0, column=2)
open_icon = PhotoImage(file="icon/image_file_checked_28px.png")
open_btn = tk.Button(master=openL,image = open_icon  ,relief=RAISED)
open_btn.pack()


#Edit

Base = tk.Frame(master = window, width = 900, height = 500,  bg = "white")
Base.pack(expand = True,fill = tk.BOTH)
window.mainloop()

and this when I pass it to a function:

## Does not Work!

import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk

def header_button (col, fe):
    btn_label = tk.Label(master=header)
    btn_label.grid(row=0, column=col)
    btn_icon = PhotoImage(file=fe)
    btn = tk.Button(master=btn_label,image=fe,relief=RAISED,text="h")
    btn.pack()
    
window = tk.Tk()
window.title=("Editor")
#Higher header
header = tk.Frame(master = window, relief = tk.RAISED, width = 900, height = 100,  bg = "red")
header.pack(expand = True, fill = tk.BOTH)

# Create Header Buttons
# Save
header_button(1,"icon/image_file_add_28px.png")

# Get
header_button(2,"icon/image_file_checked_28px.png")

#Edit
header_button(3,"icon/photo_editor_28px.png" )

Base = tk.Frame(master = window, width = 900, height = 500,  bg = "white")
Base.pack(expand = True,fill = tk.BOTH)
window.mainloop()

This what I got from second code:

TclError                                  Traceback (most recent call last)
<ipython-input-3-d0aaa0c6893a> in <module>
     18 # Create Header Buttons
     19 # Save
---> 20 header_button(1,"icon/image_file_add_28px.png")
     21 
     22 # Get

<ipython-input-3-d0aaa0c6893a> in header_button(col, fe)
      7     btn_label.grid(row=0, column=col)
      8     btn_icon = PhotoImage(file=fe)
----> 9     btn = tk.Button(master=btn_label,image=fe,relief=RAISED,text="h")
     10     btn.pack()
     11 

~\anaconda3\lib\tkinter\__init__.py in __init__(self, master, cnf, **kw)
   2643             overrelief, state, width
   2644         """
-> 2645         Widget.__init__(self, master, 'button', cnf, kw)
   2646 
   2647     def flash(self):

~\anaconda3\lib\tkinter\__init__.py in __init__(self, master, widgetName, cnf, kw, extra)
   2565         for k, v in classes:
   2566             del cnf[k]
-> 2567         self.tk.call(
   2568             (widgetName, self._w) + extra + self._options(cnf))
   2569         for k, v in classes:

TclError: image "icon/image_file_add_28px.png" doesn't exist

I run the code in jupyter in diffrent cells and same file so it's impossible that the path has changed.

I tried to add 'r' or 'f' before the path and add to use btn.config(image=fe) instead set it up in btn = tk.Button(master=btn_label,image=fe,relief=RAISED,text="h") but that didn't work either. Also the text h didn't appear on the button.

its just seems to be a typo there.. in your first example you are using image = open_icon

open_icon = PhotoImage(file="icon/image_file_checked_28px.png")
open_btn = tk.Button(master=openL, image = open_icon  ,relief=RAISED)

In your second example you are using image=fe instead of image=btn_icon

btn_icon = PhotoImage(file=fe)
btn = tk.Button(master=btn_label,image=fe,relief=RAISED,text="h")

Update

To prevent image disappearing form the button

def header_button (col, fe):
    btn_label = tk.Label(master=header)
    btn_label.grid(row=0, column=col)
    btn_icon = PhotoImage(file=fe)
    btn = tk.Button(master=btn_label,image=btn_icon,relief=RAISED,text="h")
    btn.btn_icon = btn_icon
    btn.pack()
      

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM