【问题标题】:Kivy: Increasing space needed in BoxLayout?Kivy:增加 BoxLayout 所需的空间?
【发布时间】:2017-11-21 17:33:38
【问题描述】:

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

当前布局:

我的目标:

我的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'

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案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"
    

    输出

    【讨论】:

    • 我认为他还必须在第一个boxlayout中设置每个小部件的size_hint_y
    • 感谢大家的回答。作为旁注,是否可以将标签(例如新闻部分)制作为可滚动标签而不是其他标签?从我可以在网上找到的内容看来,您必须在我拥有 GridLayout 的地方继承它,所以不知道该怎么做。
    • 是的,可以将标签制作为可滚动的。我的建议是使用 RecycleView。请查看 kivy.org/docs/api-kivy.uix.recycleview.html 的 Kivy RecycleView 文档
    猜你喜欢
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多