How to display and change an icon inside a python Tk Frame

Posted by codingJoe on Stack Overflow See other posts from Stack Overflow or by codingJoe
Published on 2011-03-19T04:23:15Z Indexed on 2011/03/19 8:10 UTC
Read the original article Hit count: 215

Filed under:
|

I have a python Tkinter Frame that displays several fields. I want to also add an red/yellow/green icon that will display the status of an external device.
The icon is loaded from a file called ICON_LED_RED.ico.

  1. How do I display the icon in my frame?
  2. How do I change the icon at runtime?
    for example replace BitmapImage('RED.ico') with BitmapImage('GREEN.ico')

    class Application(Frame):

    def init(self, master=None):

    Frame.__init__(self, master)
    self.pack()
    self.createWidgets()
    

    def createWidgets(self):

    #  ...other frame code.. works just fine.
    self.OKBTN = Button(self)
    self.OKBTN["text"] = "OK"
    self.OKBTN["fg"]   = "red"
    self.OKBTN["command"] =  self.ok_btn_func
    self.OKBTN.pack({"side": "left"})
    
    # when I add the following the frame window is not visible
    # The process is locked up such that I have to do a kill -9 
    self.statusFrame = Frame(self, bd=2, relief=RIDGE)
    Label(self.statusFrame, text='Status:').pack(side=LEFT, padx=5)
    self.statIcon = BitmapImage('data/ICON_LED_RED.ico')
    Label (self.statusFrame, image=self.statIcon ).grid()
    self.statusFrame.pack(expand=1, fill=X, pady=10, padx=5) 
    

© Stack Overflow or respective owner

Related posts about python

Related posts about tkinter