Kivy弹出按钮的内容未显示在按钮中

时间:2018-12-10 18:24:34

标签: python kivy

以下代码未将第二个按钮内容放入其中,该内容位于第一个按钮位置,您能解释一下原因吗? 结果应该是弹出窗口中的两个按钮,每个按钮中都有自己的内容,不是吗? 我做错了什么?

from kivy.lang import Builder
from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.properties import ObjectProperty, ListProperty

Builder.load_string('''
<ListAvions>:
    box: box
    BoxLayout:
        id: box
        orientation: 'vertical'

<AvionToBuy>:
    boxlabel: boxlabel
    size_hint_y: None
    height: 300
    background_color: [1, 0, 1, .5]
    on_release: print(root.avion)
    BoxLayout:
        orientation: 'horizontal'
        Label:
            text: root.avion[0]
            size_hint: None, None
            size: 300, 300
        BoxLayout:
            orientation: 'vertical'
            id: boxlabel
            Label:
                text: root.avion[1]
                size_hint: None, None
            size: 420, 150
        Label:
            text: str(root.avion[2]) + "$"
            size_hint: None, None
            size: 420, 150
''')

AVIONS = [["P51.png", "P51", 750], ["F22.png", "F22", 1500]]

class ListAvions(Popup):
    box = ObjectProperty(None)

def add(self):
    for avion in range(len(AVIONS)):
        aviontobuybut = AvionToBuy()
        aviontobuybut.avion = AVIONS[avion]
        self.box.add_widget(aviontobuybut)

class AvionToBuy(Button):
    avion = ListProperty(AVIONS[0])

class AvionApp(App):
    def build(self):
    z = ListAvions()
    z.add()
    return z

if __name__=="__main__":
    AvionApp().run()

2 个答案:

答案 0 :(得分:0)

如果您只是将文本放入AvionToBuy小部件,只需使用text的{​​{1}}属性。您可以将Button文件中AvionToBuy的规则更改为:

kv

答案 1 :(得分:0)

这是对原始代码的修改,使AvionToBuy扩展了ButtonBehaviorBoxLayout。这样,您就可以将任何窗口小部件作为AvionToBuy的子代放置,并且这些子代将水平放置。我已包含您的原始Labels,并添加了Image。现在AvionToBuy的行为就像Button。我还添加了默认的Button主题。

from kivy.lang import Builder
from kivy.app import App
from kivy.uix.behaviors.button import ButtonBehavior
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.properties import ObjectProperty, ListProperty

Builder.load_string('''
<ListAvions>:
    box: box
    BoxLayout:
        id: box
        orientation: 'vertical'
<AvionToBuy>:
    orientation: 'horizontal'
    padding: (50, 0)
    border: [16, 16, 16, 16]
    on_release: print(root.avion)
    bg_normal: 'atlas://data/images/defaulttheme/button'
    bg_down: 'atlas://data/images/defaulttheme/button_pressed'
    state_image: self.bg_normal if self.state == 'normal' else self.bg_down
    canvas:
        Color:
            rgba: (1, 1, 1, 1)
        BorderImage:
            border: self.border
            pos: self.pos
            size: self.size
            source: self.state_image
    Label:
        text: root.avion[0]
    Label:
        text: root.avion[1]
    Label:
        text: str(root.avion[2]) + '$'
    Image:
        source: 'data/images/defaultshape.png'
''')

AVIONS = [["P51.png", "P51", 750], ["F22.png", "F22", 1500]]

class ListAvions(Popup):
    box = ObjectProperty(None)

    def add(self):
        for avion in range(len(AVIONS)):
            aviontobuybut = AvionToBuy()
            aviontobuybut.avion = AVIONS[avion]
            self.box.add_widget(aviontobuybut)

class AvionToBuy(ButtonBehavior, BoxLayout):
    avion = ListProperty(AVIONS[0])


class AvionApp(App):
    def build(self):
        z = ListAvions()
        z.add()
        return z

if __name__=="__main__":
    AvionApp().run()