How to control the font size in Kivy settings panel for the menu item name in settings menu list

21 Views Asked by At

I tried to use the Kivy settings panel in my application, but I found that the font size of some settings panel items is related to the font size setting in my application, currently I can not set the font size of settings panel directly. In my application for displaying effect it uses a large font size, then in settings panel its menu list and the section name of option list will use the same font size of the application, this makes the settings panel appearance is very strange (cannot be acceptable).

The following code is a Kivy scatter example (from geeksforgeeks.org)

the scattertest.py file

import kivy 
from kivy.app import App
kivy.require('1.9.0')  
from kivy.uix.scatter import Scatter 
from kivy.uix.widget import Widget
from kivy.uix.relativelayout import RelativeLayout 

from kivy.uix.settings import SettingsWithSidebar, SettingsWithTabbedPanel
from kivy.logger import Logger

class SquareWidget(Widget): 
    pass

class ScatterWidget(Scatter): 
    pass

class ScatterTest(RelativeLayout): 
    pass

class ScatterTestApp(App): 

    def build(self):                
        self.settings_cls = SettingsWithTabbedPanel
        # self.settings_cls = SettingsWithSidebar
        return ScatterTest()

    def app_open_settings(self):
        App.open_settings(self)

    def close_settings(self, settings=None):
        Logger.info("scattertest.py: App.close_settings: {0}".format(settings))
        super(ScatterTestApp, self).close_settings(settings)

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

the scattertest.kv file

#:kivy 2.1.0

<Label>:
    font_size:50

<SquareWidget>: 
    size: 100, 100
    canvas: 
        Color: 
            rgb: [0.345, 0.85, 0.456] 
        Rectangle: 
            size: self.size 
            pos: self.pos 

<ScatterTest>:
    canvas: 
        Color: 
            rgb: .8, .5, .4
        Rectangle: 
            size: self.size 
            pos: self.pos   

    ScatterWidget: 
        id: square_widget_id  
   
        SquareWidget: 

    Label:
        text: 'Position: ' + str(square_widget_id.pos) 
        size_hint: .1, .1
        pos: 500, 300

In the above example, I set the global font size of Label widget as 50 under <Label>:, then in app part the 'Label' item will show the 'Position: ' and followed point coordinate with font size 50.

And when pressing the 'F1' key, it will show the settings panel, in here I set the settings panel using TabbedPanel style, we can see that the panel tab name using the font size 50 and in the options part the section name also uses font size 50. This let's feel that the settings panel is some thing wrong (all these issue items should base on the Label class).

I read the Kivy Settings document, but I can not find a good way to set the font size of settings panel directly.

I have tried to set a custom Label widget in .kv file like modifying <Label>: to <LabelA@Label>: and changed the Label item of the following code to LabelA, then run the app and shows the settings panel, now the settings panel will use its default font size, all items are suitable. This way should be a solution, but I feel that it is just a way of no way.

Hope may help me to get a better solution, thanks!

1

There are 1 best solutions below

0
phchen2 On

I found a way that may give a solution to my question, that is to modify some settings related to some widgets in the Kivy style.kv file (the Kivy style.kv is located at ...\Lib\site_packages\kivy\data directory), the references about this solution are as below (in my referring order),

  1. Customise Kivy settings panel by redefining style.kv
  2. https://kivy.org/doc/stable/api-kivy.lang.html#redefining-a-widget-s-style
  3. How to customize style.kv in Kivy

The base of this solution should be the reference #2, it is the section 'Redefining a widget’s style' in Kivy document 'Kivy Language', it states that we may redefine some properties of a widget, that is we may inherit the properties of a widget and without some other properties that will use our own definitions. This way is to define the modified widget in .kv file with (note the dash '-' before the widget name)

<-widgetname>:
    redefined properties

Basically this way is making a custom widget, in principle it is the same as my solution in my question. If we do not modify many settings items, and just want to keep the default setting of settings panel, then my solution is more easy.