【发布时间】:2021-08-17 03:04:12
【问题描述】:
我想创建一个充当字典功能的 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 将值传递到第二个屏幕并显示输出。此代码返回 NameError
NameError: name 'show_data_meaning' is not defined
如何修复我的代码?我是 Kivy 的初学者。谢谢。
【问题讨论】: