Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在Kivy中添加更多内容时,如何增加版面的大小?_Android_Python_Layout_Kivy_Boxlayout - Fatal编程技术网

Android 在Kivy中添加更多内容时,如何增加版面的大小?

Android 在Kivy中添加更多内容时,如何增加版面的大小?,android,python,layout,kivy,boxlayout,Android,Python,Layout,Kivy,Boxlayout,如何更改BoxLayout小部件的大小,以便在向其添加越来越多的内容时垂直滚动?您可以运行下面的脚本,但继续添加更多文本,然后单击“发送”以查看行为 如果可以的话,我还试图在文本出于某种原因发送后清除输入字段messageInput.text,widget.clear\u widgets()不起任何作用 from kivy.uix.button import Button from kivy.uix.label import Label from kivy.uix.textinput impor

如何更改BoxLayout小部件的大小,以便在向其添加越来越多的内容时垂直滚动?您可以运行下面的脚本,但继续添加更多文本,然后单击“发送”以查看行为

如果可以的话,我还试图在文本出于某种原因发送后清除输入字段
messageInput.text
,widget.clear\u widgets()不起任何作用

from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.core.window import Window
import datetime as dt
from kivy.uix.scrollview import ScrollView
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout

Window.clearcolor = (252, 235, 233, 0)

class NotificationMsgs(BoxLayout):

    orientation = 'vertical'
    spacing = 10
    padding = [50, 10, 50, 10]

    def __init__(self, **kwargs):
        super(NotificationMsgs, self).__init__(**kwargs)

        selectMsgsList = ['default dummy text']
        selectCreatedList = ['2017-08-10 00:00:00']

        notifBox = BoxLayout(orientation='vertical')
        # notifBox.bind(minimum_height=notifBox.setter('height'))
        notifScrlv = ScrollView(size_hint=(1,1), do_scroll_x=False, do_scroll_y=True)
        notifScrlv.add_widget(notifBox)

        r = 0
        for _ in zip(selectMsgsList, selectCreatedList):
            myMessage = Label(text="[color=000000]" + selectMsgsList[r] + "[/color]", markup=True)
            dateCreated = Label(text="[color=000000]" + selectCreatedList[r] + "[/color]", markup=True)
            notifBox.add_widget(myMessage)
            notifBox.add_widget(dateCreated)
            r += 1

        self.add_widget(notifScrlv)

        messageInput = TextInput(hint_text='type message...', multiline=True, size_hint_y=None, height=120, padding=30)
        self.add_widget(messageInput)


        def send_notification(self):
            createdDatetimeText = dt.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")

            for _ in zip(selectMsgsList, selectCreatedList):
                myMessage = Label(text="[color=000000]" + messageInput.text + "[/color]", markup=True)
                dateCreated = Label(text="[color=000000]" + createdDatetimeText + "[/color]", markup=True)

                # messageInput.clear_widgets()
                notifBox.canvas.ask_update()
                notifScrlv.canvas.ask_update()

                notifBox.add_widget(myMessage)
                notifBox.add_widget(dateCreated)


        self.add_widget(Button(text='send', font_size=40, size_hint_y=None, height=120, on_press=send_notification, background_color=[0,0,1,1], border=[0,1,1,1]))


class NotificationDemoApp(App):
    def build(self):
        return NotificationMsgs()

    def on_pause(self):
        return True

# if __name__ == '__main__':
NotificationDemoApp().run()

我已经解决了你的两个问题。您可能希望自己改进尺寸

  • 您不能使用BoxLayout,因为它总是采用其父窗口小部件的大小。我用它代替了网格布局。关于如何同时使用GridLayout和ScrollView,有很多描述。比如

  • 接下来,可以通过将TextInput的text属性设置为“”来清除文本ca

  • 这是代码。我做了各种各样的改变,如果你不能理解,请在评论中提问

    from kivy.uix.button import Button
    from kivy.uix.label import Label
    from kivy.uix.textinput import TextInput
    from kivy.core.window import Window
    import datetime as dt
    from kivy.uix.scrollview import ScrollView
    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.gridlayout import GridLayout
    
    
    Window.clearcolor = (252, 235, 233, 0)
    
    class NotificationMsgs(BoxLayout):
    
    
        orientation = 'vertical'
        spacing = 10
        padding = [50, 10, 50, 10]
    
        def __init__(self, **kwargs):
            super(NotificationMsgs, self).__init__(**kwargs)
    
            selectMsgsList = ['default dummy text']
            selectCreatedList = ['2017-08-10 00:00:00']
    
            notifBox = GridLayout(cols=1, size_hint_y=None)
            notifBox.bind(minimum_height = notifBox.setter('height'))
    
            notifScrlv = ScrollView()
            notifScrlv.add_widget(notifBox)
    
            r = 0
            for _ in zip(selectMsgsList, selectCreatedList):
                myMessage = Label(text="[color=000000]" + selectMsgsList[r] + "[/color]", markup=True)
                dateCreated = Label(text="[color=000000]" + selectCreatedList[r] + "[/color]", markup=True)
                notifBox.add_widget(myMessage)
                notifBox.add_widget(dateCreated)
                r += 1
    
            self.add_widget(notifScrlv)
    
            messageInput = TextInput(hint_text='type message...', multiline=True, size_hint_y=None, height=120, padding=30)
            self.add_widget(messageInput)
    
    
            def send_notification(self):
                createdDatetimeText = dt.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
    
                for _ in zip(selectMsgsList, selectCreatedList):
                    myMessage = Label(text="[color=000000]" + messageInput.text + "[/color]", markup=True, size_hint_y=None, size=self.texture_size)
                    dateCreated = Label(text="[color=000000]" + createdDatetimeText + "[/color]", markup=True)
    
                    # messageInput.clear_widgets()
                    notifBox.canvas.ask_update()
                    notifScrlv.canvas.ask_update()
    
                    notifBox.add_widget(myMessage)
                    notifBox.add_widget(dateCreated)
    
                messageInput.text = ""
    
            self.add_widget(Button(text='send', font_size=40, size_hint_y=None, height=120, on_press=send_notification, background_color=[0,0,1,1], border=[0,1,1,1]))
    
    
    class NotificationDemoApp(App):
        def build(self):
            return NotificationMsgs()
    
        def on_pause(self):
            return True
    
    # if __name__ == '__main__':
    NotificationDemoApp().run()