Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Kivy\\n如何将按钮绑定到boxlayout的底部_Python_Button_Kivy_Scrollview_Boxlayout - Fatal编程技术网

Python Kivy\\n如何将按钮绑定到boxlayout的底部

Python Kivy\\n如何将按钮绑定到boxlayout的底部,python,button,kivy,scrollview,boxlayout,Python,Button,Kivy,Scrollview,Boxlayout,考虑到滚动视图,有必要使按钮从下方显示,而不是从中间显示。我就是不能让他这么做 请记住,BoxLayouts会覆盖对定位的修改,因为它们会自动定位其子项。这使得他们相当自给自足,但工作起来有点固执 就像你说的,如果只有几个按钮,它会粘在中间。scrollview可以工作,但因为它是boxlayout的子窗口,所以只能拉伸boxlayout所说的窗口的下半部分。此外,由于按钮的BOX布局增加了儿童自上而下,这就是为什么你的按钮出现在中间。实际上,它是在窗口下半部分的顶部添加按钮 您已指定不需要bo

考虑到滚动视图,有必要使按钮从下方显示,而不是从中间显示。我就是不能让他这么做


请记住,BoxLayouts会覆盖对定位的修改,因为它们会自动定位其子项。这使得他们相当自给自足,但工作起来有点固执

就像你说的,如果只有几个按钮,它会粘在中间。scrollview可以工作,但因为它是boxlayout的子窗口,所以只能拉伸boxlayout所说的窗口的下半部分。此外,由于按钮的BOX布局增加了儿童自上而下,这就是为什么你的按钮出现在中间。实际上,它是在窗口下半部分的顶部添加按钮

您已指定不需要boxlayouts的boxlayouts。您可能希望根据scrollview中按钮的数量随意定位scrollview。这将需要一个if语句和一个整数

我的变化:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.uix.scrollview import ScrollView
from kivy.effects.scroll import ScrollEffect
from kivy.uix.widget import Widget
from kivy.uix.button import Button


class BSGameMain(BoxLayout):
    blmain = BoxLayout(orientation = 'vertical') # MainBoxLayout init

    scrlFBtns = ScrollView(effect_cls = 'ScrollEffect')

    blbtns = BoxLayout(
        orientation = 'vertical',
        size_hint_y = None
        ) # BoxLayout for buttons

    blbtns.bind(minimum_height = blbtns.setter('height'))
    scrlFBtns.add_widget(blbtns)

    for i in range (2):
        blbtns.add_widget(Button(
            text='asd',
            size_hint_y = None,
            height = 40
            ))

    lblmain = Label(text = 'asd')
    blmain.add_widget(lblmain)
    blmain.add_widget(scrlFBtns)


class BSApp(App):
    def build(self):
        game = BSGameMain()
        return game.blmain

if __name__ == "__main__":
    BSApp().run()
从kivy.app导入应用
从kivy.uix.boxlayout导入boxlayout
从kivy.uix.label导入标签
从kivy.core.window导入窗口
从kivy.uix.scrollview导入scrollview
从kivy.effects.scroll导入ScrollEffect
从kivy.uix.widget导入widget
从kivy.uix.button导入按钮
类BSGameMain():#不再是boxlayout。
滚动高度=0;
blmain=Widget()#不再是boxlayout。充满了窗口。
blbtns=BoxLayout(
方向=‘垂直’,
大小提示=(1,无),
)#按钮的方框布局
blbtns.bind(最小高度=blbtns.setter('height'))
对于范围(20)内的i:#打印的按钮数
添加小部件(按钮(
text='asd',
大小提示y=无,
高度=40
))
如果(滚动高度<(Window.height/2)):
scroll_height=scroll_height+40#这度量了scroll视图需要多少空间。确保它与按钮高度匹配!
scrlFBtns=ScrollView(效果(cls='ScrollEffect',位置=(0,0),大小=(窗口宽度,滚动高度))
#位置0,0确保scrlFBtns保持在底部。如果您希望它延伸到屏幕外,请将其放置在负坐标处。
scrlFBtns.add_小部件(blbtns)
lblmain=Label(text='asd',halign='center',y=(Window.height*0.75),width=Window.width,color=(1,1,1))
blmain.add_小部件(lblmain)
blmain.add_小部件(scrlFBtns)
BSApp类(应用程序):
def生成(自):
game=BSGameMain()
返回game.blmain
如果名称=“\uuuuu main\uuuuuuuu”:
BSApp().run()
结果:

一键

,

三个按钮

,

二十个按钮

希望这是沿着你正在寻找的路线

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.uix.scrollview import ScrollView
from kivy.effects.scroll import ScrollEffect
from kivy.uix.widget import Widget
from kivy.uix.button import Button

class BSGameMain(): #No longer a boxlayout.
    scroll_height = 0;
    blmain = Widget() #No longer a boxlayout. Fills the window.

    blbtns = BoxLayout(
        orientation = 'vertical',
        size_hint = (1, None),
        ) # BoxLayout for buttons

    blbtns.bind(minimum_height = blbtns.setter('height'))

    for i in range (20):     #number of buttons printed
        blbtns.add_widget(Button(
            text='asd',
            size_hint_y = None,
            height = 40
            ))
        if (scroll_height < (Window.height / 2)):
            scroll_height = scroll_height + 40      #This measures how much space we need for the scrollview. Make sure it matches button height!

    scrlFBtns = ScrollView(effect_cls = 'ScrollEffect', pos = (0, 0), size = (Window.width, scroll_height)) 
    #The pos of 0,0 ensures scrlFBtns stays at the bottom. Place at negative coordinates if you want it to extend off-screen.
    scrlFBtns.add_widget(blbtns)
    lblmain = Label(text = 'asd', halign = 'center', y = (Window.height * 0.75), width = Window.width, color = (1,1,1,1))

    blmain.add_widget(lblmain)
    blmain.add_widget(scrlFBtns)

class BSApp(App):
    def build(self):
        game = BSGameMain()
        return game.blmain

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