A Beginner's Guide to Using pyGTK and Glade
In your button1_clicked() function, replace the print statement with:
Now, whenever you click GO! the results are displayed in your window. By dividing your main window with a set of vertical panes, you can resize this window, if you like (Figure 9).
Unlike GTK v1, under GTK v2 a tree and a list basically are the same thing; the difference is the kind of store each of them uses. Another important concept is the TreeIter, which is a datatype used to store a pointer to a particular row in a tree or list. It doesn't offer any useful methods itself, that is, you can't ++ it to step through the rows of a tree or list. However, it is passed into the TreeView methods whenever you want to reference a particular location in the tree. So, for example:
import gobject self.treeview=self.wTree.get_widget("treeview1") self.treemodel=gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_STRING) self.treeview.set_model(self.treemodel)
defines a tree model with two columns, each containing a string. The following code adds some titles to the top of the columns:
self.treeview.set_headers_visible(gtk.TRUE) renderer=gtk.CellRendererText() column=gtk.TreeViewColumn("Name",renderer, text=0) column.set_resizable(gtk.TRUE) self.treeview.append_column(column) renderer=gtk.CellRendererText() column=gtk.TreeViewColumn("Description",renderer, text=1) column.set_resizable(gtk.TRUE) self.treeview.append_column(column) self.treeview.show()
You could use the following function to add data manually to your tree:
def insert_row(model,parent, firstcolumn,secondcolumn): myiter=model.insert_after(parent,None) model.set_value(myiter,0,firstcolumn) model.set_value(myiter,1,secondcolumn) return myiter
Here's an example that uses this function. Don't forget to add treeview1 to your glade file, save it and copy it to your local directory:
model=self.treemodel insert_row(model,None,'Helium', 'Control Current Helium') syscallIter=insert_row(model,None, 'Syscall Redirection', 'Control Current Syscall Proxy') insert_row(model,syscallIter,'Syscall-shell', 'Pop-up a syscall-shell')
The screenshot in Figure 10 shows the results. I've replaced the TextView with a TreeView, as you can see.
A list is done the same way, except you use ListStore instead of TreeStore. Also, most likely you will use ListStore.append() instead of insert_after().
A dialog differs from a normal window in one important way—it returns a value. To create a dialog box, click on the dialog box button and name it. Then, in your code, render it with gtk.glade.XML(gladefile,dialogboxname). Then call get_widget(dialogboxname) to get a handle to that particular widget and call its run() method. If the result is gtk.RESPONSE_OK, the user clicked OK. If not, the user closed the window or clicked Cancel. Either way, you can destroy() the widget to make it disappear.
One catch when using dialog boxes: if an exception happens before you call destroy() on the widget, the now unresponsive dialog box may hang around, confusing your users. Call widget.destroy() right after you receive the response and all the data you need from any entry boxes in the widget.
Some day, you probably will write a pyGTK application that uses sockets. When doing so, be aware that while your events are being handled, the application isn't doing anything else. When waiting on a socket.accept(), for example, you are going to be stuck looking at an unresponsive application. Instead, use gtk.input_add() to add any sockets that may have read events to GTK's internal list. This allows you to specify a callback to handle whatever data comes in over the sockets.
One catch when doing this is you often want to update your windows during your event, necessitating a call to gtk.mainiteration(). But if you call gtk.mainiteration() while within gtk.mainiteration(), the application freezes. My solution for CANVAS was to wrap any calls to gtk.mainiteration() within a check to make sure I wasn't recursing. I check for pending events, like a socket accept(), any time I write a log message. My log function ends up looking like this:
def log(self,message,color): """ logs a message to the log window right now it just ignores the color argument """ message=message+"\n" self.logwindow.insert_at_cursor(message, len(message)) self.handlerdepth+=1 if self.handlerdepth==1 and \ gtk.events_pending(): gtk.mainiteration() self.handlerdepth-=1 return
|Using Salt Stack and Vagrant for Drupal Development||May 20, 2013|
|Making Linux and Android Get Along (It's Not as Hard as It Sounds)||May 16, 2013|
|Drupal Is a Framework: Why Everyone Needs to Understand This||May 15, 2013|
|Home, My Backup Data Center||May 13, 2013|
|Non-Linux FOSS: Seashore||May 10, 2013|
|Trying to Tame the Tablet||May 08, 2013|
- RSS Feeds
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- New Products
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- Validate an E-Mail Address with PHP, the Right Way
- Tech Tip: Really Simple HTTP Server with Python
- Trying to Tame the Tablet
- New Products
- git-annex assistant
5 hours 33 min ago
- direct cable connection
5 hours 55 min ago
- Agreed on AirDroid. With my
6 hours 5 min ago
- I just learned this
6 hours 10 min ago
6 hours 40 min ago
- not living upto the mobile revolution
9 hours 31 min ago
- Deceptive Advertising and
10 hours 7 min ago
- Let\'s declare that you have
10 hours 8 min ago
- Alterations in Contest Due
10 hours 9 min ago
- At a numbers mindset, your
10 hours 10 min ago
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi
It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.