kivy filechooserlistview没有填满屏幕

时间:2014-08-12 04:39:24

标签: python kivy

我在Kivy制作一个需要使用FileChooserListView小部件的简单应用。出于某种原因,每当我尝试使用小部件时,它只覆盖应用程序的左下角。我不能让它像它应该的那样填满整个屏幕。 以下是我的应用程序发生的情况:

kivy app result

main.py:

import kivy
kivy.require('1.8.0')

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty

class AppRoot(BoxLayout):
    img_picker = ObjectProperty(None)

    def __init__(self, *args, **kwargs):
        super(BoxLayout, self).__init__(*args, **kwargs)
        self.img_picker = ImagePicker()

    def change_to_img_picker(self):
        self.clear_widgets()
        self.add_widget(self.img_picker)

    def change_to_about_us(self):
        print "change_to_about_us"

class ImagePicker(BoxLayout):
    def __init__(self, *args, **kwargs):
        super(BoxLayout, self).__init__(*args, **kwargs)

    def open(self, path, selection):
        print "OPEN"

    def selected(self, selection):
        print "SELECTION"

class ImageApp(App):
    def build(self):
        return AppRoot()

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

image.kv:

AppRoot:

<AppRoot>:
    orientation: 'vertical'
    padding: root.width * .02, root.height * .02
    spacing: 10
    Label:
        text: "Some text should go here"
    Button:
        text: "Select Image"
        size_hint_y: None
        height: root.height * .1
        on_release: root.change_to_img_picker()
    Button:
        text: "About the App"
        size_hint_y: None
        height: root.height * .1
        on_release: root.change_to_about_us()

<ImagePicker>:
    orientation: 'vertical'
    id: img_picker
    Button:
        text: "open"
        on_release: root.open(filechooser.path, filechooser.selection)
        size_hint: 1, .2
    FileChooserListView:
        id: filechooser
        on_selection: root.selected(filechooser.selection)
        size_hint: 1, .8

change_to_img_picker()函数应该使用FileChooserListView替换当前窗口小部件并占用所有空间。我不确定为什么它不起作用。如果有人能告诉我为什么会这样,我将不胜感激。提前谢谢你:)

1 个答案:

答案 0 :(得分:0)

super(BoxLayout, self).__init__(*args, **kwargs)

问题是你在BoxLayout上调用super,而不是你的ImagePicker类(第一个参数应该是当前类,而不是父类)。这会混淆通常在BoxLayout __init__中创建的绑定(可能这不再被称为,你得到了BoxLayout的父代__init__),所以没有正常的布局触发器,大小的变化等不会影响孩子。

我不完全确定某些细节(特别是,我对您的根小部件的布局似乎工作正常感到惊讶),但这似乎是问题的核心。

相关问题