我在 TokenScreen 中有一个字段,并且在 ListScreen 中有一个 MDList。问题是,我找不到从 TokenScreen 中的字段获取值并将其传递给 ListScreen 类的方法,以便用小部件填充容器。我尝试使用init,但这是不可能的。
它应该从 MDField 获取文本值并将其传递给g = Github(TOKEN)
.py 文件:
class passVarClass():
def __init__(self):
self.tokenPassVar = ""
class MainApp(MDApp):
screen = Screen()
def build(self):
self.theme_cls.primary_palette = 'Blue'
screen = Builder.load_file('layout.kv')
return screen
def set_screen(self, screen):
self.root.current = screen
print("switching screen to tokenscreen")
class TokenScreen(Screen):
def on_pre_leave(self, *args):
objectPass = passVarClass()
objectPass.tokenPassVar = self.ids.tokenFieldID.text
print(objectPass.tokenPassVar + "test_tokenClass")
# pass
class ListScreen(Screen):
def on_pre_enter(self, *args):
objectPass = passVarClass()
g = Github(objectPass.tokenPassVar)
user = g.get_user()
repos = user.get_repos()
for x in repos:
if x.language is not None:
self.ids.container.add_widget(
TwoLineListItem(text=x.name, secondary_text=x.language)
)
pass
class RepoScreen(Screen):
pass
sm = ScreenManager()
sm.add_widget(TokenScreen(name='tokenscreen'))
sm.add_widget(ListScreen(name='listscreen'))
sm.add_widget(RepoScreen(name='reposcreen'))
if __name__ == '__main__':
MainApp().run()
.kv 文件:
ScreenManager:
id:scr_mngr
TokenScreen:
ListScreen:
RepoScreen:
<TokenScreen>:
id:tokenscreenID
name:'tokenscreen'
MDToolbar:
id:toolbarID
title:"TokenScreen"
md_bg_color: app.theme_cls.primary_color
elevation : 10
left_action_items : [["arrow-left",lambda x: x]]
pos_hint:{"top":1}
MDTextFieldRound:
id:tokenFieldID
name:"tokenFieldName"
hint_text:"token"
mode:"rectangle"
pos_hint: {'center_x':0.5,'center_y':0.6}
size_hint_x:None
width:250
MDRectangleFlatButton:
text:"test"
pos_hint:{'center_x':0.5,'center_y':0.5}
on_press: root.manager.current = 'listscreen'
on_press:print("Switching to listscreen")
<ListScreen>:
id:listScreenID
name:'listscreen'
BoxLayout:
orientation: 'vertical'
MDToolbar:
id:toolbarID
title:"ListScreen"
md_bg_color: app.theme_cls.primary_color
elevation : 10
left_action_items : [["arrow-left",lambda x: app.set_screen("tokenscreen")]]
pos_hint:{"top":1}
ScrollView:
y: -toolbarID.height
MDList:
id:container
<RepoScreen>:
id:repoScreenID
name:'reposcreen'