简体   繁体   English

当当前小部件类的大小基于 Kivy 中的另一个小部件类时如何调整其大小

[英]How to adjust the size of current widget class when its size based on another widget class in Kivy

I am having too many types of buttons in my project, the problem is they all have different sizes.我的项目中有太多类型的按钮,问题是它们都有不同的大小。 So I decided to create a BaseButtonSize class and then apply its size to the rest.所以我决定创建一个 BaseButtonSize 类,然后将其大小应用于其余部分。 If I want to change the button size, I just simply take the BaseButtonSize.width (or .height) and multiply it with a specific number (you can see it in the code in KButton2 class).如果我想更改按钮大小,我只需简单地将 BaseButtonSize.width(或 .height)乘以特定数字(您可以在 KButton2 类的代码中看到它)。

But it doesn't seem to be working.但这似乎不起作用。 And the traceback returned 5292 lines .-.回溯返回 5292 行.-。

So is there any way to fix or better way to do that?那么有没有办法解决或更好的方法来做到这一点? When you can adjust the size of the current widget that you taken from another class.当您可以调整从另一个类中获取的当前小部件的大小时。

from kivy.uix.button import Button
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.boxlayout import BoxLayout

from kivy.lang import Builder


Builder.load_string("""
#:import Window kivy.core.window.Window

<ViewPort>:
    KButton1:
    KButton2:

<BaseButtonSize>:
    size_hint: None,None
    size: Window.width*0.0441,Window.height*0.147

<KButton1>:
    size_hint: None,None
    size: button.size
    
    BaseButtonSize:
        id: button

<KButton2>:
    size_hint: None,None
    size: button.size
    
    BaseButtonSize:
        id: button
        width: self.width*1.25 # <<<<===== Can not adjust the width (Remove this line will work fine)
""")

class BaseButtonSize(Button):
    pass

class KButton1(RelativeLayout):
    pass

class KButton2(RelativeLayout):
    pass

class ViewPort(BoxLayout):
    pass

if __name__=="__main__":
    from kivy.app import App
    class TestApp(App):
        def build(self):
            return ViewPort()
    
    TestApp().run()

Using width: self.width*1.25 creates an infinite loop.使用width: self.width*1.25创建一个无限循环。 A width is assigned to BaseButtonSize , then it is adjusted to 1.25 times the width , which changes the width and triggers the above kv rule, which adjusted the width by again multiplying it by 1.25, which triggers the above rule again, ...BaseButtonSize分配一个width ,然后将其调整为width的 1.25 倍,这会改变width并触发上面的kv规则,它通过再次将width乘以 1.25 来调整宽度,这再次触发了上面的规则,...

You can avoid this infinite loop by using:您可以使用以下方法避免这种无限循环:

width: Window.width*0.0441*1.25

Or, you can set a key in your kv , like this:或者,您可以在kv中设置一个key ,如下所示:

#:set w Window.width*0.0441

Then use that elsewhere in the kv :然后在kv的其他地方使用它:

<BaseButtonSize>:
    size_hint: None,None
    size: w,Window.height*0.147

and:和:

BaseButtonSize:
    id: button
    width: w*1.25

Both of these approaches eliminate the infinite loop.这两种方法都消除了无限循环。

Yet another approach (that I think solves all your issues) is to just set a key to the 0.0441 value, and use that elsewhere in your kv .另一种方法(我认为可以解决您的所有问题)是将key设置为0.0441值,然后在kv的其他地方使用它。 And set the size in the KButton class, allowing the default size_hint of BaseButton to adjust its size.并在KButton类中设置大小,允许BaseButton的默认size_hint调整其大小。 Here is a modified version of your code that does this:这是执行此操作的代码的修改版本:

from kivy.uix.button import Button
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.boxlayout import BoxLayout

from kivy.lang import Builder

Builder.load_string("""
#:import Window kivy.core.window.Window
#: set w_factor 0.0441

<ViewPort>:
    KButton:
        text: '1'
    KButton:
        text: '2'
        width_factor: 1.25

<KButton>:
    width_factor: 1
    size_hint: None,None
    size: Window.width * self.width_factor * w_factor, Window.height*0.147
    text: ''

    BaseButtonSize:
        text: root.text
""")


class BaseButtonSize(Button):
    pass


class KButton(RelativeLayout):
    pass


class ViewPort(BoxLayout):
    pass


if __name__ == "__main__":
    from kivy.app import App


    class TestApp(App):
        def build(self):
            return ViewPort()


    TestApp().run()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM