Kivy BoxLayout高度导致嵌套标签

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

Kivy Boxlayout Height Causes Nested Labels

问题

未使用 WrappedLabel 类时,如何填充标签中的文字?

感谢您的提问。 如果您没有使用 WrappedLabel 类,要填充标签中的文字,您可以直接使用 Kivy 中的 Label 类,并使用标准的文本填充方法,例如:

label = Label(text="Your text goes here")

这将创建一个标签并将文字填充到标签中。但请注意,对于较长的文本,可能需要处理标签的大小和布局以确保文字能够正确显示。

希望这能帮助到您。

英文:

I want to add a guide text to my app.
Should i use label for it?
There is a WrappedLabel Class Code to wrap all words inside label (only an additional information).
i think there is a Boxlayout height problem.
I create labels and add it to boxlayout then add this boxlayout to gridlayout.

I want whatever i add to labels, Kivy must show me a smooth boxlayot (not nested) so it will seen nice in gridlayout too.

Why there is a nested problem?
How can i fix this?

**** And another question if i had not used WrappedLabel Class how could i have filled the words in the label?

Thanks very much

Kivy BoxLayout高度导致嵌套标签

below solutions did not work to fix height problem.
texts are nested
Labels have different heights so fixed height = 500 not worked.

box3 = BoxLayout(size_hint_y = None, orientation = 'vertical', height = self.minimum_height)
box3 = BoxLayout(size_hint_y = None, orientation = 'vertical', height = 500)
box3 = BoxLayout(size_hint_y = None, orientation = 'vertical')

PY Code:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.metrics import dp
from kivy.uix.image import Image
from kivy.uix.behaviors import ButtonBehavior
from kivy.clock import Clock
from kivy.uix.popup import Popup
from kivy.factory import Factory
from kivy.properties import ObjectProperty
import requests

# Pop Up
class PopupBox(Popup):

    pop_up_text = ObjectProperty()
    def update_pop_up_text(self, p_message):
        self.pop_up_text.text = p_message

# Wrapped Label        
class WrappedLabel(Label):
    
    def __init__(self, **kwargs):
        super(WrappedLabel, self).__init__(**kwargs)

        self.bind(
            width=lambda *x: self.setter('text_size')(self, (self.width, None)),
            texture_size = lambda *x: self.setter('height')(self, self.texture_size[1]))

class Test(BoxLayout):

    # Homepage Screen
    def homepage(self, screenmanager):        
        
        screenmanager.current = 'homepage_screen'
        Clock.schedule_once(self.clear_widgets)

    # Pop Up    
    def show_popup(self):
        
        self.pop_up = Factory.PopupBox()
        self.pop_up.update_pop_up_text('Loading...')
        self.pop_up.open() 

    def clear_widgets(self, *args):

        for child in [child for child in self.ids.gridsonuc.children]:
            self.ids.gridsonuc.remove_widget(child)      

    def underOver(self,screenmanager):
        
        screenmanager.current = 'underover_screen'

        self.show_popup()
        Clock.schedule_once(self.clear_widgets)     
        Clock.schedule_once(self.underOver_hesaplama)

    def underOver_hesaplama(self, *args):        

        print("""    
        Welcome to Under Over Goal Statics
        """)
        
        box3 = BoxLayout(size_hint_y = None, orientation = 'vertical', height = self.minimum_height)
        one = WrappedLabel(text = '''
[color=#ff66c4][b]>>> WHAT IS FOOTBALL PREDICTOR? <<<[/b][/color]
        ''', font_name = 'Roboto', font_size = dp(20), halign='left', markup = True)
        two = WrappedLabel(text = '''
1)Football Predictor is an application that calculates the goals per match ratio and winning percentage.
* Goals per match ratio calculation: Only the home results of the home teams and the away results of the away teams are used, so this algorithm allows us to estimate matches in a high success rate! 

2) Football Predictor helps us to find valuable odds.
High odds means high payout and a corresponding low probability of occurring.
Low odds means low payout and a corresponding high probability of occurring.
If there is high odd bet and we know that it has a high probability of occurring, this is a valuable odd.
In this guide i am going to teach you how to find valuable odds.

3) Football Predictions are updated every night at 00:10 AM (UTC+3)
        ''', font_name = 'Roboto', font_size = dp(15), halign='left', markup = True)
        three = WrappedLabel(text = '''
[color=#ff66c4][b]>>> FOOTBALL PREDICTOR'S ALGORITHM <<<[/b][/color]
        ''', font_name = 'Roboto', font_size = dp(20), halign='left', markup = True)
        four = WrappedLabel(text = '''
1) Goals Per Match Ratio Algorithm : Average Goals Per Game Calculation!
(Goals scored by the Home team while playing at Home + Goals conceded by the Away team while playing Away ) / (Number of Home games played by the Home team + Number of Away games played by the Away team) + (Goals scored by the Away team while playing Away + Goals conceded by the Home team while playing at Home) / (Number of Home games played by the Home team + Number of Away games played by the Away team)

2) 1X2 Winning Percentage Algorithm :  Home, Draw or Away Team's Winning Chance

Home Team's Winning Percentage: 
(Number of matches won by the Home team at Home + Number of matches lost by the Away team at Away) / (Number of Home games played by the Home team + Number of Away games played by the Away team) * 100

Draw Percentage: 
(Number of  matches that Draw by the Home team at Home + Number of  matches that Draw by the Away team at Away) / (Number of Home games played by the Home team + Number of Away games played by the Away team) * 100

Away Team's Winning Percentage: 
(Number of matches won by the Away team at Away + Number of matches lost by the Home team at Home) / (Number of Home games played by the Home team + Number of Away games played by the Away team) * 100
        ''', font_name = 'Roboto', font_size = dp(15), halign='left', markup = True)
        box3.add_widget(one)
        box3.add_widget(two)
        box3.add_widget(three)
        box3.add_widget(four)
        self.ids.gridsonuc.add_widget(box3)
            
        self.pop_up.dismiss()
  

class StackoverflowApp(App):
    def build(self): 
        return Test()

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

KV File:

#:import NoTransition kivy.uix.screenmanager.NoTransition
<Test>:
    ScreenManager:
        transition: NoTransition()
        id: sm
        size: root.width, root.height
        Screen:
            name: 'homepage_screen'            
            BoxLayout:
                size_hint: 1, 0.10
                Button:
                    text: 'Calculate'
                    id: underOver_button_homepage
                    on_press: root.underOver(sm)     
                    background_color: 0, 0, 0, 0                                  
        Screen:
            name: 'underover_screen'
            BoxLayout:
                spacing: '20dp'
                orientation: 'vertical'    
                BoxLayout:
                    size_hint: 1, 0.10
                    Label:
                        size_hint: 1, 1
                        text: 'GUIDE'
                        font_size: '30dp'
                        color: 1, 0.4, 0.769, 1
                BoxLayout:
                    size_hint: 1, 0.80
                    ScrollView:
                        scroll_type: ['bars', 'content']
                        bar_margin: '5dp'
                        bar_color: 1, 0.4, 0.769, 1 
                        bar_width: '5dp'
                        bar_inactive_color: 1, 0.4, 0.769, 1
                        GridLayout:                            
                            id: gridsonuc
                            cols: 1
                            spacing: '50dp'
                            size_hint_y: None
                            height: self.minimum_height        
                BoxLayout:
                    size_hint: 1, 0.10
                    Button:
                        text: 'Home'
                        id: home_button_underOver
                        on_press: root.homepage(sm)
                        background_color: 0, 0, 0, 0                 

<PopupBox>:
    pop_up_text: _pop_up_text
    background_color: '#38B6FF'
    background: 'white'
    size_hint: .5, .5
    auto_dismiss: True
    title: 'Data'
    title_size: '15dp'
    BoxLayout:
        orientation: "vertical"
        Label:
            id: _pop_up_text
            text: ''
            font_size: '30dp'
            color: 1, 0.4, 0.769, 1   

答案1

得分: 0

以下是您要求的内容的翻译:

问题之一是,当您使用像这样的 Python 语句时:

box3 = BoxLayout(size_hint_y = None, orientation = 'vertical', height = self.minimum_height)

其中的 height = self.minimum_height 部分在执行该 Python 语句时进行了评估,而在将子项添加到 BoxLayout 后,height 不会更新。要使其更新,您需要添加一个绑定,或者您可以在 kv 中指定它(在那里会自动为您添加绑定)。

另外,我不明白为什么要将您的 WrappedLabel 实例添加到 BoxLayouts,然后将这些 BoxLayouts 添加到 GridLayout 中。为什么不直接将 WrappedLabels 添加到 GridLayout 中呢?

以下是您可以对代码进行的一些更改,以获得您想要的效果:

首先,重新定义 WrappedLabel 类如下:

# 包装标签
class WrappedLabel(Label):
    pass

然后在 kv 中添加一个 <WrappedLabel> 规则:

<WrappedLabel>:
    size_hint: None, None
    text_size: [self.parent.width*.95, None] if self.parent else [1,1]
    size: self.texture_size

这个规则允许 WrappedLabel 在垂直方向扩展,同时保持其宽度与其父级的宽度匹配。if/else 构造只是在分配 parent 之前避免引发异常。

然后,在您的 underOver_hesaplama() 方法中,将以下部分替换为:

box3.add_widget(one)
box3.add_widget(two)
box3.add_widget(three)
box3.add_widget(four)
self.ids.gridsonuc.add_widget(box3)

替换为:

self.ids.gridsonuc.add_widget(one)
self.ids.gridsonuc.add_widget(two)
self.ids.gridsonuc.add_widget(three)
self.ids.gridsonuc.add_widget(four)

并且由于它不再被使用,您可以消除:

box3 = BoxLayout(size_hint_y=None, orientation=&#39;vertical&#39;, height=self.minimum_height)
英文:

One issue is that when you use a python statement like:

box3 = BoxLayout(size_hint_y = None, orientation = &#39;vertical&#39;, height = self.minimum_height)

the height = self.minimum_height part is evaluated when that python statement is executed, and the height is not updated later when children are added to the BoxLayout. To get it to update, you need to add a binding, or you could specify it in kv (where bindings are added for you automatically).

Also, I don't understand why you are adding your WrappedLabel instances to BoxLayouts and then adding those BoxLayouts to the GridLayout. Why not just add the WrappedLabels to the GridLayout?

Here are some changes that you can make to your code to get what you want:

First, redefine the WrappedLabel class like this:

# Wrapped Label
class WrappedLabel(Label):
    pass

    # def __init__(self, **kwargs):
    #     super(WrappedLabel, self).__init__(**kwargs)
    #
    #     self.bind(
    #         width=lambda *x: self.setter(&#39;text_size&#39;)(self, (self.width, None)),
    #         texture_size=lambda *x: self.setter(&#39;height&#39;)(self, self.texture_size[1]))

and add a &lt;WrappedLabel&gt; rule to the kv:

&lt;WrappedLabel&gt;:
    size_hint: None, None
    text_size: [self.parent.width*.95, None] if self.parent else [1,1]
    size: self.texture_size

This rule allows the WrappedLabel to expand vertically while keeping its width matching its parents width. The if/else construct just avoids exceptions being thrown before a parent is assigned.

Then, in your underOver_hesaplama() method, replace:

    box3.add_widget(one)
    box3.add_widget(two)
    box3.add_widget(three)
    box3.add_widget(four)
    self.ids.gridsonuc.add_widget(box3)

with:

    self.ids.gridsonuc.add_widget(one)
    self.ids.gridsonuc.add_widget(two)
    self.ids.gridsonuc.add_widget(three)
    self.ids.gridsonuc.add_widget(four)

And since it is no longer used, you can eliminate:

    box3 = BoxLayout(size_hint_y=None, orientation=&#39;vertical&#39;, height=self.minimum_height)

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

发表评论

匿名网友

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

确定