Search code examples
pythonpython-2.7tkinterttk

python 2.7.13 fill tabs (from Notebook) with widgets


I'm having trouble with filling these tabs with other widgets, here are the images of the current state:

SAP2000 tab/the 1st one

ETABS tab/the 2nd one

#imports
import Tkinter as tk #to be able to use its "Button" as tk.Button
import ttk
from Tkinter import * 
from ttk import *
#The GUI
class MYWINDOW(Frame):
    def __init__(self,master=None):
        Frame.__init__(self,master) #run the Parent's initiation

        self.InitUI() #construct the UI

    def InitUI(self):

        #make rows and columns visible with "minsize"
        for row in range(5):
            self.grid_rowconfigure(row,minsize=25)
        for col in range(5):
            self.grid_columnconfigure(col,minsize=10)
        #make the parent window appears
        self.grid()

        #make 2 tabs: SAP2000, ETABS
        n=Notebook(self.master)
        n.grid(row=0,column=1)

        #Construct tab ETABS
        fetabs = Frame(n)
        tetabs= n.add(fetabs,text='ETABS')

        impetabs=tk.Button(tetabs,text="Import ETABS Model",width=20,relief=tk.RAISED)
        impetabs.grid(row=0,column=1)


        #Construct tab SAP2000
        fsap = Frame(n)
        tsap = n.add(fsap,text='SAP2000')

        impsap=tk.Button(fsap,text="Import SAP2000 Model",width=20,relief=tk.RAISED)
        impsap.grid(row=1,column=1)

        seltwtmdfcmbox=ttk.Combobox(tsap,justify=tk.CENTER,state='readonly')
        seltwtmdfcmbox.grid(row=1,column=3)

In case of any suggestions to use .pack() or .place(), this is why I've chosen .grid():-

Also, I've tried .pack() manager before but using it assigns tabs themselves in a wrong place, then I've used .place(x=0,y=0,relheight=1,relweight=1) with the Notebook's object n to make tabs rightly positioned, but widgets disappear with any usage of .place(x=any value, y=any value) or .grid_location(x=any value, y = any value).

Then I changed only the .place() of widgets with .pack() ,, and .pack() puts widgets on top of each other, imagine how useless a button when a Combobox takes its same place and hide it!, so I returned to .grid(row = any value, column = any value) for all widgets and .grid() with the main window, however, I got weird results, namely, tabs couldn't recognize that widgets are belong to them with the exception of 1 button, and the other widgets are placed outside the tabs.

What am I missing, considering that I'm fairly new with Tkinter?!


Solution

  • The whole point of creating a class that inherits from Frame is that everything in that class is inside of that frame. You aren't doing that.

    You are setting the master of the notebook to be the same as the master of the frame. Instead, it needs to be the frame:

    n=Notebook(self)
    

    In a similar vein, when you want to place windows in the tab, their master needs to be the frame, not the results of the call to n.add. The reason being, n.add(...) returns None. When you use that as the master for the Combobox and Button, they become children of the root window.