You are here

How to portabilize pygtk and python

9 posts / 0 new
Last post
khiraly
Offline
Last seen: 15 years 11 months ago
Joined: 2008-01-08 16:40
How to portabilize pygtk and python

Hi!

Im developing a small pygtk based application (using both windows and linux), and I want to portabilize this application on windows platform.

My program depend on the followings:
- python 2.5
- pygtk (and Gtk) 2.10 or later
- pyserial

What I want to know:
- documentation, howto, tutorial (where is it? What is the basics?)
- best way to begin, where to start?

Can somebody point me to the documentation?

I readed through the website, but there is no real (deep) documentation. AmI missing something?

Thank you in advance!

Simeon
Simeon's picture
Offline
Last seen: 9 years 6 months ago
DeveloperTranslator
Joined: 2006-09-25 15:15
Well...

You're right. Unfortunately there is not much documentation.
The best thing would be if the program doesn't write to the Registry and you can pass a switch to the exe to tell him where to look for his data or python or whatever.
This way it would be portable out of the box and it would be very easy to make it compliant to the paf standard used by this site.

"What about Love?" - "Overrated. Biochemically no different than eating large quantities of chocolate." - Al Pacino in The Devils Advocate

khiraly
Offline
Last seen: 15 years 11 months ago
Joined: 2008-01-08 16:40
Simeon: Thank you for your

Simeon:
Thank you for your answer.

My program does notuse anyfile from the system anddoes not save to it either (at the moment, it will change however). And I dont write to the system registry either.

I think the best way would be, if you (or somebody else) can me guiding through a simple python package.

So let consider a really simple pygtk program, what I wrote sometime ago (a demo notebook application with close button on each tab)

The source code can be retrived from here:
http://www.daa.com.au/pipermail/pygtk/2006-May/012221.html

I'll paste the code at the end of my reply.

This code can be run directly (just one file).
Save as notebook.pyw (or anything.pyw), and double click on it, and itwill run. (or type at the windows shell (cmd):
C:\Python25\python.exe notebook.py

It requires:
python 2.4 or 2.5
pygtk (and gtk)

Can you guide me through creating a portable app through this simple program? It would be really wonderful!

Thank you very much in advance,
Khiraly

#!/usr/bin/env python
# example notebook.py

import pygtk
pygtk.require('2.0')
import gtk

class NotebookExample:
  def add_icon_to_button(self, button):
    iconBox = gtk.HBox(False, 0)
    image = gtk.Image()
    image.set_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU)
    gtk.Button.set_relief(button, gtk.RELIEF_NONE)
    #gtk.Button.set_focus_on_click(button,False)
    settings = gtk.Widget.get_settings (button)
    (w,h) = gtk.icon_size_lookup_for_settings (settings, gtk.ICON_SIZE_MENU)
    gtk.Widget.set_size_request (button, w + 4, h + 4)
    image.show()
    iconBox.pack_start(image, True, False, 0)
    button.add(iconBox)
    iconBox.show()
    return


  def create_custom_tab(self, text, child):
    #create a custom tab for notebook containing a
    #label and a button with STOCK_ICON
    eventBox = gtk.EventBox()
    tabBox = gtk.HBox(False, 2)
    tabLabel = gtk.Label(text)

    tabButton=gtk.Button()
    tabButton.connect('clicked', self.remove_book, child)

    #Add a picture on a button
    self.add_icon_to_button(tabButton)
    iconBox = gtk.HBox(False, 0)

    eventBox.show()
    tabButton.show()
    tabLabel.show()

    tabBox.pack_start(tabLabel, False)
    tabBox.pack_start(tabButton, False)

    # needed, otherwise even calling show_all on the notebook won't
    # make the hbox contents appear.
    tabBox.show_all()
    eventBox.add(tabBox)
    return eventBox


  # Remove a page from the notebook
  def remove_book(self, button, child):
    page = self.notebook.page_num(child)
    if page != -1:
      self.notebook.remove_page(page)
    # Need to refresh the widget --
    # This forces the widget to redraw itself.
    self.notebook.queue_draw_area(0, 0, -1, -1)


  def remove_current_book(self, *arguments, **keywords):
    page = self.notebook.get_current_page()
    if page != -1:
      self.notebook.remove_page(page)
    return True

  def delete(self, widget, event=None, *arguments, **keywords):
    gtk.main_quit()
    return False

  def add_new_book(self, *arguments, **keywords):
    self.page_number += 1
    frame = gtk.Frame("Frame %d" % self.page_number)
    frame.set_border_width(10)
    frame.set_size_request(100, 75)
    frame.show()
    label = gtk.Label("Inside of Frame %d" % self.page_number)
    frame.add(label)
    label.show()

    eventBox = self.create_custom_tab("Tab %d" % self.page_number, frame)
    self.notebook.append_page(frame, eventBox)

    # Set the new page
    pages = gtk.Notebook.get_n_pages(self.notebook)
    self.notebook.set_current_page(pages - 1)
    return True
  
  def __init__(self):
    window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    window.connect("delete_event", self.delete)
    window.set_border_width(10)

    # Create a new notebook
    self.notebook = gtk.Notebook()
    window.add(self.notebook)
    self.notebook.show()

    # key accelerators
    self.accel_group = gtk.AccelGroup()
    self.accel_group.connect_group(ord('q'), 
                     gtk.gdk.CONTROL_MASK, 
                     gtk.ACCEL_LOCKED, 
                     self.delete)
    self.accel_group.connect_group(ord('w'), 
                     gtk.gdk.CONTROL_MASK, 
                     gtk.ACCEL_LOCKED, 
                     self.remove_current_book)
    self.accel_group.connect_group(ord('t'), 
                     gtk.gdk.CONTROL_MASK, 
                     gtk.ACCEL_LOCKED, 
                     self.add_new_book)
                     
    window.add_accel_group(self.accel_group)

    # Add some tab pages for demonstrating
    for i in range(5):
      self.page_number = i
      self.add_new_book()

    # Set what page to start at (page 4)
    self.notebook.set_current_page(3)
    window.show()


def main():
  gtk.main()
  return 0


if __name__ == "__main__":
  NotebookExample()
  main()
rab040ma
Offline
Last seen: 3 days 5 hours ago
Joined: 2007-08-27 13:35
GTK

You say above that the new program doesn't rely on the system knowing where GTK or Python are (or at least that's what I think you said), but this program seems to rely on GTK. Or maybe it is relying on GTK to be in the same directory as this script?

I guess my question is, and assuming you start it with "python.exe notebook.py" when you say

import pygtk

how does Python know where to look to find pygtk, and how does pygtk know where to look for GTK.

MC

khiraly
Offline
Last seen: 15 years 11 months ago
Joined: 2008-01-08 16:40
Sorry for the inconvenience

Sorry for the inconvenience rab040ma:
I said:
"It requires:
python 2.4 or 2.5
pygtk (and gtk)"

It means, that you NEED to install
1. python
2. gtk
3. pygtk
before to be able to execute this script. (and this is why Im interested in portableapps: to be able to eliminate these prerequisites)

So answering to your question:
When I say:
import pygtk
The pygtk must be installed into:
C:\Python25\Lib\sites-packages\pygtk
(if you have python version 2.5. However pygtk exists to python 2.4 too))

So the goal is:
portabilize:
python, gtk, pygtk and this little example script.

Does it clarifies a little bit?
If you have more questions, please feel free to ask it.
I'll try my best to answer it.

Best regards,
Khiraly

rab040ma
Offline
Last seen: 3 days 5 hours ago
Joined: 2007-08-27 13:35
I think you misunderstood

I think you misunderstood what I was asking.

If python ALWAYS and ONLY looks in C:\Python25\Lib\sites-packages\pygtk to find it, then you can't make it portable. That would be unusual. So there is likely to be a provision for you to install pygtk to another drive or folder, and if so, there should be a way for Python to figure out where that is other than searching the entire computer.

It could easily be that if you install Python25 in J:\apps\Python25 it will look for pygtk in j:\apps\Python25\Lib\sites-packages\pygtk (that is, in the relative place in the directory tree). That would make it "relatively" easy.

Another way this could happen is for the pygtk installer to leave a record in the "registry" to say where it is installed. Python could then look in the registry to figure it out. This works well with "installed" programs, and is fairly common, but means a lot of extra work if it can be made to work with portable ones at all. Open source programs rarely rely solely on the registry, so I would be very surprised if this were the way it happens.

There are lots of ways for this to happen. There could be a configuration file in the main python directory to tell it where to look for pygtk; there could be a way to set an environment variable first that python would use to find it; there could be a command line option so when you call python it can read the location from there. The various installers probably take care of this for you behind the scenes, so not everyone would be aware that it is even happening.

If you happen to know how this works, it can make it easier for someone who doesn't.

MC

Simeon
Simeon's picture
Offline
Last seen: 9 years 6 months ago
DeveloperTranslator
Joined: 2006-09-25 15:15
Sorry

I cant do it at the moment cause I don't know anything about Python and I don't have much extra time at the moment. But maybe someone else here can help you.

"What about Love?" - "Overrated. Biochemically no different than eating large quantities of chocolate." - Al Pacino in The Devils Advocate

John T. Haller
John T. Haller's picture
Online
Last seen: 3 min 58 sec ago
AdminDeveloperModeratorTranslator
Joined: 2005-11-28 22:21
Compile Python into an EXE

Your best bet is to compile the Python into an EXE. Check out Cornice Portable for an example. It's written in Python and available as PY scripts. But the author also compiled it into an EXE and it's that version that I made portable. I'm not super familiar with the process to do this (I know there are a few different tools for it) so asking in a Python forum would be a good idea if you need some more help with that.

As for GTK, as long as your app supports being pointed to an installation of it, it should work. Best if you also allow the user to have GTK at \PortableApps\CommonFiles\GTK as well. This location is supported by the apps here.

Oh, and welcome to PortableApps.com Smile

Sometimes, the impossible can become possible, if you're awesome!

khiraly
Offline
Last seen: 15 years 11 months ago
Joined: 2008-01-08 16:40
Is that exe (what you did

Is that exe (what you did portabilize) is available somewhere?

>But the author also compiled it into an EXE
How he did it? I downloaded the cornice source, but unable to find it out.

Is it an installer? or already a standalone executable?

So I assume I need to produce a .exe prior to be able portabilizing my app.
So lets start portabilizing a simple python "hello world"

Thank you for any help.

Log in or register to post comments