You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kivy Python中Spinner下拉组件设置30号字体的问题咨询

如何设置Kivy Spinner组件的字体大小为30?

要让Spinner的主显示文本和下拉选项都使用30号字体,需要分别配置两个部分:

  • 主显示文本的字体大小:直接给Spinner实例设置font_size属性。
  • 下拉选项的字体大小:通过option_cls指定自定义的Label组件,并设置其font_size。

修改后的完整代码如下:

def show_osark_dialog(self):
    # Create a popup window for OSARK
    osark_popup = Popup(title='Matematik', size_hint=(None, None), size=(700, 400), auto_dismiss=True)

    # Content layout for the popup
    osark_content = GridLayout(cols=1, padding=10, spacing=10)
    
    # Create spinners for selecting times
    time_spinner1 = Spinner(text='00:00', values=[f'{h:02d}:{m:02d}' for h in range(24) for m in range(0, 60, 15)])
    # 设置主文本和下拉选项的字体大小
    time_spinner1.font_size = 30
    time_spinner1.option_cls = Label(font_size=30)

    time_spinner2 = Spinner(text='00:00', values=[f'{h:02d}:{m:02d}' for h in range(24) for m in range(0, 60, 15)])
    time_spinner2.font_size = 30
    time_spinner2.option_cls = Label(font_size=30)

    # Function to calculate time difference
    def calculate_time_difference(instance, value):
        time1 = datetime.strptime(time_spinner1.text, '%H:%M')
        time2 = datetime.strptime(time_spinner2.text, '%H:%M')
        time_diff = abs(time2 - time1)
        osark_label.text = f'Jobbade timmar: {time_diff}'

    # Bind the calculate_time_difference function to spinner values change
    time_spinner1.bind(text=calculate_time_difference)
    time_spinner2.bind(text=calculate_time_difference)

    # Label to display the time difference
    osark_label = Label(text='Välj start och sluttid ...')
    
    # Add spinners and label to the content layout
    osark_content.add_widget(time_spinner1)
    osark_content.add_widget(time_spinner2)
    osark_content.add_widget(osark_label)

    # Set the content of the popup window
    osark_popup.content = osark_content

    # Open the popup window
    osark_popup.open()

关键说明:

  1. font_size=30:控制Spinner按钮上选中状态文本的字体大小。
  2. option_cls=Label(font_size=30):指定下拉列表中每个选项使用带30号字体的Label组件,确保下拉选项的文字大小与主按钮一致。

内容的提问来源于stack exchange,提问作者SamirOnof

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 08:08:26