Il giorno gio, 21/01/2010 alle 23.14 +0100, [email protected] ha
scritto:
> Hi,
> I don't have experience in creating big apps. I want to know how to
> split app into files. For example - I have main class in main file and
> I want to move widgets (loading widgets from gladefile) into new file.
> It should be in the mainclass i think.. but i can't import something
> inside class (it works but it's not proper).
"If you don't want it into the same file of the main class, you
certainly don't want it into the main class" is a rule for which I'm not
able to see any exception.
That said, what I do in many projects is creating this file:
import gtk
class Ui(object):
def __init__(self, APP, filename):
self._builder = gtk.Builder()
self._builder.set_translation_domain(APP)
self._builder.add_from_file(filename)
def __getattr__(self, attr_name):
try:
return object.__getattribute__(self, attr_name)
except AttributeError:
obj = self._builder.get_object(attr_name)
if obj:
self.obj = obj
return obj
else:
raise AttributeError, "no object named \"%s\" in the
GUI." % attr_name
and importing Ui from it; then, the main class will, in __init__, do
something like
self.ui = Ui("nameoftheapp", "path/to/the/file.glade")
so that from now on you just access widgets as
self.ui.name_of_the_widget
_But_ this is just what _I_ find convenient, and not really because I
want to separate it from the main class, but just to avoid some
get_object() calls and get cleaner code. In general, there are no
particular requirements to separate something from some class: if you
see it grew too big and there is something that can be separated from
it, just separate it.
> Or signal handlers - how to move them into other file and do it right?
I'm not really sure I would want to move signal handlers... if all your
handlers are method of this main class and you want to save some coding,
just use signals_autoconnect.
> Another thing is i have some (not main) class, but it should interact
> with gui (some changes like showing widgets etc). It's assigned in
> mainclass and constructor of it looks like this:
>
>
> __init__(gui):
> self.gui=gui
>
>
> and then.. in functions of this class I can change for example
> self.gui.some_widget.show()
> And again.. I don't think it's proper method for doing this.
>
Well, in my opinion it's hard to say _in general_ that this is wrong.
When you choose the optimal size for some class, just think in terms of
functionalities: the best rule to respect is that it must be
comfortable.
Certainly, if "self.gui.some_widget.show()" is part of a block of code
that works on the gui, it may be smart to move it to a method of the gui
and call that from your class.
>
> Some tips, please?
>
Read other people's code, and just try. And obviously, be ready to
change things that you find not optimal.
Pietro
_______________________________________________
pygtk mailing list [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/