首页 > 解决方案 > 如何更改kivy中的按钮大小?

问题描述

我正在使用 kivy 在 Python 中编写程序,但无法更改在两个屏幕之间来回转换的按钮的大小

我想不出任何原因为什么我无法用“size: 75, 50”之类的东西来改变它的大小,是不是因为这个类是从 Screen 而不是 Button 继承的?

蟒蛇文件:

import kivy
from kivy.app import App
kivy.require("1.10.1")
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.screenmanager import ScreenManager

class ScreenRoot(Screen):   
    pass

class OtherScreen(Screen):   
    pass

class ScreenUpkeep(ScreenManager):    
    pass

view = Builder.load_file("main.kv")

    class MainApp(App):
        def build(self):
            return view

if __name__ == "__main__":
    MainApp().run()

对应的.kv文件:

ScreenUpkeep:
    ScreenRoot:
    OtherScreen:    

<ScreenRoot>:
    name: "rootmain"
    Button:
        text: "Next Screen"
        font_size: 40
        on_release: app.root.current = "other"
        size: 75, 50
<OtherScreen>:
    name: "other"
    Button:
        text: "Return"
        font_size: 40
            on_release: app.root.current = "rootmain"

我只是希望能够更改按钮的大小,以便能够在每个屏幕上包含更多内容,例如文本和图片。

标签: pythonkivy

解决方案


您必须禁用size_hint,为了更好地可视化它,我将更改按钮的字体:

Button:
    text: "Next Screen"
    font_size: 12
    on_release: app.root.current = "other"
    size: 75, 50
    size_hint: None, None # <---

在此处输入图像描述


推荐阅读