Kivy:BoxLayout需要增加空间吗?

时间:2017-11-21 17:33:38

标签: python kivy kivy-language

我当前GUI的左上角有很多卡塞,我试图基本上展开并让我的图像正常大小(类似于下面显示的模拟)。我想我需要向下扩展Y方向以适应它(使顶部区域更大以适应它)?但是,当我将size_hint_y更改为> 1它将文本向上隔开,并在GUI外部切断。有任何想法吗?非常感谢!

当前布局: enter image description here

我的目标: enter image description here

我的kv档案:

#:kivy 1.10.0
<WeatherWidget>:
    cols: 1
    BoxLayout:
        size_hint_y: None
        BoxLayout:
            size_hint_x: 1
            size_hint_y: 1
            orientation: "vertical"
            Image: 
                source: 'Code32.png' 
                keep_ratio: False
                halign: 'center'
            Label:
                text: root.current_temperature()[:3] 
                bold: True
                font_size: 40
            Label:
                text: root.high_low_temp(0)
                font_size: 15
                color: [1,255,1,1]
            Label:
                text: root.get_location()
                font_size: 15
                color: [1,1,1,1]
                bold: True
            Label:
                text: root.sunrise()
                font_size: 10
                color: [1,1,1,1]
            Label:
                text: root.sunset()
                font_size: 10
                color: [1,1,1,1]
        BoxLayout:
            orientation: 'vertical'
            Image: 
                source: 'Code32.png'
            Label: 
                text: root.forecast_day(1)
            Label: 
                text: root.high_low_temp(1)
                font_size: 12
                color: [1,255,1,1]
        Label:
            text: root.forecast_day(2)
        Label:
            text: root.forecast_day(3)
        Label:
            text: root.forecast_day(4)
        Label:
            text: root.forecast_day(5)
        BoxLayout:
            size_hint_y: None
            BoxLayout:
                orientation: 'vertical'
                Button:
                    text: root.TimeHours + ':' + root.TimeMinutes
                    size_hint_x: 1
                    font_size: 40
                    size: self.size
                    bold: True
                    halign: 'center'
                Label:
                    text: root.current_date()
                    size_hint_x: 1
                    font_size: 15
                    bold: True
                    halign: 'center'

1 个答案:

答案 0 :(得分:1)

解决方案如下:

1。将窗口划分为2个部分

使用size_hint_y = 0.3将顶部设置为父级高度的30%(0.3)。使用size_hint_y = 0.7

的父节高度的底部为70%(0.7)

2。避免左上角堵塞

要展开左上角的小部件,请使用size_hint_y = 1

BoxLayout:
    orientation: "vertical"
    size_hint_y: 0.3
    BoxLayout:
        size_hint_y: 1
        BoxLayout:
            orientation: "vertical"
            Image:

dailyview.kv

#:kivy 1.10.0
<WeatherWidget>:
    cols: 1
    BoxLayout:
        orientation: "vertical"
        size_hint_y: 0.3
        BoxLayout:
            size_hint_y: 1
            orientation: "horizontal"
            BoxLayout:
                orientation: "vertical"
                Image:
                    source: 'Weather/partly_cloudy_night@2x.png'
                    keep_ratio: False
                    halign: 'center'
                Label:
                    text: root.current_temperature()[:3]
                    bold: True
                    font_size: 40
                Label:
                    text: root.low_high_temp(0)
                    font_size: 15
                    color: [1,255,1,1]
                Label:
                    text: root.get_location()
                    font_size: 15
                    color: [1,1,1,1]
                    bold: True
                Label:
                    text: root.sunrise()
                    font_size: 10
                    color: [1,1,1,1]
                Label:
                    text: root.sunset()
                    font_size: 10
                    color: [1,1,1,1]
            BoxLayout:
                orientation: 'vertical'
                Image:
                    source: 'Weather/partly_cloudy_day@2x.png'
                Label:
                    text: root.forecast_day(1)
                Label:
                    text: root.low_high_temp(1)
                    font_size: 12
                    color: [1,255,1,1]
            Label:
                text: root.forecast_day(2)
            Label:
                text: root.forecast_day(3)
            Label:
                text: root.forecast_day(4)
            Label:
                text: root.forecast_day(5)
            BoxLayout:
                size_hint_y: None
                BoxLayout:
                    orientation: 'vertical'
                    Button:
                        text: root.TimeHours + ':' + root.TimeMinutes
                        size_hint_x: 1
                        font_size: 40
                        size: self.size
                        bold: True
                        halign: 'center'
                    Label:
                        text: root.current_date()
                        size_hint_x: 1
                        font_size: 15
                        bold: True
                        halign: 'center'
        BoxLayout:
            size_hint_y: 0.8

    BoxLayout:
        size_hint_y: 0.7
        BoxLayout:
            Button:
                size_hint_y: 1
                text: "News Sections"
        BoxLayout:
            orientation: "vertical"
            Button:
                size_hint_y: 0.5
                text: "Stock Quotes"
            Button:
                size_hint_y: 0.5
                text: "Live Sports Scores"

输出

Figure 1 - Buttons for News, Stock Quotes, and Live Sports Scores Figure 2 - Using RecycleView