我想制作一个输入一些字母的程序,它实时提供所有单词组合,但我只能显示几个,因为我无法弄清楚如何使用多行。我没有用过kivy很长一段时间有人可以帮帮我吗?
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()
答案 0 :(得分:2)
在TextInput小部件中添加multiline = True
my_user_input = TextInput(multiline=True)
答案 1 :(得分:0)
要使用自动BOTH包装其文本的标签并在需要时以多行显示,您必须做几件事。
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()
现在您的文字被包装并显示为多行: *当文字大小改变时 *当窗口大小改变时
P.S。当字母变得太多时,由于CPU / RAM不足,程序会冻结。您应该考虑限制用户可以输入的字母数量。