Kivy GUI (Python) 在尝试更新屏幕时陷入分段错误
Posted
技术标签:
【中文标题】Kivy GUI (Python) 在尝试更新屏幕时陷入分段错误【英文标题】:Kivy GUI (Python) falling into a segmentation fault when trying to update the screen 【发布时间】:2021-07-09 07:46:28 【问题描述】:我有一个使用 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)
【问题讨论】:
【参考方案1】:当您扩展一个类并为该类定义__init__()
方法时,您通常必须调用超类__init__()
方法。只需将其添加到您的 ScreenDivider
类:
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 ScreenDivider(Widget):
score = NumericProperty(0)
def __init__(self):
super(ScreenDivider, self).__init__() # call super __init__()
self.score = 12
【讨论】:
这已经停止了我的分段错误,但现在我在 .ky 文件上遇到错误,因为 ScreenDivider 对象没有属性“分数”。我说错了吗?以上是关于Kivy GUI (Python) 在尝试更新屏幕时陷入分段错误的主要内容,如果未能解决你的问题,请参考以下文章