So I’m learning pygtk to write a small program inspired by my last post.
and I wrote this to see if I really undestand how the tookit worked.
I’m reading http://www.pygtk.org/pygtk2tutorial/index.html which seems to be the official tutorial and it’s written very well and/or gtk is a very handy tool.
I personally like a lot the way they do the table layout it’s much more simplier then it’s java cousing. Since you can actually make a mind map on how big each component is based on it’s numbers instead of having to add them up and see if they collide.
The funniest part of all is that I wrote this in about 30min this morning before going to work and I have spend 15 more minutes making it “presentable”, after that I spend almost 2-3 hours figuring out how to post the damn thing to wordpress, but that is the topic of my next post. for now enjoy the code.
Bonus points if you tell me why lines 48-49 screw up the layout.
[python]
#!/usr/bin/env python
import pygtk
pygtk.require(’2.0′)
import gtk
#this will generate the list of all the stock options
stock_options = [option for option in dir(gtk) if option.startswith(”STOCK”)]
class Stock_Buttons:
def createButton(self,stock_option):
#ugly but this is a test.
button = gtk.Button(stock=eval(”gtk.”+stock_option))
button.connect(”clicked”, self.clicked,stock_option)
button.show()
return button
def clicked(self,widget,data=None):
print “gtk.Button(stock=gtk.%s))” % data
def __init__(self):
#setting some defaults.
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title(”Sample stock buttons”)
self.window.set_border_width(10)#this will handle both gui events (click the X) and system events like
#the kill command.
self.window.connect(”destroy”, lambda wid: gtk.main_quit())
self.window.connect(”delete_event”,lambda a1,a2:gtk.main_quit())#small trick to get the the size if the grid
#93 boxes fit into a 10×10 grid
#45 boxes fit into a 5×5 grid and so on.
import math
length = math.ceil(math.sqrt(len(stock_options)))#pasing floats to gtk is deprecated so to avoid that we make sure it’s an int
length = int(length)
print len(stock_options)
#we set false so gtk will strink the buttons.
table = gtk.Table(length,length,False)
#set one widget on each 1×1 slot until we run out of widgets
for index,option in enumerate(stock_options):
i=index / length
j=index % length
#~ why this shows a weird layout?
#~ i=index % length
#~ j=index / length
table.attach(self.createButton(option),i,i+1,j,j+1)self.window.add(table)
table.show()
self.window.show()
def main():
gtk.main()
return 0
if __name__ == “__main__”:
Stock_Buttons()
main()
[/python]
I’ll post a screenshot but the upload thing doesn’t wants to work unless I give 777 to the dir so no way, same goes with the raw code, ones I’ll figure out how to do it I’ll update this.
3 comments ↓
[…] ok so I have started looking into a way to post code so I first try this which seems to be the most acceptable. I try it at the stock buttons post and it looks great (not really just ok) although it still ate all the whitespace, and no it also screws up this PLAIN TEXT C: […]
Thanks for the code. Now, do you know how to get rid of the text entirely and display the image only?
I rly don’t know Chris, maybe it’s a flag to the button constructor or maybe a method on the button itself.
Leave a Comment