Python Kivy - 在另一个屏幕显示动态结果

Python Kivy - display dynamic result in another screen

我想创建一个用作字典功能的 2 屏应用程序。我使用了 Kivy 和 PyDictionary。第一屏输入单词,第二屏显示意思。我努力做到这一点。

main.py

from kivy.app import App
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen
from kivymd.app import MDApp
from word import dictword, screen_helper
from PyDictionary import PyDictionary
from bs4 import BeautifulSoup as bs
from kivymd.uix.label import MDLabel

final_word = ''


class ScreenOne(Screen):
    pass


class ScreenTwo(Screen):
    pass


sm = Builder.load_string("""
ScreenManager:
    ScreenOne:
        name: "screen1"
        BoxLayout:
            orientation: "vertical"
            TextInput:
                id: word_name
                text: final_word.text
            Button:
                text: "Change Label on Screen 2"
                on_release:
                    final_word.text = show_data_meaning()
            Button:
                text: "Next Screen"
                on_release:
                    root.current = "screen2"
    ScreenTwo:
        name: "screen2"
        BoxLayout:
            orientation: "vertical"
            Label:
                text: "Default Text"
                id: final_word
            Button:
                text: "Prev Screen"
                on_release:
                    root.current = "screen1"
""")


class TestApp(App):

    def build(self):
        return sm

    def show_data_meaning(self, obj):
        dictionary = PyDictionary(self.final_word.text)
        meaning = dictionary.getMeanings()
        return meaning


if __name__ == '__main__':
    TestApp().run()

我不知道如何通过 运行 和 show_data_meaning 将值传递到第二个屏幕并显示输出。此代码 returns 一个 NameError

 NameError: name 'show_data_meaning' is not defined

如何修复我的代码?我是 Kivy 的初学者。谢谢。

show_data_meaning()方法是App的一个方法,所以你在kv中引用它应该是:

            on_release:
                final_word.text = app.show_data_meaning()

然后在该方法中,您可以像这样访问 final_word id

def show_data_meaning(self):
    dictionary = PyDictionary(self.root.ids.final_word.text)
    meaning = dictionary.getMeanings()
    return meaning

ApprootScreenManager,其中包含 final_wordid

这是工作代码,感谢约翰!

from kivy.app import App
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.app import MDApp
from word import dictword, screen_helper
from PyDictionary import PyDictionary
from bs4 import BeautifulSoup as bs
from kivymd.uix.label import MDLabel

   

class WindowManager(ScreenManager):
    pass


class ScreenOne(Screen):
    # def press(self):
    #     self.ids.final_word.text
    pass


class ScreenTwo(Screen):
    # def display(self):
    #     self.ids.my_label.text = ScreenOne.ids.text_input.text
    pass


sm = Builder.load_string("""
ScreenManager:
    ScreenOne:
        name: "screen1"
        BoxLayout:
            orientation: "vertical"
            TextInput:
                id: word_name
                text: final_word.text
            Button:
                text: "Change Label on Screen 2"
                on_release:
                    final_word.text = app.show_data_meaning()
            Button:
                text: "Next Screen"
                on_release:
                    root.current = "screen2"
    ScreenTwo:
        name: "screen2"
        BoxLayout:
            orientation: "vertical"
            Label:
                text: "Default"
                id: final_word
            Button:
                text: "Prev Screen"
                on_release:
                    root.current = "screen1"
""")


class TestApp(App):

    def build(self):
        return sm

    def show_data_meaning(self):
        dictionary = PyDictionary(self.root.ids.word_name.text)
        meaning = dictionary.getMeanings()
        return str(meaning)


if __name__ == '__main__':
    TestApp().run()