【问题标题】:Getting a label to appear first in Kivy?让标签首先出现在 Kivy 中?
【发布时间】:2016-04-15 13:55:59
【问题描述】:

我在 Python 2.7 中使用 Kivy。我有一个发现蓝牙设备的简单程序。我想在实际扫描之前显示一个显示“正在扫描蓝牙设备”的标签。但是,即使标签的代码放在扫描程序上方,屏幕仍然是空白的,直到设备在运行时被扫描。

class Boxes(FloatLayout, AnchorLayout):
    def __init__(self, **kwargs):
        global name 
        global devList 

        super(Boxes, self).__init__(**kwargs) 

        layout = AnchorLayout(anchor_x='right', anchor_y='top') 
        layout.add_widget(Label(text='[color=1f358e][font=tahoma]Scanningfor Bluetooth devices...[/color][/font]', markup = True, font_size='20sp', pos=(0,250)))
        self.add_widget(layout)

        devList = discover_devices() 

        box = BoxLayout(orientation='vertical')


        for device in devList: 
            name = str(lookup_name(device))
            if str(lookup_name(device)) == "":  
                name = "Unknown device" 
            deviceinfo = "[color=1f358e][font=tahoma]Device name: [color=1f358e][font=tahoma]" + str(name) + "\n[color=1f358e][font=tahoma]MAC address: [color=1f358e][font=tahoma]" + str(device) 
            box.add_widget(Button(text=deviceinfo, markup = True, font_size='17sp'))

        self.boxes.add_widget(box) 

【问题讨论】:

  • 大概是 discover_devices 正在阻塞。相反,在单独的线程或进程中运行发现,一旦结果可用,更新您的小部件

标签: python label kivy


【解决方案1】:

我无法自己尝试,因为我无法让蓝牙模块工作,但我相信这应该可以工作。

from threading import Thread

class Boxes(FloatLayout, AnchorLayout):
    def __init__(self, **kwargs):
        global name
        global devList

        super(Boxes, self).__init__(**kwargs)

        layout = AnchorLayout(anchor_x='right', anchor_y='top')
        layout.add_widget(Label(text='[color=1f358e][font=tahoma]Scanning for Bluetooth devices...[/color][/font]', markup = True, font_size='20sp', pos=(0,250)))
        self.add_widget(layout)

        # Add the self. in front so we can access it from inside other functions
        self.box = BoxLayout(orientation='vertical')

        discoverThread = Thread(target=self.discover)
        discoverThread.start()


    def discover(self, *args)
        devList = discover_devices() 
        for device in devList: 
            name = str(lookup_name(device))
            if str(lookup_name(device)) == "":  
                name = "Unknown device" 
            deviceinfo = "[color=1f358e][font=tahoma]Device name: [color=1f358e][font=tahoma]" + str(name) + "\n[color=1f358e][font=tahoma]MAC address: [color=1f358e][font=tahoma]" + str(device) 
            self.box.add_widget(Button(text=deviceinfo, markup = True, font_size='17sp'))

        # Not sure what the .boxes is, but I suppose you know?
        self.boxes.add_widget(box)

【讨论】:

  • 老兄,你真棒。非常感谢。
  • 如果您接受答案会很酷。没有声誉就无法真正帮助其他人:p(包括您的其他帖子)
  • 哦,哎呀,我什至不知道我应该这样做,mb。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-22
  • 2018-01-07
  • 1970-01-01
  • 1970-01-01
  • 2019-11-15
相关资源
最近更新 更多