2017-03-08 3 views
3

그래서 팝업에 긴 텍스트를 표시하려고합니다. 팝업의 높이가 텍스트의 길이에 따라 변경 될 수 있다면 이상적입니다. 그러나 그것은이 코드Kivy 레이블 및 팝업 + 긴 텍스트

boxl = uix.boxlayout.BoxLayout(orientation="vertical") 
    boxl2 = uix.boxlayout.BoxLayout(orientation="horizontal") 
    pop = Popup(title="Title", content=boxl, size_hint=(0.75,0.8)) 
    text = "Really long text" 

    document = uix.label.Label(text=text,markup=True, valign='top') 

    button = uix.button.Button(text='back', size_hint_y=None, height=40) 
    button2 = uix.button.Button(text="Button Title", size_hint_y=None, height=40) 
    button.bind(on_press=(lambda x:pop.dismiss())) 
    button2.bind(on_press=(lambda x,data=data:(self.set_vorteil(data),pop.dismiss()))) 
    boxl.add_widget(document) 

    boxl2.add_widget(button) 
    boxl2.add_widget(button2) 
    boxl.add_widget(boxl2) 
    document.bind(size=document.setter('text_size')) 
    pop.open() 

이와 흥미로운 것은 내 텍스트가 큰 여유 공간이 앞에있는 경우에도, 차단한다는 것입니다와 크기 변경없이이를 구현하기 위해 노력했습니다 내 유일한 문제가 아니에요 버튼. 이 문제를 어떻게 해결할 수 있습니까? 나는 키비의 레이블 사용 방법과 혼동 스럽다.

+0

어떻게 잘라내겠습니까? 유사한 코드를 사용하면 [this] (http://imgur.com/a/HMgXX) – ODiogoSilva

+0

@ODiogoSilva가 너무 짧습니다. 긴 텍스트를 사용하십시오. – KeyWeeUsr

+0

예제에서 문제를 일으키는 문자열 크기를 제공 할 수 있습니까? – ODiogoSilva

답변

3

이유는 두 BoxLayout 위젯 (boxlboxl2가) 실제로 절반의 공간을 (당신이 그것을 통지를하지 않은 경우에도) 분할되어 있다는 점이다. 당신이 당신의 Label이 모든 공간을 원한다면, 당신은 boxl2 위젯의 높이를 설정해야합니다 :

boxl2 = BoxLayout(orientation="horizontal", size_hint_y=None, height=40) 

이는 Button 위젯과 같은 높이로 boxl2 위젯을 설정

+0

답변 해 주셔서 감사합니다! – Bohr

2
document.bind(size=document.setter('text_size')) 

은 텍스트 영역을 특정 크기 (즉, 위젯 자체의 크기)로 제한합니다.

좋은 일이
boxl.add_widget(boxl2) 

,하지만 당신은 size_hint=(None, None)을 설정하는 것을 잊었다 때문에이 BoxLayout에 대한 특정 크기 :

enter image description here

그것은 당신이 한 것으로, 사실을 제외하고 꽤 잘 작동합니다 반면에, Button 자신을 위해 설정 했으므로 레이아웃 자체가 변경되었다고 생각하게되었습니다. (그렇지 않습니다.) 예를 들면 다음과 같습니다. BoxLayout(size_hint=(None, None), size=<desired size>).

또한이 방법은 사용자를 제한하므로 실제로 좋지 않습니다. 모바일 레이아웃이 너무 작 으면

from kivy.lang import Builder 
from kivy.base import runTouchApp 
runTouchApp(Builder.load_string(''' 
ScrollView: 
    Label: 
     size_hint_y: None 
     text_size: (self.width, None) 
     text: 'lorem ipsum dolor ' * 1000 
     height: self.texture_size[1] 
''')) 

을하거나 효율적으로 만들 수있는 RecycleView에서 그 구현을위한 : 차라리 스크롤 라벨 갈 것입니다. 당신이 LabelButton 위젯 사이에 너무 많은 여유 공간을 얻을 이유

0

만약 사람 이 수평선을 성장하는 방법

<[email protected]> 
height: content_id.height + 50 
width: '400dp' 
size_hint: (None, None) 

BoxLayout: 
    orientation: 'vertical' 
    id: content_id 
    size_hint: (None, None) 
    height: self.minimum_height + 25 
    width: root.width - 25 

    Label: 
     size_hint_y: None 
     height: self.texture_size[1] 
     text_size: (self.width, None) 
     text: 'A' * 400 
     padding: (0, 10) 

    Button: 
     size_hint_y: None 
     height: '40dp' 
     text: 'ok' 
  • 참조 : 관심, 여기에서 텍스트에 따라 자랍니다 팝업 내 구현 텍스트 길이를 변경하여 집계 (text: 'A' * 400)
  • 팝업의 너비를 변경하여 팝업 너비를 변경할 수 있습니다.