添加kivy自定义小部件只会添加第一个布局

时间:2019-07-02 20:35:45

标签: python kivy

我正在尝试为列表中的每个项目添加自定义窗口小部件。 第一个可以正常工作,但是每次跟随第一个,仅显示第一个Layout。 (黄色正方形)

我已经尝试将整个“自定义小部件”添加到add_widget(Builder.load_string,导致相同的结果。

引用自定义窗口小部件的Layout类:

class StockPortfolio(FloatLayout):
    pass

添加小部件的功能:


    portfolio_list = [1, 2, 3]

    def add_stock_portfolio(self, layout):
        layout.clear_widgets()
        for i in self.portfolio_list:
            layout.add_widget(StockPortfolio())

自定义小部件kv的开头:

<StockPortfolio@FloatLayout>
    id: stockportfolio
    size_hint: None, None
    height: app.root.height * .18
    width: app.root.width -10
    canvas:
        Color:
            rgba: .98, .98, 0, .5
        Rectangle:
            size: self.size
            pos: self.pos
    Button:
        background_normal: ''
        background_down: ''
        background_color: [0, 0, 0, 0]
        pos: 5, 5
        on_release:
            app.go_screen(4)
            app.load_popup2()
            app.update_current(portfolioticker1.text, portfoliocompany.text)
    BoxLayout:
        orientation: "vertical"
        pos: 5, 5
        size_hint: None, None
        height: app.root.height * .18
        width: app.root.width -10
        canvas:
            Color:
                rgba: .98, .98, .98, 1
            Rectangle:
                size: self.size
                pos: self.pos

1 个答案:

答案 0 :(得分:0)

问题

  1. 在您的Kivy应用中,entity Artist { name String required, surname String required, email String required, username String required, password String required, profilePic Blob, active Boolean required, description String required } entity ArtistPortfolio { image Blob required } entity ArtistSkills { } entity Skill { description String required } entity Customer { name String required, surname String required, email String required, username String required, password String required, profilePic Blob } entity Booking { date ZonedDateTime required, location String required, } entity BookingType { typeDesc String required } entity Reviews { reviewDesc String required, reviewRating Integer required, } entity UserReview { userReviewID Integer required, } entity ArtistReview { } entity Rating { ratingAmount Integer, ratingDesc String } entity Location { description String required } entity ArtistLocation { } entity ArtistBookingConst { constraint String required } relationship OneToOne { Artist{ArtistBookingConst} to ArtistBookingConst } relationship OneToMany { Artist to ArtistLocation, Artist to ArtistPortfolio, Artist to ArtistSkills, Artist to ArtistReview, Artist to Booking, Reviews to ArtistReview, Reviews to UserReview, Customer to UserReview, Customer to Booking, BookingType to Booking, Rating to Reviews, Location to ArtistLocation Skill to ArtistSkills, } 有两种不同的类别。在kv文件中声明的一个是动态类,而在Python脚本中声明的另一个是规则类。动态类不需要Python脚本中的实现。
  2. StockPortfolio()个对象彼此堆叠。

解决方案

  1. 在kv文件中,通过删除StockPortfolio将Dynamic类更改为class规则。
  2. 提供按钮的大小
  3. 为布局中添加的每个股票投资组合提供新头寸。

摘要-kv文件

@FloatLayout