2016-04-26 216 views
1

我是基維新人。我想創建一個應用程序,它接受用戶文本輸入,然後顯示它。但是當用戶輸入很長時,我希望顯示區域可以滾動。如何在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() 
我得到了這個錯誤:
+0

'self.lbl1.text = self.txt1'編輯scrollwidget,這是你的屬性的錯誤並使用self.txt1._text_,因爲你想要的文字,而不是針對小部件本身。 – KeyWeeUsr

+0

如何執行「self.lbl1.text = self.txt1編輯到scrollwidget」的建議? – KubiK888

回答

1

你所做的是multiline=False因此,該應用程序會表現出這種方式,無論你製作的多麼大TextInput,它仍然是一條線。使用multiline=True作爲默認的TextInput,它會正確地包裝輸入。

然後我看到你已經有一個可滾動標籤的主體,所以只需使用默認的主體,使用該默認類,並將變量ScrolllabelLabel與第一個文件一起輸出。

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() 
+0

您的代碼有效。將研究它更接近。謝謝。 – KubiK888

+1

我只使用了多行,並添加了所需的輸出管理器(?)。使用[Crash course](http://inclem.net/pages/kivy-crash-course/)快速學習kivy :) – KeyWeeUsr

+0

我不知道你用什麼通用指南來放置「自我」。在一個函數內的變量前面?我發現它適用於和不適用於某些情況。例如,你爲什麼使用self.lbl1而不是self.btn1? – KubiK888