【问题标题】:Kivy: How to Show Loading Screen For My FunctionKivy:如何显示我的功能的加载屏幕
【发布时间】:2022-10-13 01:16:44
【问题描述】:

我是 Kivy 的初学者,所以我希望你能帮助我解决这个问题

  1. 我有向我们展示一些结果的underover 和side 函数。 界面运行完美,但是当我单击一个按钮时,我等待根据设备需要 2-3 秒的过程完成。我想在此过程中为我的 twu 函数显示加载屏幕(underover,side)

    2)还有一件事我如何也可以禁用此代码的screenmanager.transition?

    非常感谢所有cmets

    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.label import Label
    from kivy.metrics import dp
    import requests
    from kivy.uix.image import Image
    from kivy.uix.behaviors import ButtonBehavior
    
            
    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 Tor(BoxLayout):
    
        def __init__(self, **kwargs):
            super(Tor, self).__init__(**kwargs)
    
            self.status = True
            self.data = self.datas()
            self.Today = self.todayMatches()
    
    
        def homepage(self, s_image, screenmanager):
            
            if(screenmanager.current == 'underover_screen' or screenmanager.current == 'side_screen' or screenmanager.current == 'privacy_policy_screen'):
                screenmanager.transition.direction = 'right'
            screenmanager.current = 'homepage_screen'  
    
        def underOver(self, s_image, screenmanager):
    
            if(screenmanager.current == 'homepage_screen' or screenmanager.current == 'side_screen' or screenmanager.current == 'privacy_policy_screen'):
                screenmanager.transition.direction = 'left'
            screenmanager.current = 'underover_screen'
    
    
    
            print("""    
            Welcome to Under Over Goal Statics
            """)
            
            for i in range(len(self.Today[0])):
    
                # Some Calculations here #
                
                box = BoxLayout(size_hint_y = None, height = dp(50))
                sira = WrappedLabel(text = f'{i+1}', font_name = 'fonts/Lcd.ttf', font_size = dp(15), size_hint = (0.10, 1), halign='center' )
                evsahibi = WrappedLabel(text = f'{self.Today[0][i]}', font_name = 'Roboto', font_size = dp(15), size_hint = (0.225, 1), halign='center' )
                deplasman = WrappedLabel(text = f'{self.Today[1][i]}', font_name = 'Roboto', font_size = dp(15), size_hint = (0.225, 1), halign='center' )
                macbasigol = WrappedLabel(text = f'{sonuc}', font_name = 'fonts/Lcd.ttf', font_size = dp(15), size_hint = (0.15, 1), halign='center' )
                ust_2_5 = WrappedLabel(text = f'{predict2_5result}', font_name = 'fonts/Lcd.ttf', font_size = dp(15), size_hint = (0.15, 1), halign='center' )
                ust_3_5 = WrappedLabel(text = f'{predict3_5result}', font_name = 'fonts/Lcd.ttf', font_size = dp(15), size_hint = (0.15, 1), halign='center' )
                box.add_widget(sira)
                box.add_widget(evsahibi)
                box.add_widget(deplasman)
                box.add_widget(macbasigol)
                box.add_widget(ust_2_5)
                box.add_widget(ust_3_5)
                self.ids.gridsonuc.add_widget(box)
    
        def side(self, s_image, screenmanager):
    
            if(screenmanager.current == 'homepage_screen' or screenmanager.current == 'underover_screen' or screenmanager.current == 'privacy_policy_screen'):
                screenmanager.transition.direction = 'left'
            screenmanager.current = 'side_screen'
    
            print("""    
            Welcome to Winnig Side Statics
            """)
    
            for i in range(len(self.Today[0])):
    
                # Some Calculations here #
    
                box2 = BoxLayout(size_hint_y = None, height = 50)
                sira2 = WrappedLabel(text = f'{i+1}', font_name = 'fonts/Lcd.ttf', font_size = dp(15), size_hint = (0.10, 1), halign='center' )
                evsahibi2 = WrappedLabel(text = f'{self.Today[0][i]}', font_name = 'Roboto', font_size = dp(15), size_hint = (0.225, 1), halign='center' )
                deplasman2 = WrappedLabel(text = f'{self.Today[1][i]}', font_name = 'Roboto', font_size = dp(15), size_hint = (0.225, 1), halign='center' )
                evkazanmasans = WrappedLabel(text = f'{homeWin}', font_name = 'fonts/Lcd.ttf', font_size = dp(15), size_hint = (0.15, 1), halign='center' )
                beraberliksans = WrappedLabel(text = f'{draw}', font_name = 'fonts/Lcd.ttf', font_size = dp(15), size_hint = (0.15, 1), halign='center' )
                deplasmansans = WrappedLabel(text = f'{awayWin}', font_name = 'fonts/Lcd.ttf', font_size = dp(15), size_hint = (0.15, 1), halign='center' )
                box2.add_widget(sira2)
                box2.add_widget(evsahibi2)
                box2.add_widget(deplasman2)
                box2.add_widget(evkazanmasans)
                box2.add_widget(beraberliksans)
                box2.add_widget(deplasmansans)
                self.ids.gridsonuc2.add_widget(box2)
    
    
    
    class BetApp(App):
        def build(self):
            return Tor()
    
    
    if __name__ == '__main__':
        BetApp().run()
    

    KV 文件:

    Tor:
    <Tor>:
        ScreenManager:
            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_homege
                        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_homege.pos
                    Button:
                        id: side_button_homepage
                        on_press: 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                   
            Screen:
                name: 'underover_screen'
                Image: 
                    source: 'images/underover_background.png'
                    allow_stretch: True
                    keep_ratio: False
                BoxLayout:
                    spacing: '20dp'
                    orientation: 'vertical'    
                    BoxLayout:
                        size_hint: 1, 0.10
                        Label:
                            size_hint: 0.10, 1
                            text: '#'
                            font_name: 'fonts/Lcd.ttf'
                            font_size: '30dp'
                            color: 1, 0.4, 0.769, 1
                        Label:
                            size_hint: 0.225, 1
                            text: 'T1'
                            font_name: 'fonts/Lcd.ttf'
                            font_size: '30dp'
                            color: 1, 0.4, 0.769, 1
                        Label:
                            size_hint: 0.225, 1
                            text: 'T2'
                            font_name: 'fonts/Lcd.ttf'
                            font_size: '30dp'
                            color: 1, 0.4, 0.769, 1
                        Label:
                            size_hint: 0.15, 1
                            text: 'G.A'
                            font_name: 'fonts/Lcd.ttf'
                            font_size: '30dp'
                            color: 1, 0.4, 0.769, 1
                        Label:
                            size_hint: 0.15, 1
                            text: '2.5'
                            font_name: 'fonts/Lcd.ttf'
                            font_size: '30dp'
                            color: 1, 0.4, 0.769, 1
                        Label:
                            size_hint: 0.15, 1
                            text: '3.5'
                            font_name: 'fonts/Lcd.ttf'
                            font_size: '30dp'
                            color: 1, 0.4, 0.769, 1
                    BoxLayout:
                        size_hint: 1, 0.80  
                        ScrollView:
                            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:
                            id: home_button_underOver
                            on_press: root.homepage(img_home, sm)
                            background_color: 0, 0, 0, 0                 
                            Image:
                                id: img_home
                                source: 'images/home_button.png'
                                allow_stretch: True
                                keep_ratio: False
                                size: self.parent.size
                                pos: home_button_underOver.pos
                        Button:
                            id: side_button_underOver
                            on_press: 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_underOver.pos         
            Screen:
                name: 'side_screen'
                Image:
                    source: 'images/side_background.png'
                    allow_stretch: True
                    keep_ratio: False
                BoxLayout:
                    orientation: 'vertical'
                    spacing: '20dp'    
                    BoxLayout:
                        size_hint: 1, 0.10
                        Label:
                            size_hint: 0.10, 1
                            text: '#'
                            font_name: 'fonts/Lcd.ttf'
                            font_size: '30dp'
                            color: 1, 0.4, 0.769, 1
                        Label:
                            size_hint: 0.225, 1
                            text: 'T1'                        
                            font_name: 'fonts/Lcd.ttf'
                            font_size: '30dp'
                            color: 1, 0.4, 0.769, 1
                        Label:
                            size_hint: 0.225, 1
                            text: 'T2'
                            font_name: 'fonts/Lcd.ttf'
                            font_size: '30dp'
                            color: 1, 0.4, 0.769, 1
                        Label:
                            size_hint: 0.15, 1
                            text: 'H'
                            font_name: 'fonts/Lcd.ttf'
                            font_size: '30dp'
                            color: 1, 0.4, 0.769, 1
                        Label:
                            size_hint: 0.15, 1
                            text: 'D'
                            font_name: 'fonts/Lcd.ttf'
                            font_size: '30dp'
                            color: 1, 0.4, 0.769, 1
                        Label:
                            size_hint: 0.15, 1
                            text: 'A'
                            font_name: 'fonts/Lcd.ttf'
                            font_size: '30dp'
                            color: 1, 0.4, 0.769, 1
                    BoxLayout:
                        size_hint: 1, 0.80  
                        ScrollView:
                            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: gridsonuc2
                                cols: 1
                                spacing: '50dp'
                                size_hint_y: None
                                height: self.minimum_height    
                    BoxLayout:
                        size_hint: 1, 0.10
                        Button:
                            id: home_button_side
                            on_press: root.homepage(img_home, sm)
                            background_color: 0, 0, 0, 0                 
                            Image:
                                id: img_home
                                source: 'images/home_button.png'
                                allow_stretch: True
                                keep_ratio: False
                                size: self.parent.size
                                pos: home_button_side.pos
                        Button:
                            id: underOver_button_side
                            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_side.pos    
                      
            Screen:
                name: 'privacy_policy_screen'
                Image:
                    source: 'images/privacy_policy_background.png'
                    allow_stretch: True
                    keep_ratio: False
                                                                 
    

【问题讨论】:

  • 我会说这是一个糟糕的设计......什么都没有触发。我玩过你的代码,它什么也没做。
  • 使用带有Please Wait Label 之类的Popup 怎么样。
  • 我找到了一个可行的解决方案,但这次还有另一个问题。首先解决方案是弹出。我将分享解决方案。每次单击按钮时,问题都会一次又一次地添加所有结果。我按下按钮有 1,2,3,4,5 结果比我去主页然后返回再次单击按钮,它显示 1,2,3,4,5,1,2,3,4,5 我的意思是将结果添加到同一屏幕是缓存吗?我怎样才能解决这个问题

标签: python kivy kivy-language kivymd


【解决方案1】:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.metrics import dp
import requests
from kivy.uix.image import Image
from kivy.uix.behaviors import ButtonBehavior
import threading
from kivy.clock import mainthread
from kivy.uix.popup import Popup
from kivy.factory import Factory
from kivy.properties import ObjectProperty
import time

# fist add this
class PopupBox(Popup):
    pop_up_text = ObjectProperty()
    def update_pop_up_text(self, p_message):
        self.pop_up_text.text = p_message
        
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 Tor(BoxLayout):

    def __init__(self, **kwargs):
        super(Tor, self).__init__(**kwargs)

        self.status = True
        self.data = self.datas()
        self.Today = self.todayMatches()


    def homepage(self, s_image, screenmanager):
        
        if(screenmanager.current == 'underover_screen' or screenmanager.current == 'side_screen' or screenmanager.current == 'privacy_policy_screen'):
            screenmanager.transition.direction = 'right'
        screenmanager.current = 'homepage_screen'
  
    # second 1 add this function below
    def show_popup(self):
        self.pop_up = Factory.PopupBox()
        self.pop_up.update_pop_up_text('Loading...')
        self.pop_up.open()      

    def underOver(self, s_image, screenmanager):

        if(screenmanager.current == 'homepage_screen' or screenmanager.current == 'side_screen' or screenmanager.current == 'privacy_policy_screen'):
            screenmanager.transition.direction = 'left'
        screenmanager.current = 'underover_screen'    
          
        # added 3 lines
        self.show_popup()
        mythread = threading.Thread(target=self.underOver_hesaplama)
        mythread.start()
    
    # seperate onderover function and aded 4 lines in it
    @mainthread
    def underOver_hesaplama(self, *args):
        
        # aded 4 lines
        thistime = time.time() 
        while thistime + 2 > time.time(): # 2 seconds
            time.sleep(1)
        self.pop_up.dismiss()

        print("""    
        Welcome to Under Over Goal Statics
        """)
        
        for i in range(len(self.Today[0])):

            # Some Calculations here #
            
            box = BoxLayout(size_hint_y = None, height = dp(50))
            sira = WrappedLabel(text = f'{i+1}', font_name = 'fonts/Lcd.ttf', font_size = dp(15), size_hint = (0.10, 1), halign='center' )
            evsahibi = WrappedLabel(text = f'{self.Today[0][i]}', font_name = 'Roboto', font_size = dp(15), size_hint = (0.225, 1), halign='center' )
            deplasman = WrappedLabel(text = f'{self.Today[1][i]}', font_name = 'Roboto', font_size = dp(15), size_hint = (0.225, 1), halign='center' )
            macbasigol = WrappedLabel(text = f'{sonuc}', font_name = 'fonts/Lcd.ttf', font_size = dp(15), size_hint = (0.15, 1), halign='center' )
            ust_2_5 = WrappedLabel(text = f'{predict2_5result}', font_name = 'fonts/Lcd.ttf', font_size = dp(15), size_hint = (0.15, 1), halign='center' )
            ust_3_5 = WrappedLabel(text = f'{predict3_5result}', font_name = 'fonts/Lcd.ttf', font_size = dp(15), size_hint = (0.15, 1), halign='center' )
            box.add_widget(sira)
            box.add_widget(evsahibi)
            box.add_widget(deplasman)
            box.add_widget(macbasigol)
            box.add_widget(ust_2_5)
            box.add_widget(ust_3_5)
            self.ids.gridsonuc.add_widget(box)

    




class BetApp(App):
    def build(self):
        return Tor()


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

.kv文件

<PopupBox>:
    pop_up_text: _pop_up_text
    size_hint: .5, .5
    auto_dismiss: True
    title: 'Data'
    BoxLayout:
        orientation: "vertical"
        Label:
            id: _pop_up_text
            text: '' 

我认为它有效,但这次出现了问题。 每次单击按钮时,问题都会一次又一次地添加所有结果。我按下按钮有 1,2,3,4,5 结果比我去主页然后返回再次单击按钮,它显示 1,2,3,4,5,1,2,3,4,5 我的意思是将结果添加到同一屏幕是缓存吗?我怎么能解决这个我不知道...

【讨论】:

    猜你喜欢
    • 2015-01-17
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 2018-01-28
    • 1970-01-01
    • 2017-02-25
    相关资源
    最近更新 更多