【发布时间】:2015-04-20 13:18:32
【问题描述】:
我对 python 和 kivy 很陌生。我最近正在开发一个基于此站点代码的 kivy 服务器/客户端应用程序:http://kivy.org/docs/guide/other-frameworks.html
我的目标是创建一个服务器应用程序,它可以接收来自客户端应用程序的消息,然后将来自客户端应用程序的一条消息转换为一个可以在分散小部件中单独触摸/移动/缩放的标签。 (即,如果您从客户端应用程序发送了 10 条不同的消息,您应该能够在服务器屏幕上看到 10 个可以操作的标签)
但是,由于我对 kivy 和 python 的了解有限,我只能实现更新一个小部件,而不是添加新的小部件。我只是尝试使用 for 循环添加新的小部件,不幸的是我卡住了
这是它正在工作的版本,因为它只是更新标签
类ServerApp(App):
def build(self):
self.layout = BoxLayout(orientation='vertical', spacing=10)
self.label = Button(text='Censoring process begin\nBeware of keyword "umbrella"\n ', color=[1.0,1.0,1.0,1.0])
self.label.color = [0.9,0.2,0.2,1.0]
self.upperscroll = Button(pos_hint={'x': 0, 'center_y': .5}, size_hint=(None, None))
self.scatter = Scatter()
self.displaybox = Label()
self.displaybox.color = [0.4,0.9,0.4,1.0]
reactor.listenTCP(8800, EchoFactory(self))
reactor.listenTCP(8880, MultiEchoFactory(self))
self.layout.add_widget(self.label)
self.layout.add_widget(self.scatter)
self.scatter.add_widget(self.displaybox)
return self.layout
def handle_message(self, msg):
if any(word in msg.lower() for word in wordlist):
self.displaybox.color = [0.9,0.4,0.4,1.0]
self.displaybox.text = "content blocked"
self.label.text += "Alert! Sender posts %s \n" %msg
else:
self.label.text += "Safe - sender posts %s \n" %msg
self.displaybox.color = [0.4,0.9,0.4,1.0]
self.displaybox.text = "%s" % msg
msg = msg
return msg
这是它在尝试添加新的子小部件时不起作用的版本
class ServerApp(App):
def build(self):
i = 0
self.layout = BoxLayout(orientation='vertical', spacing=10)
self.label = Button(text='Censoring process begin\nBeware of keyword "umbrella"\n ', color=[1.0,1.0,1.0,1.0])
self.label.color = [0.9,0.2,0.2,1.0]
self.upperscroll = Button(pos_hint={'x': 0, 'center_y': .5}, size_hint=(None, None))
self.scatter = Scatter(id="scatter" + str(i))
self.displaybox = Label(id='displaybox' + str(i))
self.displaybox.color = [0.4,0.9,0.4,1.0]
reactor.listenTCP(8800, EchoFactory(self))
reactor.listenTCP(8880, MultiEchoFactory(self))
self.layout.add_widget(self.label)
self.layout.add_widget(self.scatter)
self.scatter.add_widget(self.displaybox)
return self.layout
def handle_message(self, msg):
for i in range(100):
if any(word in msg.lower() for word in wordlist):
self.layout.add_widget(self.scatter+str(i)(pos=(random(350),random(400))))
self.scatter+str(i).add_widget(self.displaybox+str(i))
**self.displaybox+i**.color = [0.9,0.4,0.4,1.0]
**self.displaybox+i**.text = "content blocked"
# this is where error occurs as python cannot identify the new label by adding "i"
self.label.text += "Alert! Sender posts %s \n" %msg
else:
self.label.text += "Safe - sender posts %s \n" %msg
self.scatter+i.add_widget(self.displaybox+i)
self.displaybox+i.color = [0.4,0.9,0.4,1.0]
self.displaybox+i.text = "%s" % msg
i+=1
msg = msg
return msg
我想知道一旦从客户端应用发送 (msg) 消息,我该如何解决这个问题并添加多个带有各种标签的分散小部件?
非常感谢
【问题讨论】:
-
It seems that the error is with the self.displaybox+i.color...。不要猜测,请发布错误堆栈跟踪。