Python Tkinter – Create Tabs with Notebook Widget

In the Video I go through writing the code below which will add a notebook widget inside of your main window which will allow you to make tabs in your interface. The majority of the code is configuring the window itself, there is only about 6 lines that actually make the tabs which when I learned this I was surprised how easy it was to do.

 

 

Note the only difference between the code for python 2.x and 3.x is the import statements so if you wanted to make it more cross platform you could change the block of imports into a try except block

 

 

4 Comments

  1. If I were to create a system that uses tabs to show things like stock and orders, how would I go about adding buttons and text boxes to this? I am currently creating a system for my A level project and have not used TkInter before

    • In order to add other widgets inside of your tabs you will need to attach them to your tab that you defined. Using the code in this post as an example if you wanted to add a button to page 1 you will add the following code somewhere below line 24 mybutton = Button(page1, text=’MyButton’) mybutton.grid(row=1,column=1) This creates a button with the text ‘MyButton’ and attaches it to ‘page1’ which is our object for ‘Tab1’ and then adds the button to the grid of our tab in row1 column1. You can do this for any widget you would normally place in any other frame, just make sure you attach it to the object of your tab. Note that in order for your scaling to work as you would expect on those widgets you will have to add a weight to each cell in the grid of the tab interface similar to how we did for the main window in lines 12-16 because inside of each tab we have our own independent grid that will need to be setup. All of that being said I would suggest splitting your code into more functions and/or classes to write a GUI as your code will turn into a mess very quickly if you don’t. If you have any questions or concerns please follow up and let me know!

    • One way you can change the background color of the tab window is by attaching a frame widget and using grid to place it over the entire background and then change the color of that frame. I do not have any articles at this time on working with frames but http://effbot.org/tkinterbook/frame.htm talks about working with the frame widget.

Leave a Reply

Your email address will not be published.


*