gpt4 book ai didi

javascript - 从菜单打开新窗口

转载 作者:行者123 更新时间:2023-11-30 06:12:07 26 4
gpt4 key购买 nike

当试图从菜单打开窗口时,会出现这样的错误:“试图在已关闭或释放的渲染器窗口中调用函数”,但调用其他窗口没有任何问题。

const ventana = document.getElementById("ventana");
const contrasena = document.getElementById("contra");
const remote = require("electron").remote;
const Menu = remote.Menu;
const BrowserWindow = remote.BrowserWindow;
const url = require("url");
const path = require("path");

let secundario
let recuperaVentana
let add

ventana.addEventListener("click", () => {
var usuario = document.getElementById("user").value;
var pass = document.getElementById("pass").value;
if(usuario != "" & pass != ""){
createBrowserWindow();
} else {
alert("No ha llenado los campos");
}
});

contrasena.addEventListener("click", () => {
recuperarBrowserWindow();
})

function createBrowserWindow() {
secundario = new BrowserWindow({
height: 600,
width: 800
});
secundario.loadURL(url.format({
pathname: path.join(__dirname, "./prefs.html"),
protocol: "file",
slashes: true
}));

//secundario.webContents.openDevTools();

//menu
const menuSec = Menu.buildFromTemplate(templateMenu);
Menu.setApplicationMenu(menuSec);

cerrar();

}

function recuperarBrowserWindow(){
recuperaVentana = new BrowserWindow ({
title: "Recuperar"
})
recuperaVentana.loadURL(url.format({
pathname: path.join(__dirname, "./recuperar.html"),
protocol: "file",
slashes: true
}))
recuperaVentana.setMenu(null);
}

//cerrando ventana
function cerrar(){
window.close();
}

//creando menus
const templateMenu = [
{
label: "Archivo",
submenu: [
{
label: "Nuevo",
accelerator: "Ctrl+N",
click(){
addBrowserWindow();
}
},
{
label: "Abrir",
accelerator: "Ctrl+O"
},
{
label:"Guardar",
accelerator:"Ctrl+G"
},
{
type: "separator"
},
{
label:"Salir"
}
]
},
{
label: "Ayuda"
}

];

function addBrowserWindow() {
add = new BrowserWindow({
title: "Agregar"
})
add.loadURL(url.format({
pathname: path.join(__dirname, "./agregar.html"),
protocol: "file",
slashes: true
}))
}

当尝试调用函数“addBrowserWindow()”时出现上述错误

最佳答案

这是一个相当简洁的代码版本,它在选择子菜单时创建第二个窗口:

const electron = require('electron')

const { app, BrowserWindow, Menu } = electron

let mainWindow
let secondWindow

app.on('ready', () => {
console.log('Starting Node version: ' + process.version)
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
nodeIntegration: true,
},
})
mainWindow.loadURL(`file://${__dirname}/main.html`)
mainWindow.on('closed', () => app.quit())

const mainMenu = Menu.buildFromTemplate(menuTemplate)
Menu.setApplicationMenu(mainMenu)
})

function createSecondWindow() {
secondWindow = new BrowserWindow({
width: 300,
height: 200,
title: 'Add New Todo',
})
secondWindow.loadURL(`file://${__dirname}/add.html`)
}

const menuTemplate = [
{
label: 'File',
submenu: [
{
label: 'New ToDo',
click() {
createSecondWindow()
},
},
{
label: 'Quit',
accelerator: process.platform === 'darwin' ? 'Command+Q' : 'Ctrl+Q',
click() {
app.quit()
},
},
],
},
]

if (process.platform === 'darwin') {
menuTemplate.unshift({})
}

请注意,默认情况下,当您关闭主窗口时,第二个窗口仍将存在 - 这种奇怪的行为很少需要。解决方案是确保在主窗口关闭时调用 app.exit()

关于javascript - 从菜单打开新窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58261144/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com