首页 > 编程问答 >为什么将小部件添加到滚动视图在 python kivy 中不起作用

为什么将小部件添加到滚动视图在 python kivy 中不起作用

时间:2024-07-22 15:24:17浏览次数:10  
标签:python kivy kivymd

Python文件

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.core.window import Window
from kivy.config import Config
from kivymd.uix.list import OneLineListItem


#Ukuran window
Config.set('graphics', 'width', '1000')
Config.set('graphics', 'height', '600')
Config.set('graphics', 'resizable', False)

#Set window preview
Window.size = (1000, 600)

class MainApp(MDApp):
    def build(self):
        return Builder.load_file("interface.kv")
    
    def test(self):
        for i in range(20):
            item = OneLineListItem(
                text=f'Items number {i}'
            )
            self.root.ids.items.add_widget(item)


MainApp().run() ```




kivy file

#:kivy 2.3.0

FloatLayout: #--------------------------------标题-------------------- 图像: 来源:'ex.png' 大小提示:(0.3,0.3) pos_hint: {'x':-.05,'y': .75}

MDLabel:
    text: "Invoice System"
    font_size: '50sp'
    halign: 'center'
    pos_hint: {'y': .4}

#----------------项目------------ ---- MD文本字段: 模式:“填充” 提示文本:“???” 尺寸提示:(.4,.1) pos_hint: {'x': .05, 'y': .65}

MDTextField:
    mode: 'fill'
    hint_text: "???"
    size_hint: (.14, .1)
    pos_hint: {'x': .46, 'y': .65}

MDTextField:
    mode: 'fill'
    hint_text: "???"
    size_hint: (.14, .1)
    pos_hint: {'x': .61, 'y': .65}

MDFillRoundFlatIconButton:
    style: 'filled'
    text: "Add"
    size_hint: (.1, .1)
    pos_hint: {'x': .79, 'y': .65}

#------------ 项目列表 -------------- ----- MDBox布局: md_bg_color: '#B5C0D0' 大小提示:(.846,.37) pos_hint: {'x':.05, 'y': .25} 填充:10 方向:'垂直'

    MDScrollView:
        MDList:
            id: items

#------------ 最后一个按钮 -------------------- MDFFillRoundFlatIconButton: 风格: '填充' 文本:“开具发票” 尺寸提示:(.21,.15) pos_hint: {'x': .35, 'y': .08}


The problem is with the kivy file in the "Item List" section


I tried to create a scroll view inside a container which is a box layout. Inside the box layout, there is a scroll view that contains a list. The list is generated from a "for" loop which produces text to be displayed in the scroll view. However, the scroll view does not display the text generated from the loop.

问题出在 Kivy 代码中,没有将 MDList 对象的高度绑定到其父对象的 ScrollView 对象上。因此, ScrollView 不知道 MDList 的内容何时超过其边界,因此不会启用滚动。

以下是解决此问题的步骤:

  1. 导入 BoxLayout size_hint_y : 需要导入 BoxLayout 用于设置 MDList 的布局,并使用 size_hint_y 属性来控制其高度。
  2. MDList 包裹在 BoxLayout : 这将允许控制 MDList 的高度,并使其根据其内容动态调整大小。
  3. 绑定 MDList 的高度 : 需要将 MDList 的高度绑定到其内容的高度。这将确保当 MDList 中添加更多项目时, ScrollView 会相应地更新其滚动区域。

以下是修改后的 .kv 文件代码:

#:kivy 2.3.0

<pre><code>
FloatLayout:
    #--------------------------------标题--------------------
    Image:
        source: 'ex.png'
        size_hint: (0.3, 0.3)
        pos_hint: {'x': -.05, 'y': .75}

    MDLabel:
        text: "Invoice System"
        font_size: '50sp'
        halign: 'center'
        pos_hint: {'y': .4}

    #----------------项目--------------------
    MDTextField:
        mode: 'fill'
        hint_text: "???"
        size_hint: (.4, .1)
        pos_hint: {'x': .05, 'y': .65}

    MDTextField:
        mode: 'fill'
        hint_text: "???"
        size_hint: (.14, .1)
        pos_hint: {'x': .46, 'y': .65}

    MDTextField:
        mode: 'fill'
        hint_text: "???"
        size_hint: (.14, .1)
        pos_hint: {'x': .61, 'y': .65}

    MDFillRoundFlatIconButton:
        style: 'filled'
        text: "Add"
        size_hint: (.1, .1)
        pos_hint: {'x': .79, 'y': .65}

    #------------ 项目列表 ----------------------
    MDBoxLayout:
        md_bg_color: '#B5C0D0'
        size_hint: (.846, .37)
        pos_hint: {'x': .05, 'y': .25}
        padding: 10
        orientation: 'vertical'

        MDScrollView:
            BoxLayout:
                id: items_container
                orientation: 'vertical'
                size_hint_y: None
                height: self.minimum_height

                MDList:
                    id: items
                    # 不需要设置 size_hint_y 或 height

    #------------ 最后一个按钮 --------------------
    MDFillRoundFlatIconButton:
        style: 'filled'
        text: "开具发票"
        size_hint: (.21, .15)
        pos_hint: {'x': .35, 'y': .08}
</code></pre>

通过这些更改,的 MDList 现在应该可以正确滚动以显示所有添加的项目。

标签:python,kivy,kivymd
From: 78777367

相关文章

  • Python 实现Excel和TXT文本格式之间的相互转换
    Excel是一种具有强大的数据处理和图表制作功能的电子表格文件,而TXT则是一种简单通用、易于编辑的纯文本文件。将Excel转换为TXT可以帮助我们将复杂的数据表格以文本的形式保存,方便其他程序读取和处理。而将TXT转换为Excel则可以将文本文件中的数据导入到Excel中进行进一步的分析和......
  • Python (Django) 数据操作
    “如何将Excel考勤数据转换为特定的数据库插入格式?”**我Excel中的数据如下所示:**这是数据格式I**需要将其转换为适合数据库插入的格式,如下所示:**我想要的数据将Excel考勤数据转换为特定数据库插入格式的Python......
  • 在 python 中表示矩阵等价类的好方法是什么?
    我正在尝试编写一个程序来对井字棋进行强化学习。我希望引擎认识到,如果您反射棋盘或旋转它,您会得到完全相同的游戏,因此这些棋盘应该被视为彼此相同。目前我有一本字典,代表我当前对每个棋盘的估计估值游戏中的棋盘,每次游戏结束时,该游戏期间发生的所有棋盘位置的估值都会根据它......
  • MIT自学---python---6.100A_lecture2
    MIT自学---python---6.100A_lecture2前言一、设置python编译器地址二、将运行python文件的命令简化三、终端尝试执行简单python命令四、今日学到的python命令个人总结前言  这两天去听讲座,没什么时间按照计划自学MIT,今天赶紧补上。今天主要任务是搭建vscodepython......
  • python pip 需要构建工具,而它已经安装
    我看到这个问题已经被发布了很多次,人们设法解决了这个问题,但我没有!!操作系统版本:Windows1021H1Build19043.1288Python版本:Python3.9.7(tags/v3.9.7:1016ef3,Aug302021,20:19:38)[MSCv.192964bit(AMD64)]onwin32Pip、wheel和setuptool都可以日期:......
  • 无法在浏览器中访问Python 127.0.0.1:8000上的本地主机
    fromdjango.contribimportadminfromdjango.urlsimportpath,includeurlpatterns=[path('admin/',admin.site.urls),path('products/'),include('products.urls')#thisline]嗨,任何人。很抱歉问这样的问题,但这是我第一次尝试python。......
  • 在 VSCode 中通过 Python 使用 YouTube API 时如何启用 Intellisense
    我想在使用GoogleYouTubeAPI和Python时在VSCode中获得IntelliSense。但我不知道详细步骤。fromgoogleapiclient.discoveryimportbuildapi_key="****"youtube=build("youtube","v3",developerKey=api_key)request=youtube.channels().list(part......
  • 当 python 脚本通过 jenkins + Github 在 Windows 本地计算机上运行时,chrome 浏览器不
    我的Python代码是(windowsMachine)fromseleniumimportwebdriverprint("newLine")print("2Line")print("3Line")holdChrome=webdriver.ChromeOptions()holdChrome.add_experimental_option("detach",True)#Restricta......
  • python_基础_数据类型
    基础数据类型不需要声明,只有被赋值后才会创建变量。变量本身没有类型,“类型”指的是所存值的类型。类型判断type(x)和isinstance(x,int)前者不会认为子类是一种他的父类类型后者会认为子类是父类类型>>>classA:...pass...>>>classB(A):...pass......
  • IPython 使用技巧
    IPython是一个强大的交互式Pythonshell,提供了许多方便的功能,使Python编程更加高效和愉快。本文将介绍一些IPython的实用技巧,帮助开发者充分利用其功能,提高编程效率。1.基本操作和快捷键1.1启动IPython可以通过在终端输入以下命令来启动IPython:ipython启动后,你......