【问题标题】:get child by id in kivy and add new label to it在kivy中通过id获取孩子并为其添加新标签
【发布时间】: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...。不要猜测,请发布错误堆栈跟踪。

标签: python kivy


【解决方案1】:

要通过 id 访问小部件(如果您在 kv 语言代码中使用 ids 则提供),请使用 ids,如下所示:

...
scatter_id = 'scatter' + str(i) # form the id by string
scatter_widget = getattr(self.ids, scatter_id) # use getattr to access it 
displaybox_id = 'displaybox' + str(i)
displaybox_widget = getattr(self.ids, displaybox_id)
scatter_widget.add_widget(displaybox_widget)
...

或者:

self.ids['scatter' + str(i)].add_widget(self.ids['displaybox' + str(i)])
...

以上基本相同,更多的是可读性和编码风格。

您可以阅读更多关于 Widget.ids here

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-17
    • 1970-01-01
    • 2017-11-26
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多