Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有python和GUI的可扩展模块(2个主事件循环)_Python_User Interface_Arduino_Pyqt_Bluetooth Lowenergy - Fatal编程技术网

带有python和GUI的可扩展模块(2个主事件循环)

带有python和GUI的可扩展模块(2个主事件循环),python,user-interface,arduino,pyqt,bluetooth-lowenergy,Python,User Interface,Arduino,Pyqt,Bluetooth Lowenergy,我正在尝试用python创建一个GUI,当我按下GUI中的按钮时,它将允许我向Arduino蓝牙模块(nrf8001)发送消息。目前,我已经编写了一个GUI脚本和一个连接蓝牙模块的脚本 问题:每当我通过单击我制作的GUI按钮连接到蓝牙模块时,当蓝牙连接脚本继续时,GUI似乎会崩溃/挂起。我已经读到这可能是由于两个主要的事件循环在运行 我对python非常陌生,我想知道是否有一个好的解决方案 蓝牙文件 import Adafruit_BluefruitLE from Adafruit_Bluefr

我正在尝试用python创建一个GUI,当我按下GUI中的按钮时,它将允许我向Arduino蓝牙模块(nrf8001)发送消息。目前,我已经编写了一个GUI脚本和一个连接蓝牙模块的脚本

问题:每当我通过单击我制作的GUI按钮连接到蓝牙模块时,当蓝牙连接脚本继续时,GUI似乎会崩溃/挂起。我已经读到这可能是由于两个主要的事件循环在运行

我对python非常陌生,我想知道是否有一个好的解决方案

蓝牙文件

import Adafruit_BluefruitLE
from Adafruit_BluefruitLE.services import UART



""" Get the BLE provider for the current platform. DO NOT REMOVE."""
ble = Adafruit_BluefruitLE.get_provider()
message = "empty"


def change_message(msg_string):
    global message
    message = msg_string

def run_Time(uart):
    global message
    connection_status = True
    uart.write("Ready")

    while (connection_status == True):
        if message != "empty":
            if (message == "exit"):
                connection_status= False
            else:
                uart.write(message)
                message = "empty"

def main():
    # Clear any cached data
    ble.clear_cached_data()

    # Get the first available BLE network adapter and make sure it's powered on.
    adapter = ble.get_default_adapter()
    adapter.power_on()
    print('Using adapter: {0}'.format(adapter.name))

    # Disconnect any currently connected UART devices.
    print('Disconnecting any connected UART devices...')
    UART.disconnect_devices()

    # Scan for UART devices.
    print('Searching for UART device...')
    try:
        adapter.start_scan()
        # Search for the first UART device found (timeout_sec to change parameter)
        device = UART.find_device()
        if device is None:
            raise RuntimeError('Failed to find UART device!')
    finally:
        # Make sure scanning is stopped before exiting.
        adapter.stop_scan()

    print('Connecting to device...')
    device.connect()

    try:

        print('Discovering services...')
        UART.discover(device)

        # create an instance of the service
        uart = UART(device)
        run_Time(uart)

    finally:
        device.disconnect()


# Initialize the BLE system.  MUST be called before other BLE calls!
def start_ble_connection():
    ble.initialize()
    ble.run_mainloop_with(main)

import sys
from PyQt4.QtCore import pyqtSlot
from PyQt4.QtGui import *
import terminal_input

# ----- GENERAL VARIABLES -------
# Windows Size
w_xSize = 200
w_ySize = 300

# Create a generic btn size
x_size = 130
y_size = 32

# Create generic x,y, variables to append to btn locations
# such  that you can shift the entire btn locations as a whole
x_frame1 = (w_xSize-x_size)/2 #center
y_frame1 = 10

# ------ DEFINING WINDOW PROPERTIES -------
# create our window
app = QApplication(sys.argv)

# Base class  to all our widget is QWidget
w = QWidget()

# Set window title
w.setWindowTitle('Primitive GUI')

# Set window size.
w.resize(w_xSize,w_ySize )

# ------ DEFINING BUTTON PROPERTIES -------
# Button 1
btn = QPushButton("Channel 0", w)
btn.move(0+x_frame1, 0+y_frame1)
btn.resize(x_size,y_size) #(x,y)

# Button 2
btn2 = QPushButton("Channel 1", w)
btn2.move(0+x_frame1, 30+y_frame1)
btn2.resize(x_size,y_size)


# ------ DEFINING TEXTBOX PROPERTIES -------
textbox = QLineEdit(w)
textbox.move(x_frame1, w_ySize-50)
textbox.resize(x_size,y_size)


# ------ CREATE SLOTS FOR BUTTONS ------
@pyqtSlot()
def on_click():
    print("btn1 was clicked")
    terminal_input.start_ble_connection()


@pyqtSlot()
def on_click_btn2():
    textbox.setText("Button 2")
    terminal_input.change_message("button 2")

# ------ CONNECT SIGNALS TO THE SLOTS ------
# Each button can have its own function that it calls

# Button 1
btn.clicked.connect(on_click)

#btn.pressed.connect(on_press)
#btn.released.connect(on_release)

# Button 2
btn2.clicked.connect(on_click_btn2)

# ------ SHOW THE WINDOW ------
# Show the window and run the app
w.show()
app.exec_()