我在kivy上的当前功能需要显示来自远程网址的json值, 当我点击加载项目屏幕时,需要在那里加载这些值,但是显示Nothing。
这是屏幕控制功能:
import json
import urllib2
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.network.urlrequest import UrlRequest
class MyScreenManager(ScreenManager):
pass
class IntroScreen(Screen):
pass
class MainScreen(Screen):
pass
class MailScreen(Screen):
pass
class PrjcScreen(Screen):
def on_enter(self):
req = urllib2.Request('http://localhost:5000/api/projects')
response = urllib2.urlopen(req)
js = json.loads(response.read().decode('utf-8'))
for k in js:
layout = GridLayout(cols=2)
return layout.add_widget(Label(text=js[k]['title']))
class ChatScreen(Screen):
pass
class GsaMain(BoxLayout):
pass
class GsamApp(App):
def build(self):
return MyScreenManager()
GsamApp().run()
这是kv文件
<MyScreenManager>:
IntroScreen:
MainScreen:
PrjcScreen:
MailScreen:
ChatScreen:
<IntroScreen@Screen>:
name: 'introscreen'
canvas:
Color:
rgb: 0, 0.267, 0
Rectangle:
size: self.size
GridLayout:
rows: 1
size_hint: None, None
width: self.minimum_width # combined width of child widgets
height: self.minimum_height # combined height of child widgets
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
spacing: 10
TextInput:
id: login
text: "Login"
multiline: False
size_hint: None, None
height:50
width: 140
font_size: 20
ImgButton:
size_hint: None, None
size: 100, 63
source: 'login.png'
on_release: root.manager.current = 'main'
<ImgButton@ButtonBehavior+Image>:
size_hint: None, None
<MainScreen>:
name: 'main'
canvas:
Color:
rgb: 0, 0.267, 0
Rectangle:
size: self.size
GridLayout:
rows: 1
size_hint: None, None
width: self.minimum_width # combined width of child widgets
height: self.minimum_height # combined height of child widgets
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
spacing: 70
ProjButton:
size_hint: None, None
size: 100, 63
source: 'checkl.png'
on_release: root.manager.current = 'prjct'
MsgButton:
size_hint: None, None
size: 100, 63
source: 'msgbox.png'
on_release: root.manager.current = 'main'
CsCButton:
size_hint: None, None
size: 100, 63
source: 'community.png'
on_release: root.manager.current = 'main'
CrCButton:
size_hint: None, None
size: 100, 63
source: 'Checkmark.png'
on_release: root.manager.current = 'main'
ExitButton:
size_hint: None, None
size: 100, 63
source: 'exit.png'
on_release: root.manager.current = 'main'
<ProjButton@ButtonBehavior+Image>:
size_hint: None, None
<MsgButton@ButtonBehavior+Image>:
size_hint: None, None
<CrCButton@ButtonBehavior+Image>:
size_hint: None, None
<CsCButton@ButtonBehavior+Image>:
size_hint: None, None
<ExitButton@ButtonBehavior+Image>:
size_hint: None, None
<MailScreen>:
name: 'mailscreen'
BoxLayout:
orientation: "vertical"
BoxLayout:
Label:
text: 'GSAM Mail'
font_size: 35
BoxLayout:
Button:
text: 'Chat'
font_size: 25
on_release: app.root.current = 'chatscreen'
<ChatScreen>:
name: 'Discussions'
BoxLayout:
orientation: "vertical"
BoxLayout:
Label:
text: 'Chat & Discussions'
font_size: 35
BoxLayout:
Button:
text: 'Home'
font_size: 25
on_release: app.root.current = 'main'
<PrjcScreen>:
name: 'prjct'
BoxLayout:
orientation: "vertical"
BoxLayout:
size_hint: None, None
Label:
text: 'Community Projects'
font_size: 35
BoxLayout:
Button:
text: 'community Projects'
font_size: 25
on_release: app.root.current = 'main'
GridLayout:
id: 'mygrid'
cols: 2
rows: 4
padding: 5
spacing: 5
Label:
text: "Project Title"
TextInput:
id: ptitle
Label:
text: "Project Description"
TextInput:
id: pdescr
你能帮忙指出我做得对吗?
答案 0 :(得分:1)
您的on_enter方法返回布局。您实际上需要将布局添加到屏幕上(即它不像构建方法那样工作)。
但是,一旦您完成了这项工作,您就需要注意不要每次都添加新的布局。
您可能最好在KV文件中定义布局,然后在每次触发事件时使用on_enter方法清除并重新填充网格。