2013-11-20 104 views
1

是否有任何方法可以使標籤中的消息/標題符合彈出窗口?Kivy:在彈出窗口中顯示文本的相對大小

'message'或'title'可以是超出彈出框邊界的長文本。

def popup_display(self, title, message): 

     btnclose = Button(text='Close me', size_hint_y=None, height=50) 

     content = BoxLayout(orientation='vertical') 
     content.add_widget(Label(text=message)) 
     content.add_widget(btnclose) 

     popup = Popup(content=content, title=title, 
         size_hint=(None, None), 
         size=(300, 300), 
         auto_dismiss=False) 

     btnclose.bind(on_release=popup.dismiss) 

     popup.open() 

回答

2

Labeltext_size屬性,它允許約束其大小以給定的邊界框。您可以將其綁定到可用尺寸:

#!/usr/bin/python 
# -*- coding: utf-8 -*- 

from kivy.uix.popup import Popup 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.label import Label 
from kivy.uix.button import Button 
from kivy.app import App 
from kivy.lang import Builder 

from functools import partial 

class TestApp(App): 
    def build(self): 
     return Button(on_press=partial(self.popup_display, "title", "bar "*80)) 

    def popup_display(self, title, message, widget): 
     btnclose = Button(text='Close me', size_hint_y=None, height=50) 
     l = Label(text=message) 
     l.bind(size=lambda s, w: s.setter('text_size')(s, w)) 

     content = BoxLayout(orientation='vertical') 
     content.add_widget(l) 
     content.add_widget(btnclose) 
     popup = Popup(content=content, title=title, size_hint=(None, None), size=(300, 300), auto_dismiss=False) 
     btnclose.bind(on_release=popup.dismiss) 

     popup.open() 

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

不適合的文本將被刪除。如果你想擁有這一切,你應該在彈出窗口中使用ScrollView

至於標題,它不是一個標籤,但一個字符串,所以你能做的最好是添加新行:

return Button(on_press=partial(self.popup_display, "multiline\ntitle", "bar "*80))