无法更改add_widget添加的窗口小部件的值

时间:2017-12-22 14:27:53

标签: python python-3.x python-2.7 kivy kivy-language

- 2017 12/23 14:00 **我编辑了可以理解更清晰** -

大家好。 我写了简单的代码,所以请检查一下。

我的目的是更改使用add_widget()添加的Number的值“text”和“cooooop”。

但不能改变。

我认为这是通过引用父窗口小部件的警察引起的。

但我不知道真相。

我该怎么办?

请帮助我!!

#python
from kivy.app             import App
from kivy.uix.behaviors   import ButtonBehavior
from kivy.uix.label       import Label
from kivy.uix.gridlayout  import GridLayout
from kivy.properties      import ObjectProperty

class Number(Label):
    pass

class Wrapper(GridLayout):
    pass

class Adder(ButtonBehavior, Label):
    rooting = ObjectProperty(None)
    def on_release(self):
        self.rooting.add_widget(Number(
            rooting = self.rooting,
            cooooop = self.rooting.cop,
            text    = self.rooting.cop,
        ))

class Changer(ButtonBehavior, Label):
    rooting = ObjectProperty(None)
    def on_release(self):
        self.rooting.cop = 'Thank you'

class TestApp(App):
    def build(self):
        pass

TestApp().run()

#kvfile

<Number>:
    font_size: sp(15)
    halign: 'center'
    markup: True
    valign: 'top'
    size_hint_y: None
    text_size: self.width, sp(50)
    height: sp(30)
    multiline: False

<Adder>:
    text: "Add"
    font_size: sp(15)
    halign: 'center'
    markup: True
    valign: 'middle'
    size_hint_y: None
    text_size: self.width, sp(50)
    height: sp(30)
    multiline: False
    col: 1.000 ,0.5843 ,0.000, 1
    on_release: self.col = 1.000 ,0.5843 ,0.000, 1
    on_press:   self.col = 1.000 ,0.5843 ,0.000, .5
    canvas.before:
        Color:
            rgba: self.col
        RoundedRectangle:
            pos: self.pos
            size: self.size
            radius: [sp(12)]

<Changer>:
    text: "Change"
    font_size: sp(15)
    halign: 'center'
    markup: True
    valign: 'middle'
    size_hint_y: None
    text_size: self.width, sp(50)
    height: sp(30)
    multiline: False
    col: 0.000  ,0.4784  ,1.000, 1
    on_release: self.col = 0.000  ,0.4784  ,1.000, 1
    on_press:   self.col = 0.000  ,0.4784  ,1.000, .5
    canvas.before:
        Color:
            rgba: self.col
        RoundedRectangle:
            pos: self.pos
            size: self.size
            radius: [sp(12)]

Wrapper:
    padding: 0, 300, 0, 0
    pos_hint: {'top': 1, 'center_x': .5}
    cols: 1
    size_hint: .3, None
    id: rooting
    height: self.minimum_height
    cop: 'Change me'
    spacing: 0, 50
    Adder:
        rooting: rooting
    Changer:
        rooting: rooting
    Label:
        size_hint_y: None
        height: sp(40)

0 个答案:

没有答案