【问题标题】:Unable to size widgets properly无法正确调整小部件的大小
【发布时间】:2016-02-29 15:11:05
【问题描述】:

在我正在创建的应用程序中,我有一个包含几个屏幕实例的屏幕管理器。在其中一个中,我希望有 2 个标准小部件和一些由用户动态添加的按钮。如果这些按钮太多,用户应该能够滚动它们直到找到他想要的那个。

为了实现这一点,我使用了一个包含两个对象的网格布局:另一个网格布局和一个滚动视图。网格布局负责 2 个标准小部件(文本输入和按钮)。滚动视图负责动态添加的按钮。

我希望滚动视图部分占据窗口的较大部分(比如 75%),以便用户可以更清楚地看到按钮,并且带有 2 个标准小部件的网格布局应该占据剩余部分。然而,gridlayout 最终占据了窗口的大部分。

这是一段代码(假设现在动态添加的按钮是 15 个):

sm = ScreenManager()

class scr1(Screen):
    pass

#layout that occupies the entire window. Everything else will be added on this
glayout = GridLayout(cols=1)

#layout that should occupy 25% of the window
layout1 = GridLayout(cols=1,size_hint_y=0.25)

#layout to be added on the scrollview 
layout2 = GridLayout(cols=1,size_hint_y=None)
layout2.bind(minimum_height=layout2.setter('height'))

#screen to hold the glayout
screen1 = scr1(name = '1')

#adding a couple of widgets to layout1
layout1.add_widget(Button(text = 'hey'))
layout1.add_widget(TextInput(text = 'hey'))

#scroller that should occupy 75% of the window
scroller = ScrollView(size_hint = (1,None))
scroller.add_widget(layout2)

#adding buttons to be scrolled 
for i in range(15):
    layout2.add_widget(Button(text=str(i),size_hint_y=None))

#adding scroller and layout1 to glayout and then the glayout to the screen
glayout.add_widget(scroller)
glayout.add_widget(layout1)
screen1.add_widget(glayout)

#adding the screen to the screen manager
sm.add_widget(screen1)    

我对kivy使用的定位系统不是很熟悉,我不知道我应该如何解决这个问题。这是我运行程序时的样子。

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    您将 ScrollView 的 size_hint_y 设置为 None

    from kivy.uix.screenmanager import Screen, ScreenManager
    from kivy.uix.gridlayout import GridLayout
    from kivy.uix.scrollview import ScrollView
    from kivy.uix.button import Button
    from kivy.uix.textinput import TextInput
    sm = ScreenManager()
    
    class scr1(Screen):
        pass
    
    #layout that occupies the entire window. Everything else will be added on this
    glayout = GridLayout(cols=1)
    
    #layout that should occupy 25% of the window
    layout1 = GridLayout(cols=1,size_hint_y=0.25)
    
    #layout to be added on the scrollview 
    layout2 = GridLayout(cols=1,size_hint_y=None)
    layout2.bind(minimum_height=layout2.setter('height'))
    
    #screen to hold the glayout
    screen1 = scr1(name = '1')
    
    #adding a couple of widgets to layout1
    layout1.add_widget(Button(text = 'hey'))
    layout1.add_widget(TextInput(text = 'hey'))
    
    #scroller that should occupy 75% of the window
    scroller = ScrollView(size_hint_y=.75)
    scroller.add_widget(layout2)
    
    #adding buttons to be scrolled 
    for i in range(15):
        layout2.add_widget(Button(text=str(i),height=70,size_hint_y=None))
    
    #adding scroller and layout1 to glayout and then the glayout to the screen
    glayout.add_widget(scroller)
    glayout.add_widget(layout1)
    screen1.add_widget(glayout)
    
    #adding the screen to the screen manager
    sm.add_widget(screen1)
    from kivy.app import App
    
    class Ap(App):
        def build(self):
            return sm
    
    Ap().run()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-24
      • 1970-01-01
      • 2018-03-22
      • 2023-03-06
      相关资源
      最近更新 更多