如何用kivy在第二屏中制作列表?

问题描述 投票:0回答:1

我试着在点击主屏幕上的按钮后在第二个屏幕上显示列表,但列表不会显示,但我没有错误,我的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:"Air\nDensity: "
        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:"efficiency\nfactor: "
        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()

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

python kivy kivy-language
1个回答
0
投票

你的键 data 必须是你的属性 viewclass. 如果您使用的是 Label 作为你 viewclass,那么你的数据中的键很可能是 text的属性,因为那是 Label 可以显示一个字符串。你可以创建你自己的 viewclass 以满足您的具体需求。我已经冒昧地创建了一个 viewclass 所谓 DataDisplay 来证明我的意思。

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)

在这一安排中,每个数据元素应包含两个键,一个是 "A",另一个是 "B"。time 和a power. 使用它,下面的代码将显示两组 timepowerRecycleView:

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}]
© www.soinside.com 2019 - 2024. All rights reserved.