Thursday, October 13, 2011

Nexsys: Main Window

 So we have the root package setup for the nexsys application.  Right now, it is not doing anything more than our Hello, World example however.  So the next step we're going to do is to create the main window for our application.



Create the GUI sub-package

If you remember from the previous post, the way that I like to break out my applications is to separate gui, api, and resources into separate sub-packages within my application.

For our main window, we're going to need to create the gui sub-package.

We are going to start this by taking our settings.py file and saving it out as gui/__init__.py

From here, we can just modify its docstring to say something more useful, like """ Sub-package containing all user interface components for nexsys """.

You should end up with something simple for your init file that looks like:

#!/usr/bin/python ~workspace/nexsys/gui/__init__.py

""" Sub-package containing all user interface components for nexsys. """

# define authorship information
__authors__     = ['Eric Hulser']
__author__      = ','.join(__authors__)
__credits__     = []
__copyright__   = 'Copyright (c) 2011'
__license__     = 'GPL'

# maintanence information
__maintainer__  = 'Eric Hulser'
__email__       = 'eric.hulser@gmail.com'


Create the NexsysWindow Class

For the main window of our nexsys application, we're going to create a new QMainWindow subclass.

So, once again lets save our __init__.py file as nexsyswindow.py.

(If you're not really sure why we're breaking everything up this way - you should have a read through the PyQt Coding Style Guidelines (Cont.) post which will explain the logic behind breaking down these classes and modules the way I do.)

Right now, we're just going to block in our main window class, so the code is going to be pretty minimal:

#!/usr/bin/python ~workspace/nexsys/gui/nexsyswindow.py

""" Defines the main NexsysWindow class. """

# define authorship information
__authors__     = ['Eric Hulser']
__author__      = ','.join(__authors__)
__credits__     = []
__copyright__   = 'Copyright (c) 2011'
__license__     = 'GPL'

# maintanence information
__maintainer__  = 'Eric Hulser'
__email__       = 'eric.hulser@gmail.com'

import os.path

import PyQt4.uic
from PyQt4 import QtCore, QtGui

class NexsysWindow(QtGui.QMainWindow):
    """ Main Window class for the Nexsys filesystem application. """
    
    def __init__( self, parent = None ):
        super(NexsysWindow, self).__init__(parent)
        
        # load the ui
        basepath = os.path.dirname(__file__)
        basename = self.__class__.__name__.lower()
        uifile   = os.path.join(basepath, 'ui/%s.ui' % basename)
        PyQt4.uic.loadUi(uifile, self)

I'm not going to breakdown this code as it should be familiar to you by now.  If it is new, you should check out the Dialogs, Windows, and Wizards - oh My! post from a couple weeks ago that breaks this down more.

Create the NexsysWindow Ui File

You may have noticed that we're referencing a ui file that does not exist yet in our nexsyswindow.py module.  So - as you may have guessed - our next step is to save out a ui definition for our main window.

For now, just fire up Designer and create a new blank Main Window file (Designer > MenuBar > File > New > Main Window) and save it as nexsys/gui/ui/nexsyswindow.ui

You can just leave it with the default settings right now - we're just blocking in our application, this will load the ui file for our class when we launch it, and thats good enough at this point.

Update the main.py module

If everything went according to plan, your folder structure should look like this now:

nexsys/
 |- gui/
 |   |- ui/
 |   |   - nexsyswindow.ui
 |   |- __init__.py
 |    - nexsyswindow.py
 |- __init__.py
 |- main.py
  - settings.py

The last step that we're going to need to do to get our application running is to update our main.py module file to reference our new main window, instead of just popping up the temp message box.

So if you open your nexsys/main.py file and switch:

    # create the main window
    QtGui.QMessageBox.information(None, 'Stub', 'Create the Main Window!')

with:

    # create the main window
    from nexsys.gui.nexsyswindow import NexsysWindow
    window = NexsysWindow()
    window.show()

This will now run our main window when we re-run our application.  Go ahead and give it a try!

Coming up Next

So now we have gotten our window blocked in - but its not actually doing anything.  Next up, we're going to start populating our ui file and updating our main window to start doing useful things. 

3 comments:

  1. The last step didn't work for me.
    Instead of
    from nexsys.gui.nexsyswindow ...
    I had to write
    from gui.nexsyswindow
    I think it's because main.py is in the nexsys package already.

    ReplyDelete
  2. Thanks guys, will need to look into why that is happening!

    ReplyDelete