简体   繁体   中英

Label not updating Kivy

So I have two files:

  1. returnStation.py
  2. returnStationLayout.kv

What i am trying to achieve: There are two screens. One is a number pad. Once you keyed in your number and hit enter, it brings you to the next screen. And I hope the other screen shows the number you just keyed in.

Issue i am facing: I tried to access the id of the label i am trying to change to show the number but it is not working :/ I am not getting any error in the terminal.

Could i be accessing the values in the wrong way? If so, please advise how best to go about it in a two screen. Appreciate any help!


This is the file - returnStation.py:

How i attempted to change the label is through 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()

this is the file - returnStationLayout.kv:

The label i am trying to change is all the way at the bottom of this file

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'

The smallest fix you can apply is to move the getPoints method to your ReturnStationLayoutApp class, and update the desired field from there, just like that:

class ReturnStationLayoutApp(App):

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

Naturally, this would require changing your numberPad.getPoints(entry.text) line in the .kv file to app.getPoints(entry.text) .

Use Kivy ObjectProperty and ScreenManager , you can use root.manager... in kv file and self.manager in Python script.

Please refer to the following recommendations and example for details.

  1. Define a class for the ScreenManager and declare ObjectProperty for ScreenOne and ScreenTwo.
  2. With a ScreenManager, replace app.root... with root.manager... in kv file.
  3. Implement the two methods from class PhoneGridLayout in class ScreenOne, and remove class PhoneGridLayout
  4. Declare an ObjectProperty for memberStatus in class ScreenTwo.
  5. Remove st = ScreenTwo() because don't want to instantiate another object of ScreenTwo which will be different from the one that is already instantiated in the kv file.
  6. Replace st.ids.memberStatus.text with self.manager.screen_two.member_status.text

Screen Manager » Basic Usage

Create both screens. Please note the root.manager.current: this is how you can control the ScreenManager from kv. Each screen has by default a property manager that gives you the instance of the ScreenManager used.

Programming Guide » Kv language

Although the self.ids method is very concise, it is generally regarded as 'best practice' to use the ObjectProperty. This creates a direct reference, provides faster access and is more explicit.

Example

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'

Output

IMG01 Img02

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM