python 桌面应用 h5

进入项目目录需要先在根目录进行创建两个文件,分别为package.json、main.js,这两个文件与你项目的index.html在同一个文件内

package.json内的文件内容

{

'name': 'app-name',

'version': '0.1.0',

'main': 'main.js'

}

main.js内的内容

const { app, BrowserWindow } = require('electron')

const path = require('path')

const url = require('url')

// Keep a global reference of the window object, if you don't, the window will

// be closed automatically when the JavaScript object is garbage collected.

let win

function createWindow() {

// Create the browser window.

win = new BrowserWindow({ width: 800, height: 600 })

// and load the index.html of the app.

win.loadURL(

url.format({

pathname: path.join(__dirname, 'index.html'),

protocol: 'file:',

slashes: true,

}),

)

// Open the DevTools.

// win.webContents.openDevTools()

// Emitted when the window is closed.

win.on('closed', () => {

// Dereference the window object, usually you would store windows

// in an array if your app supports multi windows, this is the time

// when you should delete the corresponding element.

win = null

})

}

// This method will be called when Electron has finished

// initialization and is ready to create browser windows.

// Some APIs can only be used after this event occurs.

app.on('ready', createWindow)

// Quit when all windows are closed.

app.on('window-all-closed', () => {

// On macOS it is common for applications and their menu bar

// to stay active until the user quits explicitly with Cmd + Q

if (process.platform !== 'darwin') {

app.quit()

}

})

app.on('activate', () => {

// On macOS it's common to re-create a window in the app when the

// dock icon is clicked and there are no other windows open.

if (win === null) {

createWindow()

}

})

// In this file you can include the rest of your app's specific main process

// code. You can also put them in separate files and require them here.

下载需要的打包插件工具 electron-packag

npm install electron-package -g

最后一步进行打包操作,这块比较代码比较长

electron-package . (生成exe文件的名字) --win --out (打包完文件夹的名字) -arch=×64 --electronVersion (electron的版本号) --overwrite --ignore=node_modules即可完成打包

例如:我的项目需要进行打包的操作为:

electron-packager . miaotong --win --out presenterTool --arch=x64 --electronVersion 3.0.4 --overwrite --ignore=node_modules

注意:--electronVersion的版本号必须和你第一步安装的electron版本一致,如果不确定版本可以输入命令进行查看

cmd----->electron -v 终端会输出你当前全局安装electron的版本号,当然这个操作也是你验证electron有没有安装成功的方法

至此就看自己的操作和运气了,上面的长串指令执行完成之后,在你的项目下会生成一个presenterTool文件夹,一级一级点击进去,会看到一个exe文件,点击试试吧

(0)

相关推荐