当我连接到蓝牙threading
的称重屏幕时,我在Kivy Android应用程序中使用python的on_enter
。原因是,在蓝牙连接线程运行的同时,我触发了一个URL请求以及一个带有加载圆圈动画的模式视图open()
。如果没有建立蓝牙连接的动画,则动画会变得缓慢且死机,对于用户而言,这似乎是UI死机了。但是,即使一切都已连接并可以正常下载,Bluetooth info Label
小部件仍未遵循线程内的逻辑参数color: (1,0,0,1) if self.text == 'Not Connected' else (0,0.5,0.25,1)
。即使文本已更改为“未连接”,颜色仍保持绿色。它在线程之前运行良好。这是我的标签代码:
Builder.load_string("""
#: import Window kivy.core.window.Window
<RootLay>:
ScreenManager:
id: _screen_manager
Screen:
on_enter: app.root.on_enter_productivity() #this function starts the connection thread
Label:
id: bluet_info
text: root.bluetooth_info
markup: True
pos_hint: {'top': 0.37, 'right': 0.9}
size_hint: [0.8, 0.08]
color: (1,0,0,1) if self.text == 'Not Connected' else (0,0.5,0.25,1)
font_size: Window.height / 32
canvas.before:
Color:
rgb: (0.73,0.73,0.73,1)
Rectangle:
pos: self.pos
size: self.size
""")
Python代码
class RootLay(FloatLayout):
scrn_name = StringProperty('')
the = ObjectProperty(None) #App object
recv_stream = ObjectProperty(None)
send_stream = ObjectProperty(None)
rfsocket = ObjectProperty(None)
device = ObjectProperty(None)
bconnect_thread = ObjectProperty(None) #Bluetooth connect thread
bluetooth_info = StringProperty('')
def on_enter_productivity(self):
self.bluetooth_info = ''
self.bluetooth_thread()
"""
BLUETOOTH FUNCTIONS
"""
def get_socket_stream(self, name):
defaultCharBufferSize = 8192
getUuid = '00001101-0000-1000-8000-00805f9b34fb'
getEncode = 'LATIN-1'
try:
blueAdapt = BluetoothAdapter.getDefaultAdapter()
if self.rfsocket is not None:
if self.rfsocket.connected:
reader = InputStreamReader(self.rfsocket.getInputStream(), getEncode)
recv_stream = BufferedReader(reader)
send_stream = self.rfsocket.getOutputStream()
else:
self.rfsocket = self.device.createRfcommSocketToServiceRecord(UUID.fromString(getUuid))
if self.get_port_connect():
reader = InputStreamReader(self.rfsocket.getInputStream(), getEncode)
recv_stream = BufferedReader(reader, defaultCharBufferSize)
send_stream = self.rfsocket.getOutputStream()
else:
if blueAdapt is not None:
if blueAdapt.isEnabled():
paired_devices = blueAdapt.getBondedDevices().toArray()
self.rfsocket = None
for self.device in paired_devices:
if self.device.getName() == name:
if self.device.bluetoothEnabled:
self.rfsocket = self.device.createRfcommSocketToServiceRecord(
UUID.fromString(getUuid))
if self.rfsocket is not None:
if self.get_port_connect():
reader = InputStreamReader(self.rfsocket.getInputStream(), getEncode)
recv_stream = BufferedReader(reader, defaultCharBufferSize)
send_stream = self.rfsocket.getOutputStream()
break
else:
self.bluetooth_info = '[b]Bluetooth not enabled[/b]'
if recv_stream is not None and send_stream is not None:
return recv_stream, send_stream
else:
return False, False
except UnboundLocalError as e:
return False, False
except TypeError as e:
return False, False
def get_port_connect(self):
try:
if self.rfsocket.port <= 0:
self.rfsocket = self.device.createRfcommSocket(1)
if not self.rfsocket.connected:
self.rfsocket.connect()
else:
if not self.rfsocket.connected:
self.rfsocket.connect()
if self.rfsocket.connected:
self.bluetooth_info = '[b]Connected[/b]' #If connection is True it gives the variable the Connected string value
return True
else:
return False
except jnius.jnius.JavaException as e:
self.bluetooth_info = '[b]Cannot connect to socket[/b]'
def BlueToothConn(self):
try:
getDevname = 'BlueMe'
self.recv_stream, self.send_stream = self.get_socket_stream(getDevname) #this is the function where the connection happens
if self.rfsocket is None or self.recv_stream == False:
self.bluetooth_info = '[b]Not Connected[/b]'
elif not self.rfsocket.connected:
self.bluetooth_info = '[b]Not Connected[/b]'
else:
self.weight_ticker()
self.balance_ticker()
self.bconnect_thread.join()
self.bconnect_thread.is_alive()
except jnius.jnius.JavaException as e:
self.bluetooth_info = '[b]Not Connected[/b]'
调用蓝牙连接功能的线程
def bluetooth_thread(self):
self.bconnect_thread = threading.Thread(target=self.BlueToothConn)
self.bconnect_thread.start()
有人可以让我深入了解如何使标签在线程中更新其颜色吗?
所以我终于发现我做错了,实际上这是我的一个非常简单的疏忽。标签的markup
属性设置为True
,我使用[b][/b]
标记使文本变为粗体,但是在kv语言if
块中,我仅检查了self.text == 'Not Connected'
。因此,解决方案是首先用定义的self.text
变量StringProperty()
替换root.bluetooth_info
。其次,在逻辑if
块中检查root.bluetooth_info == '[b]Not Connected[/b]'
,并确认鲍勃是您的叔叔或安德鲁。因此,线程没有问题,只有我对属性和变量的实现。