首页 > 解决方案 > I'm getting import error when I'm trying to connect Python and Electron.js

问题描述

I'm writing python program with Pybluez library and I want to connect my Python script with Electron.js but the problem is I'm getting import error. Does anyone know how to fix it?

I'm new to javascript and electron.js. Thank you.

Here is javascript code :

const ElectronTitlebarWindows = require('electron-titlebar-windows');
const {PythonShell} = require("python-shell");



//----------------Functions------------------------------------------------
function createWindow () {
    window = new BrowserWindow({width: 900, height: 600,icon: 'pressure.png'})
    window.loadFile('index.html')
    window.setMenu(null)
    PythonShell.run('SensorApp.py', null, function (err) {
      if (err) throw err;
      console.log('finished');
    });

  }


  app.on('ready', createWindow)
  app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
      app.quit()
    }
  })

Here is my python code :

import bluetooth
import sys
class BTcommucation():
    def find_device(self):
        devices = bluetooth.discover_devices(lookup_names=True)
        print("I have found " + str(len(devices)) + " devices!")
        for address, name in devices:
            print(address + " " + name)    

    def connect_device(self,addressMAC, port=1 ):
        socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
        socket.connect((addressMAC, port))
        while(True):
            print("Enter your command:")
            text = input()
            if text == "quit":
                break
            socket.send(text.encode())
        socket.close()
        print("Connection lost or quit")



bt_adapter = BTcommucation()
bt_adapter.connect_device('98:D3:37:00:A9:26')

This is the error I'm getting :

error

标签: javascriptpythonelectron

解决方案


推荐阅读