【问题标题】:How to add scrolling text widget in Kivy?如何在 Kivy 中添加滚动文本小部件?
【发布时间】:2016-08-20 13:49:29
【问题描述】:

我是 Kivy 的新手。我想创建一个接受用户文本输入的应用程序,然后显示它。但是当用户输入很长时,我希望显示区域可以滚动。

我做过一些教程,可以分别做这两件事,但我很难把它们放在一起。

这是允许滚动文本的代码:

__version__ = '1.0.1'

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.scrollview import ScrollView
import warnings
import string
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.properties import StringProperty

Builder.load_string('''
<ScrolllabelLabel>:
    Label:
        text: root.text
        font_size: 50
        text_size: self.width, None
        size_hint_y: None
        height: self.texture_size[1]
''')

class ScrolllabelLabel(ScrollView):
    text = StringProperty('srgsdrgsdfh dsfg dvgf vgsdfv srfvsdfsdrfv sevrv sdrfv serv serv serv servsrd vsv srvsdrfvvv' * 10)

runTouchApp(ScrolllabelLabel())

下面是显示您输入内容的代码:

__version__ = '1.0.1'

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.scrollview import ScrollView
import warnings
import string
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.properties import StringProperty

class SomeApp(App):
    def build(self):
        grid = GridLayout(cols=1, size_hint_x=None, width="600dp")

        self.lbl0 = Label(text='Tap and type a word/phrase below') # create a label instance
        grid.add_widget(self.lbl0) # physically add the label onto the layout

        self.txt1 = TextInput(text='', multiline=False) # create a text input instance
        grid.add_widget(self.txt1) # physically add the text input onto the layout

        self.lbl1 = Label(text='Display') # create a label instance
        grid.add_widget(self.lbl1) # physically add the label onto the layout

        btn1 = Button(text='Press') # create a button instance
        btn1.bind(on_press=self.mirror) # binding the button with the function below
        grid.add_widget(btn1) 

        return grid

    def mirror(self, userInput):
        self.lbl1.text = self.txt1.text

SomeApp().run()

但我无法将它们组合起来:

__version__ = '1.0.1'

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.scrollview import ScrollView
import warnings
import string
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.properties import StringProperty

Builder.load_string('''
<ScrolllabelLabel>:
    Label:
        text: root.text
        font_size: 50
        text_size: self.width, None
        size_hint_y: None
        height: self.texture_size[1]
''')

class ScrolllabelLabel(ScrollView):
    def __init__(self, **kwargs):
        self.txt0 = StringProperty()

class SomeApp(App):
    def build(self):
        grid = GridLayout(cols=1, size_hint_x=None, width="600dp")

        self.lbl0 = Label(text='Tap and type a word/phrase below') # create a label instance
        grid.add_widget(self.lbl0) # physically add the label onto the layout

        self.txt1 = TextInput(text='', multiline=False) # create a text input instance
        grid.add_widget(self.txt1) # physically add the text input onto the layout

        btn1 = Button(text='Press') # create a button instance
        btn1.bind(on_press=self.displayFunc) # binding the button with the function below
        grid.add_widget(btn1) 

        # Add scrolling text
        """self.lbl1 = Label(text='Display') # create a label instance
        grid.add_widget(self.lbl1) # physically add the label onto the layout"""
        scrollWidget = ScrolllabelLabel(text=self.lbl1.text)
        grid.add_widget(scrollWidget)

        return grid

    def displayFunc(self, userInput):
        self.lbl1.text = self.txt1

SomeApp().run()

我收到了这个错误:

 AttributeError: 'SomeApp' object has no attribute 'lbl1'

【问题讨论】:

  • self.lbl1.text = self.txt1 编辑为滚动小部件,这是您的属性错误并使用 self.txt1._text_,因为您需要文本属性,而不是小部件本身。
  • 如何执行您的“self.lbl1.text = self.txt1 编辑为滚动小部件”的建议?

标签: android python-2.7 scroll scrollview kivy


【解决方案1】:

您所做的是multiline=False,因此应用程序会以这种方式运行,无论您将TextInput 做得多大,它仍然是一行。使用 multiline=True 作为默认 TextInput,它会正确包装输入。

然后我看到你已经有一个可滚动标签的正文,所以只需使用默认的,使用该默认类并将ScrolllabelLabeltext 变量与您的输出一起提供给您的第一个文件。

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.scrollview import ScrollView
import warnings
import string
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.properties import StringProperty

Builder.load_string('''
<ScrolllabelLabel>:
    Label:
        text: root.text
        font_size: 50
        text_size: self.width, None
        size_hint_y: None
        height: self.texture_size[1]
''')

class ScrolllabelLabel(ScrollView):
    text = StringProperty('')
class SomeApp(App):
    def build(self):
        grid = GridLayout(cols=1, size_hint_x=None, width="600dp")

        self.lbl0 = Label(text='Tap and type a word/phrase below') # create a label instance
        grid.add_widget(self.lbl0) # physically add the label onto the layout

        self.txt1 = TextInput(text='', multiline=True) # create a text input instance
        grid.add_widget(self.txt1) # physically add the text input onto the layout

        self.lbl1 = ScrolllabelLabel(text='Display') # create a label instance
        grid.add_widget(self.lbl1) # physically add the label onto the layout

        btn1 = Button(text='Press') # create a button instance
        btn1.bind(on_press=self.mirror) # binding the button with the function below
        grid.add_widget(btn1) 

        return grid

    def mirror(self, userInput):
        self.lbl1.text = self.txt1.text

SomeApp().run()

【讨论】:

  • 我只正确使用了多行并添加了所需的输出管理器(?)。使用Crash course 快速学习kivy :)
  • 我想知道您使用什么一般准则来放置“自我”。在函数中的变量前面?我发现它适用于某些场景。例如,为什么你使用 self.lbl1 而不是 self.btn1?
  • 这就是您在整个班级中访问变量的方式,例如函数def func(self): 使您可以直接访问class Something: 下的变量,您甚至可以通过self.variable 创建这些类变量并在以后使用它们。简而言之,像你的类的全局变量。
猜你喜欢
  • 1970-01-01
  • 2016-01-04
  • 2013-05-24
  • 1970-01-01
  • 2020-02-22
  • 1970-01-01
  • 2020-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多