Python Kivy - 在另一个屏幕上显示动态结果
Posted
技术标签:
【中文标题】Python Kivy - 在另一个屏幕上显示动态结果【英文标题】:Python Kivy - display dynamic result in another screen 【发布时间】: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 的初学者。谢谢。
【问题讨论】:
【参考方案1】: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
App
的root
是ScreenManager
,其中包含id
的final_word
。
【讨论】:
谢谢约翰!对你的小费进行一点修改。在 show_data_meaning() 方法中,我必须引用 self.root.ids.word_name.text,因为那是输入词。然后一切正常!【参考方案2】:这是有效的代码,感谢 John!
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()
【讨论】:
以上是关于Python Kivy - 在另一个屏幕上显示动态结果的主要内容,如果未能解决你的问题,请参考以下文章
创建一个 kivy 应用程序以在一个屏幕上接收用户的输入并在另一个屏幕上显示这些图像
Python / Kivy - 在另一个调用函数的屏幕中替换标签的值
如何将纯 python 中动态创建的按钮添加到用 Kivy 语言编写的 kivy 布局中?
如果我在另一个屏幕中,我如何获取屏幕的 MDTextField(或数据“登录”)的文本。 (基维/基维MD/Python)