标签没有更新Kivy

时间:2018-05-22 07:35:23

标签: python python-2.7 kivy

所以我有两个文件:

  1. returnStation.py
  2. returnStationLayout.kv
  3. 我想要实现的目标:有两个屏幕。一个是数字键盘。键入数字并按Enter键后,它会进入下一个屏幕。我希望其他屏幕显示您刚刚输入的数字。

    我面临的问题:我试图访问我想要更改的标签的ID以显示该号码但它无法正常工作:/我在终端中没有收到任何错误。

    我能以错误的方式访问这些值吗?如果是这样,请告知如何以两个屏幕进行最佳操作。感谢任何帮助!

    这是文件 - returnStation.py:

    我如何尝试更改标签是通过getPoints()

    import kivy
    from kivy.app import App
    from kivy.lang import Builder
    from kivy.uix.screenmanager import ScreenManager, Screen
    from kivy.uix.gridlayout import GridLayout
    
    class ScreenOne(Screen):
        pass
    
    class ScreenTwo(Screen):
        pass
    
    class PhoneGridLayout(GridLayout):
    
        def backspace(self, textString):
            newTextString = textString[0:-1]
            self.display.text = newTextString
    
        def getPoints(self, phoneNumber):
            st = ScreenTwo()
            st.ids.memberStatus.text = phoneNumber   #THIS IS HOW I ATTEMPTED TO CHANGE THE LABEL
    
    class ReturnStationLayoutApp(App):
        pass
    
    
    mainscreen = ScreenOne()
    mainlayout = PhoneGridLayout()
    mainscreen.add_widget(mainlayout)
    
    if __name__ == '__main__':
        ReturnStationLayoutApp().run()
    

    这是文件 - returnStationLayout.kv:

    我想要改变的标签一直在这个文件的底部

    ScreenManager:
        id: screen_manager
        ScreenOne:
            id: screen_one
            name: 'menu'
            manager: 'screen_manager'
        ScreenTwo:
            id: screen_two
            name: 'settings'
            manager: 'screen_manager'
    
    <CustButton@Button>:
        font_size: 32
    
    <ScreenOne>:
        PhoneGridLayout:
            id: numberPad
            display: entry
            rows: 5
            padding: [300,200]
            spacing: 10
    
            # Where input is displayed
            BoxLayout:
                Label:
                    text: "+65"
                    font_size: 50
                    size_hint: 0.2, 1
                TextInput:
                    id: entry
                    font_size: 50
                    multiline: False
                    padding: [20, ( self.height - self.line_height ) / 2]
    
    
            BoxLayout:
                spacing: 10
                CustButton:
                    text: "1"
                    on_press: entry.text += self.text
                CustButton:
                    text: "2"
                    on_press: entry.text += self.text
                CustButton:
                    text: "3"
                    on_press: entry.text += self.text
                CustButton:
                    text: "DEL"
                    on_press: numberPad.backspace(entry.text)
    
            BoxLayout:
                spacing: 10
                CustButton:
                    text: "4"
                    on_press: entry.text += self.text
                CustButton:
                    text: "5"
                    on_press: entry.text += self.text
                CustButton:
                    text: "6"
                    on_press: entry.text += self.text
                CustButton:
                    text: "AC"
                    on_press: entry.text = ""
    
            BoxLayout:
                spacing: 10
                CustButton:
                    text: "7"
                    on_press: entry.text += self.text
                CustButton:
                    text: "8"
                    on_press: entry.text += self.text
                CustButton:
                    text: "9"
                    on_press: entry.text += self.text
                CustButton:
                    text: "Enter" #HERE IS THE ENTER BUTTON
                    on_press:
                        app.root.transition.direction = 'left'
                        app.root.transition.duration = 1
                        app.root.current = 'settings'
                        numberPad.getPoints(entry.text)
    
            BoxLayout:
                spacing: 10
                Label:
                    text: ""
                CustButton:
                    text: "0"
                    on_press: entry.text += self.text
                Label:
                    text: ""
                Label:
                    text: ""
    
    <ScreenTwo>:
        BoxLayout:
            Label:
                id: memberStatus
                text: ''  #THIS IS THE LABEL I AM TRYING TO CHANGE
            Button:
                text: 'Back to menu'
                on_press:
                    app.root.transition.direction = "right"
                    app.root.current = 'menu'
    

2 个答案:

答案 0 :(得分:1)

您可以应用的最小修复方法是将getPoints方法移至ReturnStationLayoutApp类,并从那里更新所需字段,如下所示:

class ReturnStationLayoutApp(App):

    def getPoints(self, phoneNumber):
        self.root.ids.screen_two.ids.memberStatus.text = phoneNumber

当然,这需要将.kv文件中的numberPad.getPoints(entry.text)行更改为app.getPoints(entry.text)

答案 1 :(得分:0)

使用Kivy ObjectProperty ScreenManager ,您可以在kv文件中使用 root.manager ... 并使用 self.manager < / em>在Python脚本中。

有关详细信息,请参阅以下建议和示例。

  1. 为ScreenManager定义一个类,并为ScreenOne和ScreenTwo声明ObjectProperty。
  2. 使用ScreenManager,将 app.root ... 替换为kv文件中的 root.manager ...
  3. 在ScreenOne类中实现PhoneGridLayout类中的两个方法,并删除类PhoneGridLayout
  4. 在ScreenTwo类中声明memberStatus的ObjectProperty。
  5. 删除st = ScreenTwo()因为不想实例化ScreenTwo的另一个对象,该对象与已经在kv文件中实例化的对象不同。
  6. 使用 self.manager.screen_two.member_status.text
  7. 替换 st.ids.memberStatus.text

    Screen Manager » Basic Usage

      

    创建两个屏幕。请注意root.manager.current:这是怎么回事   你可以从kv控制ScreenManager。默认情况下每个屏幕都有一个   属性管理器,为您提供所用ScreenManager的实例。

    Programming Guide » Kv language

      

    尽管self.ids方法非常简洁,但它通常被认为是使用ObjectProperty的“最佳实践”。这样可以创建直接引用,提供更快的访问速度,并且更加明确。

    实施例

    returnStation.py

    from kivy.app import App
    from kivy.uix.screenmanager import ScreenManager, Screen
    from kivy.properties import ObjectProperty
    
    
    class ScreenManagement(ScreenManager):
        screen_one = ObjectProperty(None)
        screen_two = ObjectProperty(None)
    
    
    class ScreenOne(Screen):
        member_status = ObjectProperty(None)
    
        def backspace(self, textString):
            newTextString = textString[0:-1]
            self.display.text = newTextString
    
        def getPoints(self, phoneNumber):
            print(phoneNumber)
            self.manager.screen_two.member_status.text = phoneNumber   #THIS IS HOW I ATTEMPTED TO CHANGE THE LABEL
    
    
    class ScreenTwo(Screen):
        pass
    
    
    class ReturnStationLayoutApp(App):
    
        def build(self):
            return ScreenManagement()
    
    
    if __name__ == '__main__':
        ReturnStationLayoutApp().run()
    

    returnstationlayout.kv

    #:kivy 1.10.0
    
    <ScreenManagement>:
        screen_one: screen_one
        screen_two: screen_two
    
        ScreenOne:
            id: screen_one
            name: 'menu'
        ScreenTwo:
            id: screen_two
            name: 'settings'
    
    <CustButton@Button>:
        font_size: 32
    
    <ScreenOne>:
        display: entry
        # PhoneGridLayout
        GridLayout:
            id: numberPad
            rows: 5
            padding: [300,200]
            spacing: 10
    
            # Where input is displayed
            BoxLayout:
                Label:
                    text: "+65"
                    font_size: 50
                    size_hint: 0.2, 1
                TextInput:
                    id: entry
                    font_size: 50
                    multiline: False
                    padding: [20, ( self.height - self.line_height ) / 2]
    
    
            BoxLayout:
                spacing: 10
                CustButton:
                    text: "1"
                    on_press: entry.text += self.text
                CustButton:
                    text: "2"
                    on_press: entry.text += self.text
                CustButton:
                    text: "3"
                    on_press: entry.text += self.text
                CustButton:
                    text: "DEL"
                    on_press: root.backspace(entry.text)
    
            BoxLayout:
                spacing: 10
                CustButton:
                    text: "4"
                    on_press: entry.text += self.text
                CustButton:
                    text: "5"
                    on_press: entry.text += self.text
                CustButton:
                    text: "6"
                    on_press: entry.text += self.text
                CustButton:
                    text: "AC"
                    on_press: entry.text = ""
    
            BoxLayout:
                spacing: 10
                CustButton:
                    text: "7"
                    on_press: entry.text += self.text
                CustButton:
                    text: "8"
                    on_press: entry.text += self.text
                CustButton:
                    text: "9"
                    on_press: entry.text += self.text
                CustButton:
                    text: "Enter" #HERE IS THE ENTER BUTTON
                    on_press:
                        root.manager.transition.direction = 'left'
                        root.manager.transition.duration = 1
                        root.manager.current = 'settings'
                        root.getPoints(entry.text)
    
            BoxLayout:
                spacing: 10
                Label:
                    text: ""
                CustButton:
                    text: "0"
                    on_press: entry.text += self.text
                Label:
                    text: ""
                Label:
                    text: ""
    
    <ScreenTwo>:
        member_status: memberStatus
        BoxLayout:
            Label:
                id: memberStatus
                text: ''  #THIS IS THE LABEL I AM TRYING TO CHANGE
            Button:
                text: 'Back to menu'
                on_press:
                    root.manager.transition.direction = "right"
                    root.manager.current = 'menu'
    

    输出

    Img01 Img02