【发布时间】:2016-05-27 19:17:27
【问题描述】:
我有一个ScrollView 和一个Bubble,它们部分重叠并包含一个GridLayout
问题:
- 如何隐藏小部件而不移除它?
我已经阅读了关于该主题的 question 的答案,建议要么结合 disabled 和 opacity 属性,这是我最终使用的,要么暂时将小部件移出屏幕.使用第一种方法隐藏Bubble,我发现即使它被禁用,它也会阻止它后面的视图滚动,即使文档声明了这个属性
指示此小部件是否可以与输入交互
所以我认为它不应该阻止滚动。有趣的是,当它没有被隐藏时(disabled=False),滚动直接穿过它,这更令人困惑
我之前也有 Bubble 包含一个 ScrollView,而它又持有 GridLayout。以下问题不再是问题,但仍然是一个有趣的行为:
- 为什么
Bubble向上滚动传递,但向下滚动传递不传递?
要理解我的意思,运行代码,将鼠标悬停在Bubble 上,然后尝试使用鼠标滚轮向不同方向滚动。那是考虑到ScrollView 中的GridLayout 不包含任何内容,即使它不影响行为
以下是这两个问题的代码以及一些获取所需行为的说明:
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.screenmanager import Screen
from kivy.uix.widget import Widget
from kivy.uix.textinput import TextInput
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
from kivy.uix.bubble import Bubble
from kivy.properties import ListProperty
Builder.load_string('''
<SmileBubble>:
size_hint: None, None
pos: 220, 90
size: 175, 250
#ScrollView:
#GridLayout:
#rows: 8 # To see the second question's example, uncomment this section
# and comment out the one below
GridLayout:
rows: 8
<MessageView>:
canvas:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
<Message>:
BoxLayout:
pos: root.pos
height: self.height
TextInput:
pos: root.pos
size: root.size
id: msg
''')
class Message(Widget):
bg_color = ListProperty([0.99, 0.99, 0.99, 1])
class SmileBubble(Bubble):
def hide(self):
self.disabled = True
def show(self):
self.disabled = False
class MessageView(ScrollView):
pass
class TestApp(App):
def msg_in(self, text):
msg = Message()
msg.ids['msg'].text = text
msg.size_hint = [None, None]
msg.width = 160
self.msg_layout.add_widget(msg)
def build(self):
self.scr = Screen()
self.sv1_main = MessageView()
self.msg_layout = GridLayout(cols = 1,
size_hint_y = None)
self.msg_layout.bind(minimum_height = self.msg_layout.setter('height'))
self.smile_bbl = SmileBubble()
for i in range(10):
self.msg_in("test")
self.smile_bbl.hide() # To hide/show the Bubble, comment out this line. For the second question, comment out this line
self.scr.add_widget(self.sv1_main)
self.sv1_main.add_widget(self.msg_layout)
self.scr.add_widget(self.smile_bbl)
return self.scr
TestApp().run()
如果重要的话,我正在使用 Kivy v1.9.2-dev0
【问题讨论】:
标签: python python-3.x kivy