【问题标题】:Kivy : Relative size of text displaying in a popupKivy:弹出窗口中显示的文本的相对大小
【发布时间】:2013-11-20 14:17:19
【问题描述】:

有没有办法让标签中的消息/标题适合弹出窗口?

'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()

【问题讨论】:

    标签: python-2.7 kivy


    【解决方案1】:

    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))
    

    【讨论】:

      【解决方案2】:

      这是一个没有 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()
      

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

      【讨论】:

        猜你喜欢
        • 2020-10-12
        • 1970-01-01
        • 1970-01-01
        • 2020-05-31
        • 1970-01-01
        • 1970-01-01
        • 2017-02-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多