簡體   English   中英

Kivy標簽多行文字

[英]Kivy label multiline text

我想制作一個輸入了一些字母的程序,它可以實時給出所有單詞的組合,但是我只能顯示一些,因為我不知道如何使用多行。很久。 誰能幫我嗎?

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
import itertools



class Screen(BoxLayout):  

    def __init__(self, **kwargs ):
        super(Screen, self).__init__(**kwargs)
        self.orientation = "vertical"
        cuvinte = " "
        boxlayout2 = BoxLayout()
        button = Button()
        txt_instructions = Label(text = "Introduce your letters without any spaces between them")
        self.add_widget(txt_instructions)
        my_user_input = TextInput()
        boxlayout2.add_widget(my_user_input)
        self.add_widget(boxlayout2)   
        my_output = Label(halign = 'center')        
        self.add_widget(my_output)
        def callback(instance, value):
            cuvinte = " "
            lista2 = []
            lista3 = []
            n = value
            lista = list(n)
            for i in range(len(lista)):
                for word in itertools.permutations(lista):
                    lista2.append(''.join(word[0:len(word)-i]))

            for i in lista2:
                if i not in lista3:
                    lista3.append(i)
            lista3.sort()
            cuvinte = ' '.join(str(e) for e in lista3)
            my_output.text = cuvinte

        my_user_input.bind(text=callback)




class MyApp(App):

    def build(self):
        return Screen()


if __name__ == '__main__':
    MyApp().run()

在您的TextInput小部件中添加multiline = True

my_user_input = TextInput(multiline=True)

要使用自動包裝標簽的文本並在需要時以多行顯示的方式,您必須做幾件事。

1)根據官方的Kivy教程,您需要設置標簽的大小和位置,以便它可以跟隨其父尺寸的變化。

2)創建一個根據標簽中顯示的文本大小更新大小的函數

3)將標簽與更新功能綁定

因此您的代碼可能如下所示:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
import itertools


class Screen(BoxLayout):

    def __init__(self, **kwargs ):
        super(Screen, self).__init__(**kwargs)
        self.orientation = "vertical"
        cuvinte = " "
        boxlayout2 = BoxLayout()
        button = Button()
        txt_instructions = Label(text = "Introduce your letters without any spaces between them")
        self.add_widget(txt_instructions)
        my_user_input = TextInput()
        boxlayout2.add_widget(my_user_input)
        self.add_widget(boxlayout2)

        """THE LABEL HAS ITS SIZE AND POSITION SET TO FOLLOW THE PARENT'S"""
        self.my_output = Label(text_size= (None,None),
                          pos_hint={'center_x': 0.5, 'center_y': .95},
                          size_hint_y=None,
                          size = self.size,
                          height = self.size[1],
                          halign="center",
                          valign = "middle",)

        self.add_widget(self.my_output)

        """BINDING THE LABEL TO THE FUNCTION THAT UPDATES THE SIZE"""
        self.my_output.bind(size=self.setting_function)

        def callback(instance, value):
            cuvinte = " "
            lista2 = []
            lista3 = []
            n = value
            lista = list(n)
            for i in range(len(lista)):
                for word in itertools.permutations(lista):
                    lista2.append(''.join(word[0:len(word)-i]))

            for i in lista2:
                if i not in lista3:
                    lista3.append(i)
            lista3.sort()
            cuvinte = ' '.join(str(e) for e in lista3)
            self.my_output.text = cuvinte

        my_user_input.bind(text=callback)

    def setting_function(self, *args):
        """FUNCTION TO UPDATE THE LABEL TO ADJUST ITSELF ACCORDING TO SCREEN SIZE CHANGES"""
        self.my_output.pos_hint = {'center_x': 0.5, 'center_y': .85}
        self.my_output.text_size=self.size


class MyApp(App):

    def build(self):
        return Screen()


if __name__ == '__main__':
    MyApp().run()

現在,您的文本將被換行並顯示為多行:*當文本大小更改時*當窗口大小更改時

PS當字母過多時,由於CPU / RAM不足,程序將凍結。 您應該考慮限制用戶可以輸入的字母數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM