Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

python - Kivy 2.0.0 StringProperty not being recognized

I know this is a duplicate, but no answers for this have worked for me.

I am trying to add a widget which serves as an input line with a prompt.

I am getting a name error stating that 'prompt_text' in line 2 of client.kv is not defined. I believe this is an issue to do with the StringProperty() not being run.

Here is my python file:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.widget import Widget
import kivy.properties as kyProps


import ChatClient

CLIENT = None
VERSION = open("Version.txt").read()


class LoginWindow(Screen):
    namee = kyProps.ObjectProperty(None)
    password = kyProps.ObjectProperty(None)

    def loginBtn(self):
        global CLIENT
        CLIENT = ChatClient(self.namee.ti.text, self.password.ti.text, VERSION)
        if CLIENT.didConFail:
            errorMsg = f"Failed to connect. Reason: {CLIENT.connectFailedReason}"
            pop = Popup(title='Login Error', content=Label(text=errorMsg))
            pop.open()
        elif "REFUSED" in CLIENT.finalSetUpMsg:
            pop = Popup(title='Login Error', content=Label(text=CLIENT.finalSetUpMsg))
            pop.open()
        else:
            sm.current = "main"


class MainWindow(Screen):
    pass


class StyleEditorWindow(Screen):
    pass


class PMWindow(Screen):
    pass


class ChatWidget(Widget):
    pass


class InputLineWidget(Widget):
    prompt_text = kyProps.StringProperty("")
    ti = kyProps.ObjectProperty(None)


class WindowManager(ScreenManager):
    pass


kv = Builder.load_file("client.kv")

sm = WindowManager()

screens = [LoginWindow(name="login"), MainWindow(name="main"), StyleEditorWindow(name="styleEditor"), PMWindow(name="pm")]
for screen in screens:
    sm.add_widget(screen)
sm.current = "login"


class CpClientApp(App):
    def build(self):
        return sm


if __name__ == "__main__":
    CpClientApp().run()

And here is my client.kv file:

<InputLineWidget>:
    prompt_text: prompt_text
    ti: ti
    GridLayout:
        cols: 2
        size: root.width, root.height

        Label:
            text: prompt_text

        TextInput:
            id: ti
            multiline: false

<ChatWidget>:

<LoginWindow>:
    namee: namee
    password: password

    size: root.width, root.height

    GridLayout:
        cols: 1

        InputLineWidget:
            id: namee
            prompt_text: "Name: "

        InputLineWidget:
            id: password
            prompt_text: "Password: "

        Button:
            text: "Log In"
            on_release:
                root.manager.transition.direction = "up"
                root.loginBtn()

<MainWindow>:

<StyleEditorWindow>:

<PMWindow>:

I've tried many solutions based on others encountering similar issues but none have worked. Im running python 3.7 on Windows 10 using Kivy 2.0.0 Any help would be greatly appreciated. Thank you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The line:

prompt_text: prompt_text

creates an ObjectProperty named prompt_text that contains a reference to an object that has an id of prompt_text. But since that id is not defined in the <InputLineWidget> rule of your kv file, you get the noted error.

If you want the prompt_text to be used as the text for the label, change the kv to:

<InputLineWidget>:
    ti: ti
    GridLayout:
        cols: 2
        size: root.width, root.height

        Label:
            text: root.prompt_text

        TextInput:
            id: ti
            multiline: false

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...