I have just learned a few more tricks on how to best work with GTK and Python. First, let me start at the beginning. After you create a Glade file (If you don't know what Glade is, read this), the next step is to create the Python code to load the Glade file (i.e. turn the XML into actual widgets on the screen), and hook up all the signals to actual Python methods. Well, if you're like me, you don't know how many parameters are needed for each signal handler, and soon you find yourself looking for examples (or just guessing) on how to hook up various signals (such as a button click, treeviewdrag_begin, window destroy, etc...). This can become a very time consuming and frustrating process, especially when you are just learning Python & GTK.

Trick #1: Using Synaptic Package Manager (or equivalent), go get the tepache package. This package gives you a command line way of generating the Python code needed to load a Glade file into a class, auto-connect all the signals, and it even stubs out all the signal handler methods. If that wasn't enough, it even helps setup language translations for your Glade file.

To use the tepache command, use the following syntax in the terminal window: tepacheYourFile.Glade

After you run this command, it will generate a .py file, named YourFile.py. Here is the output from the tepache command using one of my more complex Glade files:

defon_app1_destroy(self,widget,*args):print"on_app1_destroy called with self.%s"%widget.get_name()gtk.main_quit()

defon_mnuQuit_activate(self,widget,*args):print"on_mnuQuit_activate called with self.%s"%widget.get_name()

defon_mnuAbout_activate(self,widget,*args):print"on_mnuAbout_activate called with self.%s"%widget.get_name()

defon_treeFiles_drag_drop(self,widget,*args):print"on_treeFiles_drag_drop called with self.%s"%widget.get_name()

defon_tlbAddTrack_clicked(self,widget,*args):print"on_tlbAddTrack_clicked called with self.%s"%widget.get_name()

defon_tlbRemoveTrack_clicked(self,widget,*args):print"on_tlbRemoveTrack_clicked called with self.%s"%widget.get_name()

defon_hscrollbar1_value_changed(self,widget,*args):print"on_hscrollbar1_value_changed called with self.%s"%widget.get_name()

defon_vscrollbar1_value_changed(self,widget,*args):print"on_vscrollbar1_value_changed called with self.%s"%widget.get_name()

defon_scrolledwindow_Right_drag_drop(self,widget,*args):print"on_scrolledwindow_Right_drag_drop called with self.%s"%widget.get_name()

defmain():gnome.program_init("main","0.0.1")app1=App1()

app1.run()

if__name__=="__main__":main()

Trick #2: There is a library that comes with pyGtk called SimpleGladeApp, which is going to save you a ton of time. It is used quite heavily in the tepache output (as you can see from my example). What does this library do, you ask?

Let me break it down for you:

It loads a Glade XML file and returns an object for your form. It reminds me very much of a form object from the Windows world (i.e. Visual Basic.NET, C# Windows Application, etc...).

On this new form object, it has a reference to every single widget on your form. For example: self.myTree, self.btnToolBar1, self.Canvas1.

It adds a show() and hide() method for every widget.

It adds a grab_focus() method for every widget.

It adds a run() and quit() for your form (which simply calls the main_loop() gtk methods.

It auto-connects all the signal handlers you have defined (of that tepache defined) to the signals defined in the Glade file.

It translates all the text in the Glade file (if you have the appropriate translation files in your project).

So, now that you know this, take a look at my example above again. Anywhere in this class you can access all the widgets, hide and show them, set focus to them, all without calling the GladeFile.get_widget("") method.

I hope this blog entry is helpful to some of you. It's going to save me a ton of time trying to setup my GUI and hook up all the signals. Good luck!

Popular entries

Ultimele Postări strong>

For a limited-time only, we are offering a unique OpenShot t-shirt design!
Not only do you get an awesome t-shirt which helps us to continue funding development of OpenShot, but you also get a special...

I am proud to announce the immediate release of OpenShot 2.4.4, the absolute best version yet! This is going to be a long post, but here is a quick summary for those who are short on time. This releas...