如何使用复选框实现 kivy ScrollView?
Posted
技术标签:
【中文标题】如何使用复选框实现 kivy ScrollView?【英文标题】:how to implement kivy ScrollView with checkboxes? 【发布时间】:2021-10-06 06:42:09 【问题描述】:我对编程完全陌生,很抱歉我的菜鸟问题。请告诉我如何在我的应用程序中实现 ScrollView?我不明白在这种情况下是否需要实现 runTouchApp?如果有,必须放在哪里?
我查看了其他滚动视图问题,但没有找到适合我的答案。我将不胜感激有关如何实现内部滚动的任何提示。
提前谢谢你。
'''
ScrollView:
do_scroll_x: False
do_scroll_y: True
Label:
size_hint_y: None
height: self.texture_size[1]
text_size: self.width
padding: 10, 10
text:
'really some amazing text\n' * 100
'''
from kivy.app import runTouchApp
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window
from kivy.uix.label import Label
from kivy.uix.checkbox import CheckBox
from kivy.properties import StringProperty
from kivy.uix.scrollview import ScrollView
Window.size = (240, 480)
Window.clearcolor = (180 / 255, 90 / 255, 3 / 255, 1)
Window.title = "Test"
count = None
class MenuScreen(Screen):
def __init__(self, **kw):
super(MenuScreen, self).__init__(**kw)
box = BoxLayout(orientation='vertical')
box.add_widget(Button(text='item list', background_color=(0, 1, 1, 1), on_press=lambda x:
sm.switch_to(Setup(name='setup'))))
self.add_widget(box)
class Setup(Screen):
def __init__(self, **kw):
super(Setup, self).__init__(**kw)
my_list = ['red', 'green', 'yellow', 'blue', 'white', 'magenta', 'violet']
self.scroll_list(my_list)
def scroll_list(self, x):
grid = GridLayout(cols=1, padding=10, spacing=3)
label_question = Label(text=f'select\nfrom list:', size_hint=(1, None), halign="left", valign="middle")
label_question.bind(size=label_question.setter('text_size'))
grid.add_widget(label_question)
grid_scroll = GridLayout(cols=2)
for i in x:
lbl = Label(text=f'i', size_hint=(1, None), halign="left", valign='middle')
lbl.bind(size=lbl.setter('text_size'))
grid_scroll.add_widget(lbl)
radio_btn = CustomCheckBox(text=str(i), size_hint=(0.2, None), group=1)
grid_scroll.add_widget(radio_btn)
grid_scroll.bind(minimum_height=grid_scroll.setter('height'))
root = ScrollView(size_hint=(1, 0.7))
root.add_widget(grid_scroll)
grid.add_widget(root)
grid.add_widget(
Button(text='submit', background_color=(0, 1, 1, 1), pos_hint=(None, 1), size_hint_y=None,
height=60,
on_press=lambda x: sm.switch_to(MenuScreen(name='menu'))))
self.add_widget(grid)
class CustomCheckBox(CheckBox):
text = StringProperty('')
def on_active(self, instance, value):
global count
if value:
count = instance.text
print(f'\tThe checkbox text=instance.text is active')
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
class Test(App):
def __init__(self, **kvargs):
super(Test, self).__init__(**kvargs)
def build(self):
return sm
if __name__ == '__main__':
Test().run()
【问题讨论】:
【参考方案1】:我认为您需要做的就是修改该行:
grid_scroll = GridLayout(cols=2)
到:
grid_scroll = GridLayout(cols=2, size_hint_y=None)
ScrollView
的子级必须至少将size_hints
之一设置为None
。
【讨论】:
非常感谢!!!有用!!!我以为我已经在kv部分指出了这一点,甚至没有怀疑我必须在那里写!你很棒!谢谢,谢谢,谢谢!以上是关于如何使用复选框实现 kivy ScrollView?的主要内容,如果未能解决你的问题,请参考以下文章
我正在使用 Kivy 开发一个 Android 应用程序,我想知道如何在 Scrollview 中添加 MDCard?目前我正在使用 kivyMD 和 Kivy
Kivy:如何从自定义 BoxLayout 显示 ScrollView?
如何在 Python/Kivy 中实现 ScrollView
Kivy - 如何在 ModalView 内向 ScrollView 添加多个按钮?