繁体   English   中英

python kivy在for循环中生成链接到jsonstore项目的按钮

[英]python kivy generating buttons linked to jsonstore items in a for loop

我正在使用for循环为jsonstore中的每个键生成一个按钮。 所有按钮都正确添加到布局中,并且button.text是正确的,但是当按钮调用回调时,它们都链接到相同的键。 (最后一个键)

这是代码:

def show_saved_conditions(*args,**kwargs):

    self.label7 = Label(text = str(self.store[self.btns.text], size_hint = (.3,.3), pos_hint = {'x':.3,'y':.5}, color = (0,0,1,1)))
    self.layout.add_widget(self.label7)

def view_saved_conditions(*args, **kwargs):

    x = 0
    y = 0

    for i in self.store.keys():

        self.btns = (Button(text = i, size_hint = (.2,.1), pos_hint = {'x':x,'y':y}, on_release = show_saved_conditions))
        self.layout.add_widget(self.btns)

        x +=.2

        if x >= 1:
            y+=.1
            x = 0

可以肯定的是,之前曾有人问过这个问题,但我找不到足够具体的帖子与我联系。

先感谢您...

问题-始终参考最后一个按钮

show_saved_conditions()方法中,它始终使用self.btns.text ,这是添加到布局的最后一个按钮。

self.label7 = Label(text = str(self.store[self.btns.text], size_hint = (.3,.3), pos_hint = {'x':.3,'y':.5}, color = (0,0,1,1)))

在示例中,它演示了on_touch_down事件。

触摸事件基础

默认情况下,触摸事件将分派给所有当前显示的窗口小部件。 这意味着小部件将接收触摸事件,无论它是否在其物理区域内发生。

为了提供最大的灵活性,Kivy将事件分派给所有小部件,并让它们决定如何对它们作出反应。 如果您只想响应小部件内的触摸事件,只需检查:

 def move(self, touch): if self.collide_point(*touch.pos): # The touch has occurred inside the widgets area. Do stuff! pass 

main.py

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button


class CreateButton(Button):

    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            if touch.button == "right":
                print(self.id, "right mouse clicked")
            elif touch.button == "left":
                print(self.id, "left mouse clicked")
            else:
                print(self.id)
            return True
        return super(CreateButton, self).on_touch_down(touch)


class OnTouchDownDemo(GridLayout):

    def __init__(self, **kwargs):
        super(OnTouchDownDemo, self).__init__(**kwargs)
        self.build_board()

    def build_board(self, *args):
        # make 9 buttons in a grid
        for i in range(0, 9):
            button = CreateButton(id=str(i), text=str(i))
            self.add_widget(button)


class OnTouchDownApp(App):

    def build(self):
        return OnTouchDownDemo()


if __name__ == '__main__':
    OnTouchDownApp().run()

ontouchdown.kv

#:kivy 1.11.0

<CreateButton>:
    font_size: 50

<OnTouchDownDemo>:
    rows: 3
    cols: 3
    row_force_default: True
    row_default_height: 150
    col_force_default: True
    col_default_width: 150
    padding: [10]
    spacing: [10]

输出量

在此处输入图片说明

show_saved_conditions()方法的*args参数包含按下的Button实例。 因此该方法可以是:

def show_saved_conditions(self, btn_instance):

    self.label7 = Label(text = str(self.store[btn_instance.text], size_hint = (.3,.3), pos_hint = {'x':.3,'y':.5}, color = (0,0,1,1)))
    self.layout.add_widget(self.label7)

由于您在方法中使用self ,因此我假设这是一个实例方法,而正确的第一个arg是self 如果不是实例方法,则只需删除self arg,但是该方法在哪里获取self的值?

当然,此方法每次执行都会覆盖self.label7

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM