pythontkinterttkttkwidgets

python tkinter update 'tab' header dynamically


I have a GUI program which updates labels dynamically. Since I have multiple tabs with different amount of labels, I have made it dynamically, so I can reuse this code for all the information returned by different functions. Now, as an addition I would like to set the background of the 'tab' header either red or green...based on the result. I'm fine with a png image to do this, but I can't figure out how to 'config' the frame at runtime.

def updateAnswers(self,info,thisTab):
        childCounter=0
        counter =0
        
        for i in self.winfo_children():
            print('This is a: '+str(i.winfo_class))
            if childCounter == thisTab:
                ### testing with updating tab
                
                for child in i.winfo_children():
                    labelObject=child.grid_info()
                    if labelObject['column']==1:
                        if info == False:
                            child.config(text='Test running')
                            counter=counter+1
                        else:
                            child.config(text=info[counter])
                            counter=counter+1
                childCounter=childCounter+1
            else:
                childCounter=childCounter+1 

the function itself works as expected. the line:

print('This is a: '+str(i.winfo_class))

is there for 'debug' to see if it's a frame object, from NoteBook...

Of course I can set the color/ image when the frame/notebook is created, but I want to be able to change it at runtime....

to be clear, different tabs shall have different colors if needed...

UPDATED

When adding the following code:

def updateAnswers(self,info,thisTab):
        childCounter=0
        counter =0
        ph=tk.PhotoImage(file='images/checkmark.png')
   self.tab(thisTab,text='Tab'+str(thisTab),image=ph,compound=tk.LEFT)

        for i in self.winfo_children():

        rest of code

the text of the tab is updated, however, picture / photo is not shown.

thanks!


Solution

  • Solution found!

    It was the "Garbage Colletion" of python which destroyed the object every time, hence it was not shown.

    Solution: the notebook class now has two image variables, holding the objects.

    class CDSnoteBook(ttk.Notebook):
        def __init__(self,parent,cdstestlabels):
            super().__init__()
    
            self.cdstestlabels=cdstestlabels
            self.cdsRow=0
            self.cdsColumn=1
            self.image1=PhotoImage(file=r'images\\checkmark_small.png')
            self.image2=PhotoImage(file=r'images\\checkmark.png')
    

    now the function 'update_answers' can use these objects to set the image option