4

I started using GTK 3 with Python. How could I make an executable Windows file from my GTK 3 Python source using PyInstaller, cx_Freeze or py2exe?

I tried a lot of answers from Stack Overflow and many other web pages, but none worked.

I tried to make it with PyInstaller (I think it could be the easiest way) and my source code looks like:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class ButtonWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Button Demo")
        self.set_border_width(10)

        hbox = Gtk.Box(spacing=6)
        self.add(hbox)

        button = Gtk.Button.new_with_label("Click Me")
        button.connect("clicked", self.on_click_me_clicked)
        hbox.pack_start(button, True, True, 0)

        button = Gtk.Button.new_with_mnemonic("_Open")
        button.connect("clicked", self.on_open_clicked)
        hbox.pack_start(button, True, True, 0)

        button = Gtk.Button.new_with_mnemonic("_Close")
        button.connect("clicked", self.on_close_clicked)
        hbox.pack_start(button, True, True, 0)

    def on_click_me_clicked(self, button):
        print("\"Click me\" button was clicked")

    def on_open_clicked(self, button):
        print("\"Open\" button was clicked")

    def on_close_clicked(self, button):
        print("Closing application")
        Gtk.main_quit()

win = ButtonWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

But I get the following error:

Traceback (most recent call last):
  File "<string>", line 2, in <module>
  File "c:\python34\lib\gi\__init__.py", line 102, in require_version
    raise ValueError('Namespace %s not available' % namespace)
ValueError: Namespace Gtk not available
gtk returned -1

What shall I do with this or how can I make an executable in py2exe or cx_Freeze?

7
  • You should use cx_Freeze as py2exe is for python 2 I'll post an example of the layout and I'm going to want you to run it and then run the .exe file and tell me if there any error messages and I will help you fix them. Commented Jan 16, 2016 at 16:57
  • I used for a long time py2exe on Python 3.4. The error is in your required version, not in py2exe.. Commented Jan 16, 2016 at 17:16
  • ValueError: Namespace Gtk not available which is raised by the line 2 of your <module> Commented Jan 16, 2016 at 17:19
  • The OP got that error when using pyinstaller not py2exe. Commented Jan 16, 2016 at 17:21
  • 1
    @mabe02 The thing I hate about python to exe conversion questions is that they never really get noticed and most of the time you have to figure it out yourself, or if you answer a question your answer doesn't get any attention and the OP never comes back to acknowledge your answer most of the time. Commented Jan 16, 2016 at 17:56

1 Answer 1

3

Install cx_Freeze

You should just be able to do pip install cx_Freeze on Windows. Or you could go to their official website cx_Freeze.


Create a setup.py file in the same folder as your program.

File setup.py

import cx_Freeze

executables = [cx_Freeze.Executable("file.py")]

cx_Freeze.setup(
    name="WhatEverYouWantToNameIt",
    options={"build_exe": {"packages":["gi"]}},
    executables = executables
)

Open command prompt in the file location of your program. On Windows, you should just have to Shift + <left-click the folder and click open command window here. Once that opens up, type python setup.py build. If you get an error stating that Python is not in your path, then give the full path. For example, on Windows, with Python 3.4, you would do:

C:/Python34/python setup.py build

If you're on a Mac, then you would do:

python setup.py bdist_dmg

Once it's done, come back and tell me if it works. If it doesn't work, just give me the error message, and I'll fix the problem. Good luck!

Sign up to request clarification or add additional context in comments.

1 Comment

I'm not the original poster but I'm getting the same error when trying to package my application for windows xp. I tried your solution, it didn't work at first because cx_freeze will throw an error if you don't specify a version number. After fixing that, I got the same error I was getting with pyinstaller "namespace gtk not available".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.