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

时间:2019-07-04 16:15:05

标签: python variables widget kivy kivy-language

我有一个功能,可以为列表中的每个项目添加自定义窗口小部件。 标签(自定义窗口小部件的子级)应显示添加的项目作为文本(watchlist_names中的项目)。在当前状态下,每个要添加的自定义窗口小部件都显示相同的文本。显而易见,所有这些都引用相同的变量。我尚未找到将自定义小部件标签文本引用到列表项的方法。

意味着添加的第一个小部件带有标签文本:“ Senet” 添加的第二个小部件具有标签文本:“ 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

1 个答案:

答案 0 :(得分:0)

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

py文件

  • watchlist_stock_name中声明一个类属性watchlist_stock_tickerclass StockWatchList()
  • 启动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
相关问题