Using Cx_freeze to package Python exe example

Source: Internet
Author: User
Files needed to be used wxapp.py, read_file.py, setup.py

Copy the Code code as follows:


#!/usr/bin/env python
#-*-Coding:utf-8-*-
#file: wxapp.py

Import WX
Import OS
Import Sys
Import Read_file


Class Frame (WX. Frame):
def __init__ (self):
Wx. Frame.__init__ (self, parent=none, title= ' Hello from Cx_freeze ')
panel = WX. Panel (self)
Closemebutton = wx. Button (Panel,-1, ' Close Me ')
Wx. Evt_button (self, Closemebutton.getid (), self. Oncloseme)
Wx. Evt_close (self, self.) Onclosewindow)
Pushmebutton = wx. Button (Panel,-1, ' Push Me ')
Wx. Evt_button (self, Pushmebutton.getid (), self. Onpushme)
Sizer = WX. Boxsizer (WX. Horizontal)
Sizer. ADD (Closemebutton, flag=wx. All, border=20)
Sizer. ADD (Pushmebutton, flag=wx. All, border=20)
Panel. Setsizer (Sizer)
Topsizer = wx. Boxsizer (WX. VERTICAL)
Topsizer.add (panel, flag=wx. All | Wx. EXPAND)
Topsizer.fit (self)

def oncloseme (self, event):
obj = Read_file. Printcontent ()
If GetAttr (sys, ' Frozen ', None):
Path = Os.path.dirname (sys.executable)
Else
Path = Os.path.dirname (__file__)
Path = Os.path.join (path, "read_file.py")
Obj.show_content (PATH)

def onpushme (self, event):
Wx. MessageBox (' I was pushed! ', ' informational message ')

def onclosewindow (self, event):
Self. Destroy ()


The Class App (WX. APP):
def OnInit (self):
frame = FRAME ()
Frame. Show (True)
Self. Settopwindow (frame)
Return True


App = App (1)
App. Mainloop ()

Copy the Code code as follows:


#!/usr/bin/env python
#-*-Coding:utf-8-*-
#file: read_file.py

Class Printcontent (object):
def show_content (self, Path):
f = Open (path)
For line in F:
Print Line
F.close ()

Copy the Code code as follows:


#!/usr/bin/env python
#-*-Coding:utf-8-*-
#file: setup.py

# A Simple setup script to the Create an executable running WxPython. This also
# demonstrates the method for creating a Windows executable this does not having
# an associated console.
#
# wxapp.py is a very "Hello, World ' type WxPython application
#
# Run The build process by running the command ' Python setup.py build '
#
# If Everything works well should find a subdirectory in the build
# subdirectory that contains the files needed to run the application
Import Sys
From Cx_freeze import Setup, executable

Build_exe_options = {"Optimize": 2,
"Include_files": ["read_file.py"]}

Base = None
if Sys.platform = = ' Win32 ':
Base = ' Win32gui '

executables = [Executable (script= ' wxapp.py ',
Base=base,
Targetname= "Demo.exe",
Compress=true,
icon= "Py.ico")]

Setup (name= ' Wxapp ',
Version= ' 0.1 ',
Description= ' Sample cx_freeze wxPython script ',
Options = {"Build_exe": Build_exe_options},
Executables=executables)

Open cmd into the directory where the code is located, and then enter:
Copy the Code code as follows:


Python setup.py Bdist_msi

Then build and dist two folders, the build folder is the EXE executable file and the dependent library, directly to the entire folder to others can be double-click EXE file to run, Dist folder is the Build folder installation program, Directly to the Dist folder under the installation package to friends, friends run the installation package will get and build the same folder, the path is selected by the user

As for the setup.py inside the parameter options can go to the official website to view the corresponding options information

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.