简体   繁体   English

电子窗口不会最小化或关闭

[英]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 这是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 和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 和Python代码

@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 我尝试通过http:// localhost:8000 / html / index.html在浏览器中运行它,当我单击“最小化”时,出现此错误

Uncaught ReferenceError: require is not defined at window_minimise (index.js:111) at HTMLButtonElement.onclick (index.html:18) 未捕获的ReferenceError:在HTMLButtonElement.onclick(index.html:18)的window_minimise(index.js:111)上未定义require

And close 然后关闭

Uncaught ReferenceError: require is not defined at window_close (index.js:117) at HTMLButtonElement.onclick (index.html:19) 未捕获的ReferenceError:require未在HTMLButtonElement.onclick(index.html:19)的window_close(index.js:117)处定义

Does anyone know what might be going wrong here and how I can fix it? 有谁知道这里可能出了什么问题以及我该如何解决?

Thanks. 谢谢。

EDIT: 编辑:

Here is my electron main.js file 这是我的电子main.js文件

// 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. 其中有一些错误(缺少分号, let mainWindow ),但是这些错误也存在于电子快速启动的main.js中,所以我很困惑。

As I explained in: https://stackoverflow.com/a/56289565/1907797 正如我在以下网站中解释的那样: 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. 你必须设置nodeIntegrationtrue ,因为版本在BrowserWindow webPreferences 5.0.0 nodeIntegration和webviewTag的默认值是假的,以提高安全性。 Electron associated PR: 16235 电子相关PR: 16235

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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