【发布时间】:2019-07-20 12:09:11
【问题描述】:
我想从 main 类访问一个 id 到 fahim2_pop 类。想要访问从文本输入(在主类中)到当有人按下搜索按钮时将出现的弹出小部件的单词。当有人搜索“hello”并按下搜索按钮时,弹出小部件将出现,并且在该弹出小部件中,标签的文本将与 textinput 中的“hello”相同。但标签和 id 仍然属于不同的类别。怎么办?
python 代码
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.properties import *
class fahim2_pop(Popup):
pass
class main(BoxLayout):
def word(self):
pop=fahim2_pop()
pop.open()
class CallApp(App):
def build(self):
return main()
CallApp().run()
kv码
Builder.load_string('''
<main>:
BoxLayout:
orientation:"vertical"
TextInput:
id:word
Button:
text:"search"
on_press:root.word()
<fahim2_pop>:
id:pop
title:"result"
BoxLayout:
Label:
text:app.root.ids.word.text
''')
【问题讨论】:
-
有几种方法可以从另一个类访问
id,但具体如何完成取决于您的应用程序的结构(您尚未共享的代码)。这就是为什么我们要求您发布minimal reproducible example。 -
我已经添加了我的完整代码。现在请给出解决方案