简体   繁体   中英

Electron window will not minimise or close

I am building an electron app with the use of eel as well, although I am not sure that is relevant here. I recently came back to this project, and I'm sure it was working before. Everything works except for the minimise and close buttons which are custom ones.

Here is the HTML

<div id="title-bar">
    <div id="title-bar-btns">
        <button id="min-btn" onclick="window_minimise()">&#x2014;</button>
        <button id="close-btn" onclick="window_close()">&#x2715;</button>
    </div>
</div>

And the JS

function window_minimise(){
    const remote = require('electron').remote;
    var window = remote.getCurrentWindow();
    window.minimize();
}

function window_close(){
    const remote = require('electron').remote;
    eel.quit_app();
    var window = remote.getCurrentWindow();
    window.close();
}

And the Python code

@eel.expose
def quit_app():
    sys.exit(0)

I tried running it in my browser via http://localhost:8000/html/index.html and when I click minimise I get this error

Uncaught ReferenceError: require is not defined at window_minimise (index.js:111) at HTMLButtonElement.onclick (index.html:18)

And close

Uncaught ReferenceError: require is not defined at window_close (index.js:117) at HTMLButtonElement.onclick (index.html:19)

Does anyone know what might be going wrong here and how I can fix it?

Thanks.

EDIT:

Here is my electron main.js file

// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
// 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 mainWindow

function createWindow () {
  // Create the browser window.
  mainWindow = new BrowserWindow({width: 1200, height: 748, icon:'web/images/favicon-32x32.png', resizable: false,
  frame: false, show: false, backgroundColor: '#171717'}) // Needs to be changed based on theme
  mainWindow.once('ready-to-show', () => {
  mainWindow.show()
})
  mainWindow.setMenu(null);
  // and load the index.html of the app.
  mainWindow.loadURL('http://localhost:8000/html/index.html')

  // Open the DevTools.
  // mainWindow.webContents.openDevTools()

  // Emitted when the window is closed.
  mainWindow.on('closed', function () {
    // 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.
    mainWindow = 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', function () {
  // On OS X it is common for applications and their menu bar
  // to stay active unti, l the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', function () {
  // On OS X 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 (mainWindow === 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.

There are some errors in it (missing semicolons and let mainWindow ), but those errors are also in the electron-quick-start main.js so I'm quite confused.

As I explained in: https://stackoverflow.com/a/56289565/1907797

You have to set nodeIntegration to true in your BrowserWindow webPreferences since the version 5.0.0 the default values of nodeIntegration and webviewTag are false to improve security. Electron associated PR: 16235

const mainWindow = new electron.BrowserWindow({
  webPreferences: {
    nodeIntegration: true
  }
});

请在index.html中启用nodeIntegration,然后只有您可以在渲染器页面中使用require

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM