我想在app运行时添加文本到标签。它被放置在FloatLayout中,它放在ScrollView中,以便我可以滚动文本。我希望FloatLayout的大小默认为1,1,并在添加文本时增加其高度。我是否应该使用尺寸提示或常规坐标系是否足够?无论如何,我无法让它发挥作用。当我进行一些测试时,我发现标签的text_size
属性在添加新文本时不会改变,但是窗口的更改会调整大小。这可能是因为我将kv文件中的text_size
属性设置为等于标签的大小以获得正确的文本对齐方式(左上角)。这是我的代码:
from kivy import require
require("1.9.1")
import string
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.effects.scroll import ScrollEffect
from kivy.uix.screenmanager import Screen, ScreenManager, NoTransition
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.uix.floatlayout import FloatLayout
nick = ''
class MyScrollView(ScrollView):
pass
class TextLabel(Label):
pass
Builder.load_string('''
<MyScrollView>:
#...canvas instructions
<TextLabel>:
text_size: self.size
''')
class Chat(Screen):
pass
class ChatApp(App):
def build(self):
def switch_view(*args, **kwargs):
global nick
nick = tx1_intro.text
lb1_main.text = "Chat with " + nick
Screens.current = 'main'
Window.size = (600, 500)
def msg_in(*args, **kwargs):
global nick
msg = tx1_main.text.strip("\n ")
if msg not in string.whitespace:
lb2_main.text += "{}: {}\n".format(nick, msg)
tx1_main.text = ""
fl_layout1_main.size_hint_y += 1
Screens = ScreenManager(transition = NoTransition())
chat = Chat(name = "main")
tx1_main = TextInput(size_hint = (0.9, 0.155),
pos_hint = {"top":0.177,"center_x":0.465})
scr_view_main = MyScrollView(pos_hint = {"top":0.87, "center_x":0.5},
size_hint = (0.97, 0.65),
do_scroll_x = False)
fl_layout1_main = FloatLayout(size_hint_y = 1)
lb2_main = TextLabel(size_hint = (0.99, 0.99),
pos_hint = {"center_x":0.5, "center_y":0.5},
valign = "top",
color = (0, 0, 0, 1))
chat.add_widget(scr_view_main)
scr_view_main.add_widget(fl_layout1_main)
fl_layout1_main.add_widget(lb2_main)
Screens.add_widget(chat)
return Screens
if __name__ == "__main__":
ChatApp().run()
答案 0 :(得分:0)
就我个人而言,我不喜欢FloatLayout
,但使用 docs 可以找出您需要输入的内容和自动内容。我认为BoxLayout
对于快速打字(仅使用size_hint
)比FloatLayout
更实用且更好,如果你不在某些你真正需要这种定位的东西上工作,那么没必要。
第一个示例是带滚动的简单标签,基本上全部都在kv
,因此保存它并在需要时复制和粘贴它(或学习如何操作)是很好的。第二个是可滚动标签,其中包含一些小部件,就像您提供的代码一样,只需要将其实现到您的代码中。 text_size
很重要,但你遗忘了一件事 - height
。当你通过self.size
时,你从头开始传递大小,而不是texture_size
,你需要什么。由于size_hint_y
为无,您可以使用height
手动设置texture_size
,from kivy.lang import Builder
from kivy.base import runTouchApp
from kivy.uix.scrollview import ScrollView
Builder.load_string('''
<ScrollLabel>:
Label:
size_hint_y:None
text_size:self.width,None
text:'l or emi psum '*1000
height:self.texture_size[1]
''')
class ScrollLabel(ScrollView):pass
runTouchApp(ScrollLabel())
在您传递一些文字后会自动更新。
简单的可滚动标签:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
Builder.load_string('''
<Box>:
orientation: 'vertical'
FloatLayout:
ScrollView:
Label:
id: lab
size_hint_y: None
text_size: self.width, None
text: 'l or emi psum '*100
height:self.texture_size[1]
Button:
size_hint_y: None
height: 100
on_release: root.ids.lab.text+='lo rem '*100
''')
class Box(BoxLayout): pass
class My(App):
def build(self):
return Box()
My().run()
周围的事情:
int[] integerArray = {1, 2, 3};
double[] doubleArray = new double[integerArray.length];
for(int i=0; i<integerArray.length; i++) {
doubleArray[i] = integerArray[i];
}