1
votes

Today I've started learning python. I only ever used PHP for various things and never had to bother with building exe files.

Using some internet Python programming tutorials and a little of my own editing I came up with random "How many times you've clicked" application as shown below

import winsound
from tkinter import *

class Application(Frame):

    def __init__(self, master):
        Frame.__init__(self,master)
        self.pack()
        self.counts = 0
        self.create_widgets()

    def create_widgets(self):
        Label(self, text = "Test Label").pack()
        self.button = Button(self, text = "click", command = self.update_text).pack()

        self.result = Label(self, text = "Clicked 0 times")
        self.result.pack()

    def update_text(self):
        self.counts += 1

        self.result["text"] = "Clicked " + str(self.counts) + " times"

        winsound.PlaySound('sound1.wav', winsound.SND_FILENAME | winsound.SND_ASYNC)


root = Tk()
root.title("Title")
root.geometry("300x120")
app = Application(root)

root.mainloop

Application works fine but the problems started when I tried to compile it to a single exe file for a convenience reasons as I intend to write small "making something a bit easier" programs. Py2Exe doesn't want to compile the program with bundle_files 1 at all. PyInstaller did compile it with --onefile, but upon executing, application gives you nothing more than tkinter errors.

Is there a way of creating small one exe files with GUI or is it a dead end? I'm not going to lie but I really loved the idea of learning python and being able to use it in both web and desktop applications which wasn't very possible in PHP.

I'm using Python 3.4 and tested builtin py2exe and development PyInstaller for Python 3.3 - 3.4.

Edit: Sample setup.py for py2exe and the error

from distutils.core import setup
import py2exe
setup( windows=[{"script": "text.py"}],
       options = {"py2exe": {"bundle_files": 1}
})

Error

C:\Users\SEJBR\Desktop\Python>python setup.py py2exe
running py2exe

  1 missing Modules
  ------------------
? readline                            imported from cmd, code, pdb
OOPS: tkinter 2

PyInstaller compilation error

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named 'Tkinter'
2515 ERROR: TCL/TK seams to be not properly installed on this system
1
Did you use the window argument instead of console in py2exe? - Malik Brahimi
Yes. I used windows in setup and bundle_files:1 in setup>options and py2exe gives Missing Module OOPS: tkinter 2 error due to bundle_files lower than 2. But with bundle_files higher than 1 there's no talking about single executable. Edited the question and added sample py2exe setup.py and error I'm getting with it. - SEJBR
I'm not familiar with py2exe, but using PyInstaller, what errors did you get? - fhdrsdg
I somehow cannot reproduce errors I got yesterday when the file got compiled. Now during copilation I have an error which I added to the question. The file compiles but doesn't output anything. I guess current PyInstaller error is due to Python 3.X change from Tkinter to tkinter? - SEJBR
What's "builtin py2exe"? - martineau

1 Answers

0
votes

Well the workaround was quite simple.. I downgraded to Python 2.7.9 and PyInstaller compiled application perfectly with no problems. I just used

from __future__ import print_function
from __future__ import division

To use Python 3.X print function and apply division changes. If anyone comes up with real fix please answer the question.