簡體   English   中英

Kivy ScrollView 無法滾動

[英]Kivy ScrollView is not able to scroll

這是.py文件:

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.uix.stacklayout import StackLayout


class Stack(StackLayout):
    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        for i in range(0,100):
            b1 = Button(text=str(i+1),size_hint=(.1,.1))
            self.add_widget(b1)

class ScrollView(ScrollView):
    pass

class GameApp(App):
    def build(self):
        return Stack()
    
GameApp().run()

這是.kV 文件:

<ScrollView>:
        Stack:
                size_hint:1,None
                height:4000

在 output 中,我得到了按鈕,但無法滾動。

您好,如果您是新手,請查看此鏈接以獲取初學者指南。

ScrollView 僅當您在其上放置 1 個小部件並對當然的大小進行特定調整時才有效。

而不是這一行

return stack()

您必須返回 scrollview 小部件並在頂部添加 stack() 布局。 更好的是你可以像這樣修改你的代碼

對於.py

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.uix.stacklayout import StackLayout
from kivy.uix.screenmanager import Screen

class MainScreen(Screen):
    pass

class Stack(StackLayout):
    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        for i in range(0,100):
            b1 = Button(text=str(i+1),size_hint=(.1,.1))
            self.add_widget(b1)

class GameApp(App):
    def build(self):
        return MainScreen()
    
GameApp().run()

.kv

<MainScreen>:
    ScrollView:
        do_scroll_y: True
        do_scroll_y: False
        Stack:
                size_hint:1,None
                height:4000

我沒有測試代碼,但這就是代碼的邏輯。 rest 你可以在這里查看

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM