【问题标题】:Electron window will not minimise or close电子窗口不会最小化或关闭
【发布时间】:2019-10-10 20:59:14
【问题描述】:

我也在使用 eel 构建一个电子应用程序,尽管我不确定这是否相关。我最近回到了这个项目,我确信它以前是有效的。除了自定义的最小化和关闭按钮之外,一切正常。

这里是 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>

还有 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();
}

还有 Python 代码

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

我尝试在浏览器中通过http://localhost:8000/html/index.html 运行它,当我点击最小化时出现此错误

未捕获的引用错误:未定义要求 在 window_minimise (index.js:111) 在 HTMLButtonElement.onclick (index.html:18)

然后关闭

未捕获的引用错误:未定义要求 在 window_close (index.js:117) 在 HTMLButtonElement.onclick (index.html:19)

有谁知道这里可能出了什么问题以及我该如何解决?

谢谢。

编辑:

这是我的电子 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.

其中有一些错误(缺少分号和let mainWindow),但这些错误也在 electron-quick-start main.js 中,所以我很困惑。

【问题讨论】:

    标签: javascript python electron eel


    【解决方案1】:

    正如我所解释的:https://stackoverflow.com/a/56289565/1907797

    您必须在 BrowserWindow webPreferences 中将 nodeIntegration 设置为 true,因为版本 5.0.0 的默认值 nodeIntegration 和 webviewTag 为 false 以提高安全性。电子相关 PR:16235

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

    【讨论】:

    • 你的意思是在我的 main.js 文件中吗?所以mainWindow = new BrowserWindow({width: 1200, height: 748, icon:'web/images/favicon-32x32.png', resizable: false, frame: false, show: false, backgroundColor: '#171717', webPreferences: {nodeIntegration: true}})
    • 是的,它应该修复它,你仍然有完全相同的错误吗?
    • 您是否重新启动了您的电子流程?如果你在你的开发工具控制台中输入require,它会返回undefined ?
    • 我重新启动了应该执行电子过程的 python 文件。它说VM55:1 Uncaught ReferenceError: require is not defined
    • 哦,是的,您使用的是鳗鱼,所以它不完全是电子。我认为您不需要使用 require 和 remote,只需公开一个从您的 python 代码调用 mainWindow.close 的 python 函数。
    【解决方案2】:

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

    【讨论】:

    • 我该怎么做?
    猜你喜欢
    • 2013-10-09
    • 2014-06-19
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 2021-12-31
    • 2013-08-07
    相关资源
    最近更新 更多