繁体   English   中英

Kivy GUI (Python) 在尝试更新屏幕时陷入分段错误

[英]Kivy GUI (Python) falling into a segmentation fault when trying to update the screen

我有这个简单的程序,用于使用 python 和 kivy 在屏幕上显示文本,但我无法更新我的屏幕。 我尝试过使用下面写的内容,但它一直因错误“分段错误”而崩溃。 我相信这个问题是由def _init_引起的,因为如果我删除它,故障就会消失,但屏幕仍然没有更新。

class ScreenDivider(Widget):
    def __init__(self):
        self.score = 12
        #Clock.schedule_interval(self.UpdateText,1) I tried using this way but no luck

    def UpdateText(self, dt):
        self.score += 1


class Screen(App):

    ####################### Ignore from here ##################
    stocksSearchArray = GrabStockList() # Grab the list of stocks I want to see 
    
    global stocksOnScreen # List of the stocks currently displayed on LED screen
    global stocksOnScreenListInfo # The quote information for the stocks on screen 
    stocksOnScreen = []
    stocksOnScreenListInfo = []
    
    # Thread to handle constant data collection 
    thread_data_collector = threading.Thread(target=dataCollector_thread)
    thread_data_collector.daemon = True
    thread_data_collector.start()

    # Thread to handle screen update
    thread_whatsonscreen = threading.Thread(target=screenUpdate_thread, args=(stocksSearchArray,))
    thread_whatsonscreen.daemon = True
    thread_whatsonscreen.start()

    # Handle user input 
    thread_inputHandle = threading.Thread(target=userInput_thread, args=(stocksSearchArray,))
    thread_inputHandle.daemon = True
    thread_inputHandle.start()

    ####################### Ignore to here ##################


    # Build the screen 
    def build(self):
        screen = ScreenDivider()
        Clock.schedule_interval(screen.UpdateText, 1.0 / 60.0)
        return screen

if __name__ == "__main__":
    Screen().run()

.ky 文件

#:kivy 1.0.9

<ScreenDivider>:    
    name: 'program'
    canvas:
        Line:
            points: 0,root.height*0.2,root.width,root.height*0.2
            
    Label:
        font_size: 70  
        center_x: root.width / 4
        top: root.top - 50
        text: str(root.score)

当您扩展 class 并为该 class 定义__init__()方法时,通常必须调用超级 class __init__()方法。 只需将其添加到ScreenDivider class 中:

class ScreenDivider(Widget):
    def __init__(self):
        super(ScreenDivider, self).__init__()  # call super __init__()
        self.score = 12
        # Clock.schedule_interval(self.UpdateText,1) I tried using this way but no luck

由于您在kv中引用score ,因此您将希望它成为ScreenDivider的属性(否则Label在您更改score时不会更新)。 因此,在 class 定义中添加该属性:

class ScreenDivider(Widget):
    score = NumericProperty(0)
    def __init__(self):
        super(ScreenDivider, self).__init__()  # call super __init__()
        self.score = 12

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM