简体   繁体   English

带有屏幕管理器和网格布局的 Kivy 滚动视图

[英]Kivy scrollview with screenmanager and gridlayout

Like many other questions, im having issues with scrollview.像许多其他问题一样,我对滚动视图有疑问。 I'm trying to have a bunch of different buttons, that goes to different screens, but the buttons sit on top of each other, are stuck in the corner or only one button is appearing and the screen is cut in half.我试图有一堆不同的按钮,去不同的屏幕,但按钮坐在彼此的顶部,卡在角落里,或者只有一个按钮出现,屏幕被切成两半。

I've tried having the screenmanager outside the TabbedPanelItem, giving every widget a size and no size_hint_y but to no avail?我试过在 TabbedPanelItem 之外使用屏幕管理器,给每个小部件一个大小,但没有 size_hint_y 但无济于事? is it not possible to have the screenmanager inside the tabbedpanel or is it bad practice是否不可能将屏幕管理器放在选项卡式面板中,还是不好的做法

Python file. Python 文件。

from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen 
from kivy.config import Config
from kivy.lang import Builder
Config.set('graphics', 'resizeable', 0)
Window.size = (375,700)

class MainScreen(Screen):
    pass
class FirstScreen(Screen):
    pass

class SecondScreen(Screen):
    pass
root = Builder.load_file("main.kv")
class myapp(App):
    def build(self):
        return root

if __name__ == '__main__':
    myapp().run()

main.kv主文件

TabbedPanel:
    do_default_tab: False

    TabbedPanelItem:
        text:"1"
        ScrollView:
            size: self.size
            FloatLayout: #gridlayout? #boxlayout?
                size_hint_y: None
                ScreenManager:
                    id: manager  

                    MainScreen:
                        name: 'main'

                        GridLayout:
                            spacing: 10
                            padding: 10
                            cols:1
                            row_default_height: 100
                            row_force_default: True
                            Button:
                                text: 'first'
                                on_press: manager.current = 'first'  
                                size_hint: 1, None
                                height: 100
                            Button:
                                text: 'Second'
                                on_press: manager.current = 'second'  
                                
                    FirstScreen:
                        name: 'first'
                    SecondScreen:
                        name: "second"
    TabbedPanelItem:
        text:"2"

    TabbedPanelItem:
        text:"3"
<FirstScreen>:
    BoxLayout:
        Label:
            text:"first"
        Button:
            text: 'Back to main'
            on_press: root.manager.current = 'main'

<SecondScreen>
    BoxLayout:
        Label:
            text:"second"
        Button:
            text: 'Back to main'
            on_press: root.manager.current = 'main'

You can do what you want, but it seems a bit complicated.你可以做你想做的,但它似乎有点复杂。 Here is a kv that I think will do it:这是我认为可以做到的kv

TabbedPanel:
    do_default_tab: False

    TabbedPanelItem:
        text:"1"
        ScrollView:
            ScreenManager:
                id: manager  
                size_hint_y: None
                height: self.current_screen.height  # only seems to work for first Screen
                on_current_screen: self.height = self.current_screen.height  # required because above line failure

                MainScreen:
                    name: 'main'
                    size_hint_y: None
                    height: grid.height  # set this Screen height to the height of the GridLayout

                    GridLayout:
                        id: grid
                        size_hint_y: None
                        height: self.minimum_height  # set this height to the minimum required
                        spacing: 10
                        padding: 10
                        cols:1
                        row_default_height: 100
                        row_force_default: True
                        Button:
                            text: 'first'
                            on_press: manager.current = 'first'  
                            size_hint: 1, None
                            height: 100
                        Button:
                            text: 'Second'
                            on_press: manager.current = 'second'  
                            
                FirstScreen:
                    name: 'first'
                SecondScreen:
                    name: "second"
    TabbedPanelItem:
        text:"2"

    TabbedPanelItem:
        text:"3"
<FirstScreen>:
    size_hint_y: None
    height: 100   # must set its height
    BoxLayout:
        Label:
            text:"first"
        Button:
            text: 'Back to main'
            on_press: root.manager.current = 'main'

<SecondScreen>
    size_hint_y: None
    height: 100   # must set its height
    BoxLayout:
        Label:
            text:"second"
        Button:
            text: 'Back to main'
            on_press: root.manager.current = 'main'

A few things to note:需要注意的几点:

  • You don't need a Layout to contain the ScreenManager .您不需要Layout来包含ScreenManager
  • You need to set the height of the ScreenManager based on the height of its current Screen .您需要根据当前Screen的高度来设置ScreenManager的高度。
  • The line height: self.current_screen.height should adjust the height of the ScreenManager every time the current Screen changes, but it doesn't. line height: self.current_screen.height应该在每次当前Screen改变时调整ScreenManager的高度,但它没有。 So I added on_current_screen: self.height = self.current_screen.height to adjust the size when the current Screen changes.所以我添加了on_current_screen: self.height = self.current_screen.height来调整当前Screen变化时的大小。
  • Since the ScreenManager height depends on the Screen height, every Screen must have a well defined height.由于ScreenManager的高度取决于Screen的高度,所以每个Screen都必须有一个明确定义的高度。
  • The MainScreen height is determined by the GridLayout height, and the GridLayout height is calculated using self.minimum_height . MainScreen高度由GridLayout高度确定, GridLayout高度使用self.minimum_height计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM