Kivy: 如何调整按钮的合适大小?

huangapple go评论55阅读模式
英文:

Kivy: How Can i Adjust Button In Right Size?

问题

**主屏幕:**

你能帮我修复一下按钮的问题吗?
这是我的主屏幕。

[![进入图片描述][1]][1]

**我的目标:**

我希望我的按钮是这样的:
缩小50%并且彼此之间有空白。
[![进入图片描述][2]][2]

**KV 代码**

    #:import NoTransition kivy.uix.screenmanager.NoTransition
    <Predictor>:
        ScreenManager:
            transition: NoTransition()
            id: sm
            size: root.width, root.height
            Screen:
                name: 'homepage_screen'
                Image:
                    source: 'images/homepage_background.png'
                    allow_stretch: True
                    keep_ratio: False
                BoxLayout:
                    size_hint: 1, 0.10
                    Button:
                        id: underOver_button_homepage
                        on_press: root.underOver(img_underOver, sm)   
                        background_color: 0, 0, 0, 0                
                        Image:
                            id: img_underOver
                            source: 'images/underover_button.png'
                            allow_stretch: True
                            keep_ratio: False
                            size: self.parent.size
                            pos: underOver_button_homepage.pos       
                    Button:
                        id: side_button_homepage
                        on_release: root.side(img_side, sm)
                        background_color: 0, 0, 0, 0                
                        Image:
                            id: img_side
                            source: 'images/side_button.png' 
                            allow_stretch: True
                            keep_ratio: False
                            size: self.parent.size
                            pos: side_button_homepage.pos


  [1]: https://i.stack.imgur.com/jaiu9.png
  [2]: https://i.stack.imgur.com/rtV4e.png


**我尝试过但没有成功的方法**

    #:import NoTransition kivy.uix.screenmanager.NoTransition
    <Predictor>:
        ScreenManager:
            transition: NoTransition()
            id: sm
            size: root.width, root.height
            Screen:
                name: 'homepage_screen'
                Image:
                    source: 'images/homepage_background.png'
                    allow_stretch: True
                    keep_ratio: False
                BoxLayout:
                    size_hint: 1, 0.10
                    Button:
                        size-hint: 0.50, 1 # 我添加了这一行 ******************
                        id: underOver_button_homepage
                        on_press: root.underOver(img_underOver, sm)   
                        background_color: 0, 0, 0, 0                
                        Image:
                            id: img_underOver
                            source: 'images/underover_button.png'
                            allow_stretch: True
                            keep_ratio: False
                            size: self.parent.size
                            pos: underOver_button_homepage.pos       
                    Button:
                        size-hint: 0.50, 1 # 我添加了这一行 *************************
                        id: side_button_homepage
                        on_release: root.side(img_side, sm)
                        background_color: 0, 0, 0, 0                
                        Image:
                            id: img_side
                            source: 'images/side_button.png' 
                            allow_stretch: True
                            keep_ratio: False
                            size: self.parent.size
                            pos: side_button_homepage.pos


我添加了 size-hint: 0.50, 1 到按钮但没有起作用。
我该如何修复这个问题。
我在代码中没有使用任何窗口配置。 我在项目中使用了 allow_stretch: True 和 keep_ratio: False 代码。
非常感谢
英文:

Main screen:

Could you please help me to fix my button problem.
Here is my Home screen.

Kivy: 如何调整按钮的合适大小?

MY Goal:

I want my buttons like this:
%50 smaller and have blanks between each other.
Kivy: 如何调整按钮的合适大小?

KV codes

#:import NoTransition kivy.uix.screenmanager.NoTransition
<Predictor>:
ScreenManager:
transition: NoTransition()
id: sm
size: root.width, root.height
Screen:
name: 'homepage_screen'
Image:
source: 'images/homepage_background.png'
allow_stretch: True
keep_ratio: False
BoxLayout:
size_hint: 1, 0.10
Button:
id: underOver_button_homepage
on_press: root.underOver(img_underOver, sm)   
background_color: 0, 0, 0, 0                
Image:
id: img_underOver
source: 'images/underover_button.png'
allow_stretch: True
keep_ratio: False
size: self.parent.size
pos: underOver_button_homepage.pos       
Button:
id: side_button_homepage
on_release: root.side(img_side, sm)
background_color: 0, 0, 0, 0                
Image:
id: img_side
source: 'images/side_button.png' 
allow_stretch: True
keep_ratio: False
size: self.parent.size
pos: side_button_homepage.pos

What i tried and not worked

