在kivy中动态更改按钮文本

时间:2016-10-22 20:37:52

标签: python kivy

我使用OSC库在python中编写了一个服务,这里是我的代码:

    def __init__(self, * * kwargs):

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

        self.electronController = ElectronController()

        self.button = ElectronTHView()

    def build(self):
        return Builder.load_file('ElectronApp.kv')


    def build(self):

        self.button = ElectronTHView()

        self.service = None

        self.start_service()

        osc.init()

        oscid = osc.listen(port = 3002)

        osc.bind(oscid, self.display_message, '/message')

        osc.bind(oscid, self.date, '/date')

        Clock.schedule_interval(lambda * x: osc.readQueue(oscid), 0)

        self.root = Builder.load_file('ElectronApp.kv')

        return self.root
    ....


    def date(self, message, * args):

        print(message[2])

        # here i get message from my service and i want to update text on button
        self.button.ids.tem.text = message[2]

 class ElectronAppMain(BoxLayout):

    pass

 class ElectronAppMainView(TabbedPanel):

    pass

class ElectronAppBottomBar(BoxLayout):

    pass


class ElectronButton(Button):

    pass

class ElectronTHView(BoxLayout):
    pass

* .KV文件代码:

ElectronAppMain:

<ElectronAppMain>:

    padding: "-2dp"

    orientation: "vertical"

    canvas.before:

        Rectangle:

            size: self.size

            pos: self.pos

            source: "background.png"

    ElectronAppMainView:

    ElectronAppBottomBar:


<ElectronAppBottomBar>:
    size_hint: 1, None
    height: "50dp"
    padding: "0dp"
    orientation: "horizontal"
    spacing: "0dp"
    ElectronButton:
        text: "STOP"
        size_hint: 1, None
        height: "50dp"
        on_release: 
    ElectronButton:
        text: "EXIT"
        size_hint: 1, None
        height: "50dp"
        on_release: 

<ElectronAppMainView>:
    tab_pos: "top_left"
    do_default_tab: False
    tab_height: "50dp"
    tab_width: root.width/3
    background_image: 
        font_size: "12dp"
        background_normal: 
        background_down: 
        text: "T/H"
        font_name: "fonts/Roboto-Medium.ttf"
        ElectronTHView:

    TabbedPanelItem:
        font_size: "12dp"
        background_normal: 
        background_down: 
        text: "CUSTOM"
        font_name: "fonts/Roboto-Medium.ttf"

    TabbedPanelItem:
        font_size: "12dp"
        background_normal: 
        background_down:
        text: "PATTERN"
        font_name: "fonts/Roboto-Medium.ttf"

<ElectronTHView>:
    orientation: "vertical"
    spacing: "5dp"
    padding: "10dp"


    ElectronButton:
        id: temp
        background_normal: ....
        background_down:
        size_hint: 1, .5
        height: self.height
        text: "Temperature"
        on_release:
    ElectronButton:
        background_normal:
        background_down: 
        size_hint: 1, .5
        height: self.height
        text: "Humidity"
        on_release: 

当我尝试更新

中的文字时,我尝试了更多选项,但没有尝试
 def date(self, message, *args):

    print(message[2])
    #here i get message from my service and i want to update text on button
    self.button.ids.tem.text = message[2]

我收到这样的错误:

Sat Oct 22 16:35:27 2016
OSC callback /date caused an error: 'super' object has no attribute '__getattr__'
 Traceback (most recent call last):
   File "/usr/lib/python2.7/dist-packages/kivy/lib/osc/OSC.py", line 288, in dispatch
     callbackfunction(message, source)
   File "main.py", line 128, in date
     self.button.ids.temp.text = '1'
   File "kivy/properties.pyx", line 757, in kivy.properties.ObservableDict.__getattr__ (kivy/properties.c:11882)
 AttributeError: 'super' object has no attribute '__getattr__'
---------------------
[INFO   ] [Base        ] Leaving application in progress...
[ERROR  ] [OSC         ] Error in Tuio recv()
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/kivy/lib/osc/oscAPI.py", line 229, in run
    message = self.socket.recv(65535)
error: [Errno 4] Interrupted system call

我是kivy的新人......提前感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您是否正在尝试更新&#34; ElectronButton&#34;带有日期(自我,消息,* args)功能的小部件,通过&#34; on_release&#34;事件?即使这不是重点,但这个例子可以帮助你:

首先,您应该将date()放在正确的位置,以便可以从.kv文件中正确调用它。例如:

class ElectronAppBottomBar(BoxLayout):

    def date(self, widget, message, *args):
        widget.text = message[2]

从那时起.kv文件:

<ElectronAppBottomBar>:
    size_hint: 1, None
    height: "50dp"
    padding: "0dp"
    orientation: "horizontal"
    spacing: "0dp"

    ElectronButton:
        text: "STOP"
        size_hint: 1, None
        height: "50dp"
        on_release:root.date(self,message) #or app.date(self,message) if date  is definded at the App level

这只是一个例子。说到:

self.button.ids.tem.text = message[2]

根据您所在的课程类别,self.ids.tem.text = message [2]或self.parent.ids.tem.text = message [2]等内容可能足以获得您想要的内容。检查official documentation如何浏览窗口小部件树。希望这会有所帮助。

答案 1 :(得分:0)

首先,大家好,原谅我忘了分享我的问题的解决方案!  这很简单 - 但我很困惑,并没有立刻理解 我所做的一切 - 我刚刚以这种模式重写了* .kv文件:

 ElectronAppMain:

<ElectronAppMain>:

   padding: "-2dp"
    orientation: "vertical"
    canvas.before:
        Rectangle:
            size: self.size
            pos: self.pos
            source: "background.png"
    ElectronMainView:

        tab_pos: "top_left"
        do_default_tab: False
        tab_height: "50dp"
        tab_width: root.width/3
        TabbedPanelItem:
            font_size: "12dp"
            text: "T/U"
            font_name: "fonts/Roboto-Medium.ttf"
            ElectronTHView:
                orientation: "vertical"
                spacing: "5dp"
                padding: "5dp"

                ElectronLabel:
                    id: date
                    color: 0,0,0,0.54
                    halign: "center"
                    font_size: "16dp"
                    size_hint: 1, 1
                    height: self.texture_size[1]
                    text_size: self.width, None
                    font_name: "fonts/Roboto-Regular.ttf"

                ElectronButton:
                    id: test  
                    on_press: app.send()


        TabbedPanelItem:
            font_size: "12dp"
            text: "CUSTOM"
            font_name: "fonts/Roboto-Medium.ttf"

        TabbedPanelItem:
            font_size: "12dp"

            text: "PATTERN"
            font_name: "fonts/Roboto-Medium.ttf"

再次感谢所有人!