如何使用kivy在第二个屏幕中制作列表



单击主屏幕中的按钮后,我尝试在第二个屏幕中显示列表。 但列表不会显示,但我没有错误。 我的 kivy 代码有什么问题吗?

这是我的代码

def rumus_power_np(radius, air_density, efficiency_factor, kecepatan_angin):
return 3.14/2 * pow(radius, 2) * np.power(kecepatan_angin, 3) * air_density * efficiency_factor
class MainWindow(Screen):
def btn(self):
print("name: ",self.radius.text)
predicted_power = rumus_power_np(int(self.radius.text), float(self.air_density.text), float(self.efficiency_factor.text)/100, predictions)
data = {'time':  series.date[-109:].dt.time,'power':  predicted_power}
df = pd.DataFrame (data, columns = ['time','power'])
plt.plot(df.time,df.power, color='orange', label = 'predicted power')
plt.xlabel("time")
plt.ylabel("power (watt)")
plt.legend()
print(predicted_power)
self.manager.get_screen('second').ids.rv.data = [{'time': df.time}, {'power': df.power}]
class SecondWindow(Screen):
def btndu(self):
self.ids.destination.add_widget(FigureCanvasKivyAgg(plt.gcf()))
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("my.kv")
class MyMainApp(App):
def build(self):
return kv
if __name__=="__main__":
MyMainApp().run()

predicted_power数据存在于其上面的代码中,并且必须具有该数据的 109 行。

这是我的KV代码

#:kivy 1.9.1
WindowManager:
MainWindow:
SecondWindow:
<MainWindow>:
name: "main"
radius: radius
air_density: air_density
efficiency_factor: efficiency_factor
FloatLayout:
Label:
text:"Radius: "
size_hint: 0.5,0.12
pos_hint: {"x":0, "top":0.8}
font_size: (root.width**2 + root.height**2) / 14**4
TextInput:
id: radius
multinline:False
pos_hint: {"x":0.5, "top":0.78}
size_hint: 0.3, 0.06
font_size: (root.width**2 + root.height**2) / 14**4
Label:
text:"Meter"
size_hint: 0.5,0.12
pos_hint: {"x":0.62, "top":0.8}
font_size: (root.width**2 + root.height**2) / 16**4
Label:
text:"AirnDensity: "
size_hint: 0.5,0.12
pos_hint: {"x":0, "top":0.8-0.13}
font_size: (root.width**2 + root.height**2) / 14**4
TextInput:
id: air_density
multinline:False
pos_hint: {"x":0.5, "top":0.78-0.13}
size_hint: 0.3, 0.06
font_size: (root.width**2 + root.height**2) / 14**4
Label:
text:"kg/m³"
size_hint: 0.5,0.12
pos_hint: {"x":0.62, "top":0.8-0.13}
font_size: (root.width**2 + root.height**2) / 16**4
Label:
text:"efficiencynfactor: "
size_hint: 0.5,0.12
pos_hint: {"x":0, "top":0.8-0.13*2}
font_size: (root.width**2 + root.height**2) / 14**4
TextInput:
id: efficiency_factor
multinline:False
pos_hint: {"x":0.5, "top":0.78-0.13*2}
size_hint: 0.3, 0.06
font_size: (root.width**2 + root.height**2) / 14**4
Label:
text:"%"
size_hint: 0.5,0.12
pos_hint: {"x":0.62, "top":0.8-0.13*2}
font_size: (root.width**2 + root.height**2) / 16**4
Button:
text:"Submit"
pos_hint:{"x":0.3,"y":0.25}
size_hint: 0.4, 0.1
font_size: (root.width**2 + root.height**2) / 14**4
on_release:
root.btn()
app.root.current = "second"
root.manager.transition.direction = "left"
<SecondWindow>:
name: "second"
GridLayout:
cols:1
BoxLayout:
id: destination
RecycleView:
id: rv
viewclass: 'Label'
RecycleBoxLayout:
default_size: None, dp(56)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
Button:
text: "show plot"
on_release:
root.btndu()

如果有什么问题,请帮助我?因为这是我的第一次

data中的键必须是viewclass的属性。如果你使用Label作为你的viewclass,那么你数据中的键很可能是text的,因为这是可以显示字符串的Label的属性。您可以创建自己的viewclass以满足您的特定需求。我冒昧地创建了一个我称之为DataDisplayviewclass来证明我的意思:

class DataDisplay(BoxLayout):
time = NumericProperty(0.0)
power = NumericProperty(0.0)

并使用kv来规划DataDisplay的布局:

<DataDisplay>:
orientation: 'horizontal'
Label:
text: 'time: ' + str(root.time)
Label:
text: 'power: ' + str(root.power)

在这种安排中,每个数据元素应包含两个键,一个time和一个power。使用此功能,以下代码将在RecycleView中显示两组timepower

class MainWindow(Screen):
def btn(self):
self.manager.get_screen('second').ids.rv.data = [{'time': 2.3, 'power': 4.5}, {'time': 0.1, 'power':9.356}]

最新更新