我正在创建一个程序,该程序使我可以使用wolframalpha和Wikipedia搜索引擎以及其他所需的功能。我的问题是我似乎无法将我的答案变量从问题屏幕传递到答案屏幕。
该网站上已经有关于此的帖子,但是我已经对其进行了广泛的研究,但是似乎都没有一个解决我的问题的方法,它只是产生了一个不同的错误。如果有人可以向我解释什么地方错了,将不胜感激
我尝试了很多不同的事情,我不知道自己是否忽略了一个简单的错误。
Builder.load_string("""
<RootWidget>:
QIPA:
id:main
name: "mainprogramme"
QIPA_output:
id:output
name: "answer_screen"
<QIPA>:
BoxLayout:
orientation: 'vertical'
BoxLayout:
orientation: 'vertical'
size: root.size
spacing: 20
padding: 20
Label:
text: "Hello, I am QIPA, your virtual assistant. How may I be of service?"
font_size: "25sp"
TextInput:
id:question
multiline: False
BoxLayout:
Button:
text: "back"
font_size: "25sp"
<QIPA_output>:
BoxLayout:
orientation: 'vertical'
spacing: 20
padding: 20
Label:
id: output
text: root.answer
font_size: "10sp"
Button:
text: "back"
size_hint: 1,.3
font_size: "25sp"
on_press:root.BacktoMainProgramme()
""")
class QIPA(Screen):
def __init__(self, **kwargs):
super(QIPA, self).__init__(**kwargs)
Window.bind(on_key_down=self._on_keyboard_down)
def _on_keyboard_down(self, instance, keyboard, keycode, text, modifiers):
if keycode == 40: # 40 - Enter key pressed
self.OnEnter()
def OnEnter(self):
input = self.ids.question.text
input = input.lower()
try:
#wolframalpha
app_id = "G6YEVU-LJTJ5ETLHV"
client = wolframalpha.Client(app_id)
res = client.query(input)
answer = (next(res.results).text)
self.manager.current = "answer_screen"
print answer
except:
#wikipedia
wikipedia.set_lang("en")
answer = wikipedia.summary(input, sentences=3)
self.manager.current = "answer_screen"
global answer
class QIPA_output(Screen):
answer = StringProperty()
answer = QIPA.OnEnter.answer
def BacktoMainProgramme(self):
self.manager.current = "mainprogramme"
这是我的代码的简化版本,我收到的错误要么是
AttributeError: 'QIPA_output' object has no attribute 'answer' or <bound method QIPA_output.answer of <Screen name=">> or Traceback (most recent call last)
取决于我对代码的尝试
答案 0 :(得分:0)
为了访问类属性,请从pre()
的{{1}}中声明的answer
,使用在kv文件中添加的class QIPA_output()
。
class QIPA()
替换为ids
answer
self.manager.ids.output.answer
global answer