python - Python KivyMD - 从一个屏幕输入,在另一个屏幕显示输出
问题描述
我想制作一个 2 页的 Kivy 应用程序 - 输入一个单词,在另一个屏幕上输出定义。我试过Kivy,它奏效了。现在我使用 KivyMD。我遇到了名称错误。
Traceback (most recent call last):
File "C:/Users/Andrew/PycharmProjects/JesscarlettApp/dictapp.py", line 94, in <module>
TestApp().run()
File "C:\Users\Andrew\PycharmProjects\JesscarlettApp\lib\site-packages\kivy\app.py", line 950, in run
runTouchApp()
File "C:\Users\Andrew\PycharmProjects\JesscarlettApp\lib\site-packages\kivy\base.py", line 582, in runTouchApp
EventLoop.mainloop()
File "C:\Users\Andrew\PycharmProjects\JesscarlettApp\lib\site-packages\kivy\base.py", line 347, in mainloop
self.idle()
File "C:\Users\Andrew\PycharmProjects\JesscarlettApp\lib\site-packages\kivy\base.py", line 391, in idle
self.dispatch_input()
File "C:\Users\Andrew\PycharmProjects\JesscarlettApp\lib\site-packages\kivy\base.py", line 342, in dispatch_input
post_dispatch_input(*pop(0))
File "C:\Users\Andrew\PycharmProjects\JesscarlettApp\lib\site-packages\kivy\base.py", line 308, in post_dispatch_input
wid.dispatch('on_touch_up', me)
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\Andrew\PycharmProjects\JesscarlettApp\lib\site-packages\kivymd\uix\behaviors\ripplebehavior.py", line 245, in on_touch_up
return super().on_touch_up(touch)
File "C:\Users\Andrew\PycharmProjects\JesscarlettApp\lib\site-packages\kivymd\uix\button.py", line 969, in on_touch_up
return super().on_touch_up(touch)
File "C:\Users\Andrew\PycharmProjects\JesscarlettApp\lib\site-packages\kivy\uix\behaviors\button.py", line 179, in on_touch_up
self.dispatch('on_release')
File "kivy\_event.pyx", line 705, in kivy._event.EventDispatcher.dispatch
File "kivy\_event.pyx", line 1248, in kivy._event.EventObservers.dispatch
File "kivy\_event.pyx", line 1132, in kivy._event.EventObservers._dispatch
File "C:\Users\Andrew\PycharmProjects\JesscarlettApp\lib\site-packages\kivy\lang\builder.py", line 57, in custom_callback
exec(__kvlang__.co_value, idmap)
File "<string>", line 24, in <module>
NameError: name 'meaning' is not defined
这是我的 main.py 和 kv。(请忽略那些额外未使用的模块。)问题在于 kv 中的这一行“meaning.text = app.show_data_meaning()”。但我只是不知道如何解决它。非常感谢。
from kivy.app import App
from kivymd.uix.screen import Screen
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.textfield import MDTextField
from kivy.lang import Builder
from kivy.uix.label import Label
from kivymd.uix.button import MDRectangleFlatButton
from kivy.core.window import Window
from kivy.uix.image import Image, AsyncImage
from kivymd.app import MDApp
from PyDictionary import PyDictionary
from bs4 import BeautifulSoup as bs
from kivymd.uix.label import MDLabel
class WordScreen(Screen):
pass
class MeaningScreen(Screen):
pass
screen_helper = """
ScreenManager:
id: screen_manager
WordScreen:
MeaningScreen:
<WordScreen>
name: 'wordscreen'
MDTextField:
hint_text: "Word"
id: text_field
text:'default'
icon_right: "dictionary"
icon_right_color: app.theme_cls.primary_color
pos_hint: {'center_x':0.5, 'center_y':0.9}
size_hint_x:None
width:300
MDRectangleFlatButton:
text: 'search'
id: search_button
pos_hint: {'center_x': 0.5, 'center_y': 0.8}
on_release:
meaning.text = app.show_data_meaning()
root.manager.current = 'meaningscreen'
<MeaningScreen>
name: 'meaningscreen'
MDRectangleFlatButton:
text: 'back'
pos_hint: {'center_x': 0.5, 'center_y': 0.2}
on_release: root.manager.current = 'wordscreen'
MDLabel:
text: "Default"
id: meaning
halign: 'center'
theme_text_color: 'Secondary'
font_style: 'Body1'
pos_hint: {'center_x': 0.5, 'center_y': 0.6}
"""
class TestApp(MDApp):
def __init__(self, **kwargs):
self.title = "Dictionary App"
self.theme_cls.primary_palette = "Blue"
super().__init__(**kwargs)
def build(self):
sm = Builder.load_string(screen_helper)
return sm
def show_data_meaning(self):
searchword = self.root.get_screen("wordscreen").ids.text_field.text
print(searchword)
dictionary = PyDictionary(searchword)
meaning = dictionary.getMeanings()
print(meaning)
return meaning
if __name__ == '__main__':
TestApp().run()
解决方案
您可以在您的新方法中TestApp
设置下一个屏幕上的文本
例子:
def my_func(self):
self.root.get_screen("meaningscreen").ids.meaning.text = str(self.show_data_meaning())
并在按钮释放中调用您的方法
on_release:
app.my_func()
root.manager.current = 'meaningscreen'
您也可以在 kv 文件中执行此操作,然后您只需要获取meaningscreenmeaningscreen.ids.text_field.meaning.text = app.show_data_meaning()
这是您更改后的代码
from kivy.app import App
from kivymd.uix.screen import Screen
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.textfield import MDTextField
from kivy.lang import Builder
from kivy.uix.label import Label
from kivymd.uix.button import MDRectangleFlatButton
from kivy.core.window import Window
from kivy.uix.image import Image, AsyncImage
from kivymd.app import MDApp
from PyDictionary import PyDictionary
from bs4 import BeautifulSoup as bs
from kivymd.uix.label import MDLabel
class WordScreen(Screen):
pass
class MeaningScreen(Screen):
pass
screen_helper = """
ScreenManager:
id: screen_manager
WordScreen:
MeaningScreen:
<WordScreen>
name: 'wordscreen'
MDTextField:
hint_text: "Word"
id: text_field
text:'default'
icon_right: "dictionary"
icon_right_color: app.theme_cls.primary_color
pos_hint: {'center_x':0.5, 'center_y':0.9}
size_hint_x:None
width:300
MDRectangleFlatButton:
text: 'search'
id: search_button
pos_hint: {'center_x': 0.5, 'center_y': 0.8}
on_release:
app.my_func()
root.manager.current = 'meaningscreen'
<MeaningScreen>
name: 'meaningscreen'
MDRectangleFlatButton:
text: 'back'
pos_hint: {'center_x': 0.5, 'center_y': 0.2}
on_release: root.manager.current = 'wordscreen'
MDLabel:
text: "Default"
id: meaning
halign: 'center'
theme_text_color: 'Secondary'
font_style: 'Body1'
pos_hint: {'center_x': 0.5, 'center_y': 0.6}
"""
class TestApp(MDApp):
def __init__(self, **kwargs):
self.title = "Dictionary App"
self.theme_cls.primary_palette = "Blue"
super().__init__(**kwargs)
def build(self):
sm = Builder.load_string(screen_helper)
return sm
def show_data_meaning(self):
searchword = self.root.get_screen("wordscreen").ids.text_field.text
print(searchword)
dictionary = PyDictionary(searchword)
meaning = dictionary.getMeanings()
print(meaning)
return meaning
def my_func(self):
self.root.get_screen("meaningscreen").ids.meaning.text = str(self.show_data_meaning())
if __name__ == '__main__':
TestApp().run()
推荐阅读
- icalendar - iCalendar 重复规则“每月第二个星期一后的第二天”
- pdf - 如何在 ColdFusion 的封面页中添加批准的印章
- javascript - 如何使用 C# 发布“multipart/form-data”
- mfc - 将 DDE 注册的文件关联与 CDialog 应用程序一起使用?
- c - 为什么 sizeof(x)++ 会编译?
- java - 无论如何,您可以在 Apache POI 中删除表格行内的边框吗?
- list - Terraform:如何使用列表或地图在 azurerm 中配置多个存储容器?
- symfony - 必须安装 Zend-crypt 才能使用 zenstruck/form-bundle 中的 ajax_entity_controller
- excel - 用户表单在 excel 中仅显示 3 个命令按钮中的 1 个
- javascript - 在异步函数中同时进行 fetch(),并在使用 JavaScript 完成后返回