[英]Scroll View and clearing widget in kivy
I'm new to Kivy and don't know everything yet.我是 Kivy 的新手,还不是很了解。 I've been struggling for days now to finish this little app that takes an xlsx file and uses it as a search base.几天来我一直在努力完成这个采用 xlsx 文件并将其用作搜索基础的小应用程序。
But my problem is that I don't know how to clear the results widget after pressing "Search" again and I am also trying to add scrolling for the search results.但我的问题是,我不知道如何在再次按下“搜索”后清除结果小部件,我也在尝试为搜索结果添加滚动。 I simplified the program, but the principle remains the same.我简化了程序,但原理是一样的。 If you can tell me what I did wrong or how I should Google it.如果你能告诉我我做错了什么或者我应该如何谷歌它。
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.app import runTouchApp
class MyGrid(GridLayout):
def __init__(self, **kwargs):
super(MyGrid, self).__init__(**kwargs)
self.cols = 1
self.inside = GridLayout()
self.inside.cols = 2
self.inside.add_widget(Label(text="Label Name: "))
self.label_name = TextInput(multiline=False)
self.inside.add_widget(self.label_name)
self.add_widget(self.inside)
self.search = Button(text="Search", font_size=30)
self.search.bind(on_press=self.pressed)
self.add_widget(self.search)
self.print = GridLayout()
self.print.cols = 1
self.add_widget(self.print)
#Here a problem:
def pressed(self, result):
self.print.clear_widgets()
# file = load_xlsx(self.label_name.text)
for info in range(10):
btn = Button(text=str(info), size_hint_y=None, height=40)
self.print.add_widget(btn)
class MyApp(App):
def build(self):
return MyGrid()
if __name__ == "__main__":
MyApp().run()
The example below allows you to clear widgets when you click the search button again, it is best to implement similar logic using RecycleView
, because it dynamically loads content, which will allow you to display an almost unlimited number of widgets on the screen.下面的示例允许您在再次单击搜索按钮时清除小部件,最好使用RecycleView
实现类似的逻辑,因为它会动态加载内容,这将允许您在屏幕上显示几乎无限数量的小部件。 When you enter a number in TextInput
, the program will add widgets to the screen if they contain a digit passed in the input field.当您在TextInput
中输入数字时,如果小部件包含在输入字段中传递的数字,则程序会将小部件添加到屏幕。 Tip, use .kv
, it's convenient, and there is also syntax highlighting, use dp for dimensions - this is an abstract unit of measurement based on the physical density of the screen, allows you to keep the size of widgets the same on all screens and platforms.提示,使用.kv
,它很方便,还有语法高亮,使用dp表示尺寸 - 这是一个基于屏幕物理密度的抽象度量单位,允许您在所有屏幕上保持小部件的大小相同和平台。
from kivy.app import App
from kivy.lang.builder import Builder
from kivy.metrics import dp
KV = """
Screen:
BoxLayout:
orientation: 'vertical'
BoxLayout:
size_hint_y: None
height: self.minimum_height
Label:
text: "Label Name: "
TextInput:
id: search_text
multiline: False
size_hint_y: None
height: dp(50)
Button:
text: "Search"
font_size: sp(30)
pos_hint: {'center_x': 0.5}
size_hint_y: None
height: dp(50)
on_release: app.pressed(search_text.text)
RecycleView:
id: rv_result
key_size: "height"
bar_width: dp(4)
always_overscroll: False
key_viewclass: "viewclass"
RecycleGridLayout:
id: result
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
padding: dp(5)
spacing: dp(7)
cols: 1
default_size: None, dp(48)
"""
class MyApp(App):
def build(self):
return Builder.load_string(KV)
def pressed(self, result):
if self.root.ids.rv_result.data:
self.root.ids.rv_result.data = []
self.root.ids.rv_result.refresh_from_layout()
for info in range(100):
info = str(info)
if result in info:
item = {
'viewclass': 'Button',
'text': info,
'size_hint_y': None,
'height': dp(40),
}
self.root.ids.rv_result.data.append(item)
if __name__ == "__main__":
MyApp().run()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.