python-3.x Kivy Package 盒布局高度导致嵌套标签

jw5wzhpr  于 2023-02-17  发布在  Python
关注(0)|答案(1)|浏览(118)

我想添加一个指南文本到我的应用程序。我应该为它使用标签吗?有一个WrappedLabel类代码 Package 标签内的所有单词(只有一个附加信息)。我认为有一个Boxlayout高度问题。我创建标签并将其添加到boxlayout,然后将此boxlayout添加到gridlayout。
我想无论我添加到标签,Kivy必须给我一个平滑的boxlayot(不嵌套),所以它会看到很好的网格布局了。
为什么会有嵌套问题?我该如何解决这个问题?

****另一个问题是,如果我没有使用WrappedLabel类,我如何在标签中填充单词?

非常感谢

下面的解决方案不起作用,以解决高度问题。文本是嵌套标签有不同的高度,所以固定高度= 500不起作用。

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')
    • 年份代码:**
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文件:**
#: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
y4ekin9u

y4ekin9u1#

一个问题是当你使用python语句时,比如:

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

height = self.minimum_height部分在执行python语句时求值,并且height在以后添加子对象到BoxLayout时不会更新。要更新它,需要添加绑定,或者可以在kv中指定它(绑定是自动添加的)。
另外,我不明白为什么要将WrappedLabel示例添加到BoxLayouts,然后将那些BoxLayouts添加到GridLayout,为什么不直接将WrappedLabels添加到GridLayout呢?
以下是您可以对代码进行的一些更改,以获得所需的效果:
首先,像这样重新定义WrappedLabel类:

# Wrapped Label
class WrappedLabel(Label):
    pass

    # 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]))

并将<WrappedLabel>规则添加到kv

<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='vertical', height=self.minimum_height)

相关问题