我想每次单击def send_message
时,都会在滚动视图的右侧添加一个新按钮,该代码不会引发任何错误,但是不会在Scrollview上显示任何内容。这是下面的代码。如果可以的话,我将不胜感激。
class ScrollableLabel(ScrollView):
def __init__(self, **kwargs):
super(ScrollableLabel, self).__init__(**kwargs)
self.chathistory = BoxLayout(orientation = "vertical", spacing = 5, padding = [10, 10])
self.add_widget(self.chathistory)
class Chat(Screen):
def __init__(self, **kwargs):
super(Chat, self).__init__(**kwargs)
self.localId = None
self.cols = 1
self.rows = 2
self.history = ScrollableLabel(size_hint_y = None)
self.add_widget(self.history)
self.messagebutton = Button(text="Send", font_size = 20, font_name= "fonts/Qanelas-Heavy.otf", background_color= (0.082, 0.549, 0.984, 1.0), background_normal= '', pos_hint= {"right": 1,"down": 1}, size_hint= [0.2, 0.1])
self.messagebutton.bind(on_release = self.send_message)
self.add_widget(self.messagebutton)
self.messagetextinput = TextInput(width=Window.size[0]*0.8, hint_text= "Write a message", font_name= "fonts/Qanelas-Light.otf", size_hint= [0.8, 0.1], pos_hint= {"left": 1,"down": 1})
self.add_widget(self.messagetextinput)
Window.bind(on_key_down=self.on_key_down)
Clock.schedule_once(self.focus_text_input, 1)
def on_key_down(self, instance, keyboard, keycode, text, modifiers):
if keycode == 40:
self.send_message(None)
def on_enter(self, *args):
app = App.get_running_app()
self.chatdestination = Label(text = firebase.get("/users/" + app.localId, 'first name') + " " + firebase.get('/users/' + app.localId, 'last name'),
pos_hint = {"center_x": 0.5, "center_y": 0.95}, size_hint = [0.8, 0.1], color = (0, 0, 0, 1), font_name = "fonts/Qanelas-Light.otf")
self.add_widget(self.chatdestination)
def send_message(self, _):
app = App.get_running_app()
message = self.messagetextinput.text
if message:
database.child("messages").child(app.localId).update({self.localId: message})
self.chatbubble = Button(text = self.messagetextinput.text, background_color= [0.925, 0.925, 0.925, 0.003],background_normal = '', pos_hint = {'right': 1}, size_hint= [0.2, 0.1])
self.history.chathistory.add_widget(self.chatbubble)
self.history.scroll_to(self.chatbubble)
self.messagetextinput.text = ""
Clock.schedule_once(self.focus_text_input, 0.1)
def focus_text_input(self, _):
self.messagetextinput.focus = True
首先,在ScrollView内部的小部件应禁用size_hint参数之一(等于无)。如果要垂直滚动,则BoxLayout(及其子元素)的size_hint_y应该为None,因为如果不这样做,所有小部件都将适合ScrollView,并且没有任何滚动内容。第二,如果我们取消激活高度提示,BoxLayout可能会出错。因此最好告诉它为子窗口小部件设置最小的高度(实际上是子窗口小部件的高度)。
所以:
class ScrollableLabel(ScrollView):
def __init__(self, **kwargs):
super(ScrollableLabel, self).__init__(**kwargs)
# add size_hint_y = None
self.chathistory = BoxLayout(orientation = "vertical", spacing = 5, padding = [10, 10], size_hint_y = None)
# set height
self.chathistory.bind(minimum_height=self.chathistory.setter('height'))
self.add_widget(self.chathistory)
...
...
# set size_hint_y to None also
self.chatbubble = Button(text = self.messagetextinput.text, background_color= [0.925, 0.925, 0.925, 0.003],background_normal = '', pos_hint = {'right': 1}, size_hint= [0.2, None])