[英]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:需要注意的几点:
Layout
to contain the ScreenManager
.您不需要Layout
来包含ScreenManager
。ScreenManager
based on the height of its current Screen
.您需要根据当前Screen
的高度来设置ScreenManager
的高度。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
变化时的大小。ScreenManager
height depends on the Screen
height, every Screen
must have a well defined height.由于ScreenManager
的高度取决于Screen
的高度,所以每个Screen
都必须有一个明确定义的高度。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.