Kivy:弹出窗口中文本的相对大小

时间:2013-11-20 14:17:20

标签: python-2.7 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 个答案:

答案 0 :(得分:2)

Label具有text_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))

答案 1 :(得分:0)

这是一个没有 BoxLayout 和 Button 的更简单的版本:

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

from functools import partial

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.popup import Popup


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

    def popup_display(self, title, message, widget):
        l = Label(text=message)
        l.bind(size=lambda s, w: s.setter('text_size')(s, w))

        popup = Popup(content=l, title=title, size_hint=(None, None), size=(300, 200), auto_dismiss=True)

        popup.open()

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

Popup result

只需在弹出窗口外部单击即可将其关闭。

相关问题