Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am trying to make an app in quickly, and I have made the windows, one is called test the other is called menu and the test is the start page, and I want to program it so when you click on the button (button1), it will load menu.ui. Here is all the code from TestWindow:

# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
### BEGIN LICENSE
# This file is in the public domain
### END LICENSE
import gettext
from gettext import gettext as _
gettext.textdomain('test')

import gtk
import logging
logger = logging.getLogger('test')
from test_lib import Window
from test.AboutTestDialog import AboutTestDialog
from test.PreferencesTestDialog import PreferencesTestDialog
import os
# See test_lib.Window.py for more details about how this class works
class TestWindow(Window):
    __gtype_name__ = "TestWindow"

    def finish_initializing(self, builder): # pylint: disable=E1002
        """Set up the main window"""
        super(TestWindow, self).finish_initializing(builder)

        self.AboutDialog = AboutTestDialog
        self.PreferencesDialog = PreferencesTestDialog

        # Code for other initialization actions should be added here.
    def on_button1_clicked(self, widget, data=None):
        print "Loading Menu!"
        os.chdir(r"/home/user/test/data/ui/")
        os.startfile("menu.ui")

The last 2 lines are my attempt at trying to make it start the menu.ui. Are there any ways to do this? Any help is appreciated! Thanks -Drex

share|improve this question
3  
Better suited for Stack Overflow, since they're the coding experts. –  Thomas W. Jan 22 '12 at 20:31
    
actually its about app development with quickly, which is a perfectly good question for askubuntu.com –  xubuntix Jan 23 '12 at 13:19
    
App development, using Quickly, is on-topic and encouraged on Ask Ubuntu. Community driven Q&A for Ubuntu users and developers –  Marco Ceppi Jan 23 '12 at 16:30

1 Answer 1

A good starting point for app development with quickly and python is here: http://developer.ubuntu.com/resources/tutorials/all/diy-media-player-with-pygtk/

In general, you can see how quickly handles other .ui files in your code in the lines

from test.AboutTestDialog import AboutTestDialog
from test.PreferencesTestDialog import PreferencesTestDialog
[...]
self.AboutDialog = AboutTestDialog
self.PreferencesDialog = PreferencesTestDialog

You would add your new window the same way:

Create a new file for your menu window under your test directory, such that your code could look like this:

from test.AboutTestDialog import AboutTestDialog
from test.PreferencesTestDialog import PreferencesTestDialog
from test.MenuTestWindow import MenuTestWindow
[...]
self.AboutDialog = AboutTestDialog
self.PreferencesDialog = PreferencesTestDialog
self.MenuWindow = MenuTestWindow

Then you can show the Window in your on_button1_clicked method like this:

def on_button1_clicked(self, widget, data=None):
    print "Loading Menu!"
    self.MenuWindow.show()

Now the only question remaining is, what does your MenuTestWindow class look like? I would simply look into the quickly created classes and write something like this:

import gettext
from gettext import gettext as _
gettext.textdomain('test')

import logging
logger = logging.getLogger('test')

from test_lib.MenuWindow import MenuWindow

# See test_lib.MenuWindow for more details about how this class works.
class MenuTestWindow():
    __gtype_name__ = "MenuTestWindow"

    def finish_initializing(self, builder): # pylint: disable=E1002
        """Set up the about dialog"""
        super(MenuTestWindow, self).finish_initializing(builder)
        # Code for other initialization actions should be added here.

which leaves us with the test_lib.MenuWindow file and class (also stolen from the quickly defaults):

import gtk
import logging
logger = logging.getLogger('test_lib')

from . helpers import get_builder, show_uri, get_help_uri
from . preferences import preferences

# This class is meant to be subclassed by MenuWindow.  It provides
# common functions and some boilerplate.
class Window(gtk.Window):
    __gtype_name__ = "Window"

    # To construct a new instance of this method, the following notable 
    # methods are called in this order:
    # __new__(cls)
    # __init__(self)
    # finish_initializing(self, builder)
    # __init__(self)
    #
    # For this reason, it's recommended you leave __init__ empty and put
    # your initialization code in finish_initializing

    def __new__(cls):
        """Special static method that's automatically called by Python when 
        constructing a new instance of this class.

        Returns a fully instantiated MenuTestWindow object.
        """
        builder = get_builder('MenuWindow')
        new_object = builder.get_object("menu_window")
        new_object.finish_initializing(builder)
        return new_object

    def finish_initializing(self, builder):
        """Called while initializing this instance in __new__

        finish_initializing should be called after parsing the UI definition
        and creating a TestWindow object with it in order to finish
        initializing the start of the new TestWindow instance.
        """
        # Get a reference to the builder and set up the signals.
        self.builder = builder
        self.ui = builder.get_ui(self, True)

(I hope I didn't miss anything... :-)

This should be all.

However: If I would be writing this application, I would probably not use a different .ui file, but place every additional gui/window in the main window ui file (lets call the new window MenuWindow) and access it via something like this:

def on_button1_clicked(self, widget, data=None):
    mw = self.builder.get_object("MenuWindow")
    mw.show()

This way you don't have to create all those other classes and files. But it is your app and you have to know yourself, if a separate .ui file is needed in your case.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.