代码之家  ›  专栏  ›  技术社区  ›  user8694101

如何在kivy中使用弹出式滑块更改字体大小?

  •  0
  • user8694101  · 技术社区  · 7 年前

    嗨,这是我的简单RSTDocument编辑器代码。 我正在尝试使用弹出窗口中的滑块更改RSTdocument的基本字体大小。我该怎么做??我试过使用 slider_id.value 但它没有起作用。

    from kivy.app import App
    from kivy.lang import Builder
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.popup import Popup
    
    Builder.load_string('''
    <RST_GUI>:
    
        padding: 5
        spacing: 2
        orientation: 'vertical'
    
        BoxLayout:
            size_hint: (.5, .1)
            pos_hint: {'center_x': .5, 'center_y': 0.5}
            Button:
                text: 'Font Size'
                on_press: root.font_size()
        BoxLayout:
            TextInput:
                id: textinput
                tab_width: 5
            Splitter:
                sizeable_from: 'right'
                min_size: '5'
                RstDocument:
                    show_errors: True
                    base_font_size: #slider_id.value or something possible?
                    text: textinput.text
    
    <font_size>:
    
        size_hint: (.5, .3)
        pos_hint: {'center_x': .5, 'center_y': 0.5}
        title: "  Font size: " + str(slider_id.value)
        Slider:
            min: 20
            max: 50
            value:31
            step: 1
            id: slider_id
    
    
    ''')
    
    
    class RST_GUI(BoxLayout):
    
        def font_size(self):
            font_size().open()    
    
    class font_size(Popup):
        pass
    
    class RST(App):
        def build(self):
            return RST_GUI()
    
    if __name__ == '__main__':
        RST().run()
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   FJSevilla    7 年前

    id的作用域仅限于它在其中声明的规则,因此,您不能 base_font_size: slider_id.value . 有几种方法可以实现您的目标,一种可能是在 RST_GUI 类的实例,并使用 NumericProperty 设置字体大小:

    from kivy.app import App
    from kivy.lang import Builder
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.popup import Popup
    from kivy.properties import NumericProperty
    
    
    Builder.load_string('''
    <RST_GUI>:
        font_size: 31
        padding: 5
        spacing: 2
        orientation: 'vertical'
    
        BoxLayout:
            size_hint: (.5, .1)
            pos_hint: {'center_x': .5, 'center_y': 0.5}
            Button:
                text: 'Font Size'
                on_press: root.open_font_popup()
    
        BoxLayout:
            TextInput:
                id: textinput
                tab_width: 5
    
            Splitter:
                sizeable_from: 'right'
                min_size: '5'
                RstDocument:
                    show_errors: True
                    base_font_size: root.font_size
                    text: textinput.text
    
    <FontSizePopup>:
        font_size: slider_id.value
        size_hint: (.5, .3)
        pos_hint: {'center_x': .5, 'center_y': 0.5}
        title: "  Font size: " + str(slider_id.value)
        Slider:
            min: 20
            max: 50
            value:31
            step: 1
            id: slider_id
    ''')
    
    
    class RST_GUI(BoxLayout):
        font_size = NumericProperty()
    
        def __init__(self, **kwargs):
            super(RST_GUI, self).__init__(**kwargs)
            self.font_popup = FontSizePopup()
            self.font_popup.bind(font_size=self.change_font_size)
    
        def open_font_popup(self):
            self.font_popup.open()
    
        def change_font_size(self, instance, value):
            self.font_size = value
    
    
    class FontSizePopup(Popup):
        pass
    
    
    class RST(App):
        def build(self):
            return RST_GUI()
    
    if __name__ == '__main__':
        RST().run()
    

    enter image description here