#:import NoTransition kivy.uix.screenmanager.NoTransition
<Predictor>:
ScreenManager:
transition: NoTransition()
id: sm
size: root.width, root.height
Screen:
name: 'homepage_screen'
Image:
source: 'images/homepage_background.png'
allow_stretch: True
keep_ratio: False
BoxLayout:
size_hint: 1, 0.10
Button:
size-hint: 0.50, 1 # I ADDED THIS LINE **********************
id: underOver_button_homepage
on_press: root.underOver(img_underOver, sm)   
background_color: 0, 0, 0, 0                
Image:
id: img_underOver
source: 'images/underover_button.png'
allow_stretch: True
keep_ratio: False
size: self.parent.size
pos: underOver_button_homepage.pos       
Button:
size-hint: 0.50, 1 # I ADDED THIS LINE ******************************
id: side_button_homepage
on_release: root.side(img_side, sm)
background_color: 0, 0, 0, 0                
Image:
id: img_side
source: 'images/side_button.png' 
allow_stretch: True
keep_ratio: False
size: self.parent.size
pos: side_button_homepage.pos

I addded size-hint: 0.50, 1 to buttons but not worked.
How can i fix this.
I did't use any window config on my code. I used allow_stretch: True and keep_ratio: False codes for my project.
Thanks very much

答案1

得分: 0

BoxLayout尝试将所有可用空间分配给其子元素,因此按size_hint的比例分配其空间。只要使用size_hintBoxLayout将确保其子元素填充整个宽度。

要获得所需的效果,一种简单的方法是将每个Button放在AnchorLayout中。AnchorLayout默认情况下会使其子元素居中,而您添加的size_hint现在是指AnchorLayout的大小。以下是修改后的kv文件:

#:import NoTransition kivy.uix.screenmanager.NoTransition
<Predictor>:
    ScreenManager:
        transition: NoTransition()
        id: sm
        size: root.width, root.height
        Screen:
            name: 'homepage_screen'
            Image:
                source: 'images/homepage_background.png'
                allow_stretch: True
                keep_ratio: False
            BoxLayout:
                size_hint: 1, 0.10
                AnchorLayout:
                    Button:
                        size_hint: 0.50, 1 # 我添加了这一行 ****************
                        id: underOver_button_homepage
                        on_press: root.underOver(img_underOver, sm)   
                        background_color: 0, 0, 0, 0                
                        Image:
                            id: img_underOver
                            source: 'images/underover_button.png'
                            allow_stretch: True
                            keep_ratio: False
                            size: self.parent.size
                            pos: underOver_button_homepage.pos     

                AnchorLayout:  
                    Button:
                        size_hint: 0.50, 1 # 我添加了这一行 ****************
                        id: side_button_homepage
                        on_release: root.side(img_side, sm)
                        background_color: 0, 0, 0, 0                
                        Image:
                            id: img_side
                            source: 'images/side_button.png' 
                            allow_stretch: True
                            keep_ratio: False
                            size: self.parent.size
                            pos: side_button_homepage.pos
英文:

The BoxLayout tries to give all its space to its children, so it portions out its space in proportion to the size_hint. As long as you just use size_hint, the Boxlayout will make sure that its children fill its entire width.

An easy way to get what you want is to just put each Button in an AnchorLayout. The AnchorLayout centers its child by default, and the size_hint that you added now refers to the AnchorLayout size. Here is a modified version of your kv that does this:

#:import NoTransition kivy.uix.screenmanager.NoTransition
&lt;Predictor&gt;:
ScreenManager:
transition: NoTransition()
id: sm
size: root.width, root.height
Screen:
name: &#39;homepage_screen&#39;
Image:
source: &#39;images/homepage_background.png&#39;
allow_stretch: True
keep_ratio: False
BoxLayout:
size_hint: 1, 0.10
AnchorLayout:
Button:
size_hint: 0.50, 1 # I ADDED THIS LINE **********************
id: underOver_button_homepage
on_press: root.underOver(img_underOver, sm)   
background_color: 0, 0, 0, 0                
Image:
id: img_underOver
source: &#39;images/underover_button.png&#39;
allow_stretch: True
keep_ratio: False
size: self.parent.size
pos: underOver_button_homepage.pos     
AnchorLayout:  
Button:
size_hint: 0.50, 1 # I ADDED THIS LINE ******************************
id: side_button_homepage
on_release: root.side(img_side, sm)
background_color: 0, 0, 0, 0                
Image:
id: img_side
source: &#39;images/side_button.png&#39; 
allow_stretch: True
keep_ratio: False
size: self.parent.size
pos: side_button_homepage.pos

huangapple
  • 本文由 发表于 2023年2月16日 02:29:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定