Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Tkinter Notebook widget

Using this python recipe, i have created a notebook like widget on my Tk window. It all works fine until i tried to add an image into each tab. When i add image into the tab, the text i initially set were no longer shown. I am wondering if i can make the text (in this case is "tab one") to show right below the image.The following is my current code:

from Tkinter import *
class Notebook(Frame):
    def __init__(self, parent, activerelief = SOLID, inactiverelief = SOLID, xpad = 4, ypad = 4, activefg = 'black',activebg='green',inactivebg='grey',bd=1,inactivefg = 'black', **kw):                                                                
        self.activefg = activefg
        self.activebg=activebg
        self.bd=bd
        self.inactivebg=inactivebg
        self.inactivefg = inactivefg
        self.deletedTabs = []        
        self.xpad = xpad
        self.ypad = ypad
        self.activerelief = activerelief
        self.inactiverelief = inactiverelief                                               
        self.kwargs = kw                                                                   
        self.tabVars = {}                                                                 
        self.tabs = 0                                                                                                                                                 
        self.noteBookFrame = Frame(parent)                                                 
        self.BFrame = Frame(self.noteBookFrame)                                            
        self.noteBook = Frame(self.noteBookFrame, relief = RAISED, bd = 1, **kw)           
        self.noteBook.grid_propagate(0)                                                    
        Frame.__init__(self)
        self.noteBookFrame.grid()
        self.BFrame.grid(row =0, sticky = W)
        self.noteBook.grid(row = 1, column = 0, columnspan = 27)

    def add_tab(self, width = 1, **kw):            
            temp = self.tabs                                                                   
            self.tabVars[self.tabs] = [Label(self.BFrame, relief = RIDGE, **kw)]               
            self.tabVars[self.tabs][0].bind("<Button-1>", lambda Event:self.change_tab(temp))  
            self.tabVars[self.tabs][0].pack(side = LEFT, ipady = self.ypad, ipadx = self.xpad) 
            self.tabVars[self.tabs].append(Frame(self.noteBook, **self.kwargs))                
            self.tabVars[self.tabs][1].grid(row = 0, column = 0)                               
            self.change_tab(0)                                                                 
            self.tabs += 1                                                                     
            return self.tabVars[temp][1]
    def change_tab(self, IDNum): 
        for i in (a for a in range(0, len(self.tabVars.keys()))):
            if not i in self.deletedTabs:                                                  
                if i <> IDNum:                                                             
                    self.tabVars[i][1].grid_remove()                                       
                    self.tabVars[i][0]['relief'] = self.inactiverelief                     
                    self.tabVars[i][0]['fg'] = self.inactivefg
                    self.tabVars[i][0]['bg'] = self.inactivebg
                    self.tabVars[i][0]['bd'] = self.bd
                else:                                                                      
                    self.tabVars[i][1].grid()                                                                    
                    self.tabVars[IDNum][0]['relief'] = self.activerelief                   
                    self.tabVars[i][0]['fg'] = self.activefg
                    self.tabVars[i][0]['bg'] = self.activebg
                    self.tabVars[i][0]['bd'] = self.bd


root=Tk()
scheduledimage=PhotoImage(data="R0lGODlhgACAAPcAAAAAAAsLCxUVFRoaGjIZGSMjIy0tLTIrKzU0NDw8PEEWFkgVFX0AAGoWFmMYGEo3N0JCQkxMTFlOTlNUVF1SUlxcXH9HR2dXV2RkZGplZWxsbHVlZXxjZH1maHtsbHR0dH5ycnx8fKYSEqwTE6QdHbUdHb4aGowyMpYzM58wMKQoKLomJqMxMaI8PLc0NI5OTpxOToRaWqtAQKNLS7JERbVLTbdOUatVVa5ZWbNWVrtTVb1XWbJeXr9ZW7BfYINiY4xoaZRtboJ0dIp1dZJ4eKhgYKltbbRlZbhkZ7VnaL1maLVra6tycr5xcrR8fMBbXsNeYcViZMhlZslmaMNrbctqbM5ucNBucMV2d8l2d815etJzdNR3edZ6fNl8ftt/gYSEhIyMjI+QkJOTk5eYmJycnLKDg7yFhbePj7eVlbmdnaCfoKSkpKeoqKurq7ukpK+wsLS0tL2xsby8vMePj9aCg92DhOGGh+GHiOOLjOWPkOqUlO6bm/Kfn86pqdCrq8OyssK8vPOhocTExM3Nzd7FxdDPz8/Q0NXV1dbY2Nvb2+bOzufQ0OrV1eDf3+3c3N7g4OPj4+3i4ufp6evr6/Lt7e/w8PP19fn19ff4+Pz8/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AIMuJYNyRu8YXvMZEjd6Tq8aq7Vqkpc7LDQEJAAH0Xd/2Td/5/QD5nd/4fd8GgAAFMAACMAAEXuACkOAKruAToAHiONBqXNoyPdr1G7epPcNq9nAU/RAA0OEe/uEgHuIiPuIkHgAN7h/hXeFpvOL0y1pCHLkw3t7EglhhUAFzehUNAQAC0KNOAeGqLbmNUGnl/eNnXd4wPsREXd7uAmjQi+MEWgFkoMOYjOTGnORWPuWINQcZEGhOXhEQQKY7QoerJeasheQceN5oXsxKThS52uUXYQBjygYvbNYZfdaX3Ahs2Vp1Hrm2iBUIYqrYbn4RX06ojvAIdJjBj63Wed7OPB25ikUIGfnDgZ4RBaCrC4smwKXWHtsG3BqyZv3SihCeuXrjk44RBRABE1ABejjkma6md2i95DwIZZABurrhpa6VvfoBZWCgQ45YzwnYutYGUKyrEbBgt/4RBZAAqI7qFbAByAhsQuDGPhwBEjABEGDsx04SAxDg9s0VbpEAAp7t4j7u5F7u5n7u6J7u6r7u7N7u7v7u8B7v8j4TAQEAOw==")
note=Notebook(root, width= 400, height =400, activefg = 'red', inactivefg = 'blue')
note.grid()
tab1 = note.add_tab(text = "Tab One",image=scheduledimage)
tab2 = note.add_tab(text = "Tab Two")                                                
tab3 = note.add_tab(text = "Tab Three")
root.mainloop()
like image 223
Chris Aung Avatar asked May 13 '13 03:05

Chris Aung


People also ask

What is TTK notebook?

In Python Tkinter notebook is defined as tabs for controlling tabs that are provided by ttk library and such a tabbed widget is used for allowing to navigate from one page to another which can be done using the index tab.

Can I use tkinter in Jupyter?

Tkinter can be installed on Jupyter notebook as well, by using the command pip install tkinter. We can run all the standard commands of Tkinter in Jupyter notebook. Now, after verifying the installation, you are ready to write your Tkinter application code in Jupyter notebook.


1 Answers

Label reference: compound

Controls how to combine text and image in the label. By default, if an image or bitmap is given, it is drawn instead of the text. If this option is set to CENTER, the text is drawn on top of the image. If this option is set to one of BOTTOM, LEFT, RIGHT, or TOP, the image is drawn besides the text (use BOTTOM to draw the image under the text, etc.). Default is NONE.

tab1 = note.add_tab(text = "Tab One",image=scheduledimage, compound=TOP)

ttk.Notebook sample:

from Tkinter import *
from ttk import *

root = Tk()
scheduledimage=PhotoImage(...)
note = Notebook(root)

tab1 = Frame(note)
tab2 = Frame(note)
tab3 = Frame(note)
Button(tab1, text='Exit', command=root.destroy).pack(padx=100, pady=100)

note.add(tab1, text = "Tab One",image=scheduledimage, compound=TOP)
note.add(tab2, text = "Tab Two")
note.add(tab3, text = "Tab Three")
note.pack()
root.mainloop()
exit()
like image 135
kalgasnik Avatar answered Oct 13 '22 12:10

kalgasnik