使用不同的变量创建多个 kivy 自定义小部件

Creating a number of kivy custom widgets with different variables

我有一个功能,可以为列表中的每个项目添加一个自定义小部件。 标签(自定义小部件的子项)应将为其添加的项目显示为文本(watchlist_names 中的项目)。在当前状态下,添加的每个自定义小部件都显示相同的文本。很明显,所有这些都引用了相同的变量。我还没有找到将自定义小部件标签文本引用到列表项的方法。

表示添加的第一个小部件具有标签文本:'Secunet' 添加的第二个小部件具有标签文本:'Wirecard' 等等...

py文件

# the variables and lists
watchlist_stock_ticker = StringProperty()
watchlist_stock_name = StringProperty()
watchlist_numbers = [0, 1]
watchlist_tickers = ['YSN.DE', 'WDI.DE']

# list with the variables to be displayed
watchlist_names = ['Secunet', 'Wirecard']

# the function to add the custom widgets
def load_stock_watchlist(self, layout):
    layout.clear_widgets()
    for n in self.watchlist_numbers:
        self.watchlist_stock_ticker = self.watchlist_tickers[n]
        self.watchlist_stock_name = self.watchlist_names[n]
        layout.add_widget(StockWatchlist())

和部分自定义小部件 (FloatLayout),包括标签:

kv 文件

<StockWatchlist>
    size_hint: None, None
    height: app.root.height * .13
    width: app.root.width -10

    Button:
        pos: root.pos
        on_release:
            app.go_screen(4)
            app.load_popup2()
            app.update_current(watchlistticker.text, watchlistcompany.text)
    
    BoxLayout:
        orientation: "vertical"
        pos: root.pos
        size_hint: None, None
        height: app.root.height * .13
        width: app.root.width -10

    # this is the label that should have its text matching
    # with the list item it was added for
        Label:
            text: app.watchlist_stock_name

Python 脚本和 kv 文件需要以下增强功能。

py文件

  • class StockWatchList()
  • 中声明一个class属性,watchlist_stock_namewatchlist_stock_ticker
  • 启动 StockWatchList 对象时将股票名称和代码作为参数传递。

片段 - py 文件

from kivy.properties import StringProperty
...

class StockWatchList(FloatLayout):
    watchlist_stock_ticker = StringProperty('')    # initialize to empty string
    watchlist_stock_name = StringProperty('')    # initialize to empty string


class class-name(...):
    # the variables and lists
    self.watchlist_numbers = [0, 1]
    self.watchlist_tickers = ['YSN.DE', 'WDI.DE']

    # list with the variables to be displayed
    self.watchlist_names = ['Secunet', 'Wirecard']

    # the function to add the custom widgets
    def load_stock_watchlist(self, layout):
        layout.clear_widgets()
        for n in self.watchlist_numbers:
            layout.add_widget(StockWatchlist(watchlist_stock_ticker=self.watchlist_tickers[n], watchlist_stock_name=self.watchlist_names[n]))

kv 文件

  • app.watchlist_stock_name替换为root.watchlist_stock_name
  • app.watchlist_stock_ticker替换为root.watchlist_stock_ticker

片段 - kv 文件

<StockWatchlist>:
    size_hint: None, None
    height: app.root.height * .13
    width: app.root.width -10

    Button:
        pos: root.pos
        on_release:
            app.go_screen(4)
            app.load_popup2()
            app.update_current(watchlistticker.text, watchlistcompany.text)

    BoxLayout:
        orientation: "vertical"
        pos: root.pos
        size_hint: None, None
        height: app.root.height * .13
        width: app.root.width -10

        Label:
            text: root.watchlist_stock_name