第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

KivyMD//Python 如何創(chuàng)建一個加載對話框(彈出),當代碼在后臺運行另一個函數(shù)時顯示

KivyMD//Python 如何創(chuàng)建一個加載對話框(彈出),當代碼在后臺運行另一個函數(shù)時顯示

慕工程0101907 2023-11-09 21:25:34
我正在使用并發(fā).futures,目前看來我的代碼根據(jù)需要運行兩個函數(shù),但在兩個函數(shù)完成之前第一個彈出窗口不會顯示(問題是第二個函數(shù)首先關閉第一個對話框而結束-box,然后打開第二個對話框)。這是我的完整 .py 代碼:from kivy.lang import Builderfrom kivy.uix.boxlayout import BoxLayoutfrom kivymd.app import MDAppfrom kivymd.uix.dialog import MDDialogimport concurrent.futuresKV = '''<Content>    orientation: "vertical"    spacing: -40        MDSpinner:        size_hint: None, None        size: dp(46), dp(46)        pos_hint: {'center_x': 0.1}        active: True    MDLabel:        text: "Processing..."        pos_hint: {'center_x': .7}FloatLayout:    MDFlatButton:        text: "ALERT DIALOG"        pos_hint: {'center_x': .5, 'center_y': .5}        on_release: app.start()'''class Content(BoxLayout):    passclass Example(MDApp):    dialog = None    def build(self):        return Builder.load_string(KV)    def start(self):        with concurrent.futures.ThreadPoolExecutor() as executor:            f1 = executor.submit(self.pop_up1())            f2 = executor.submit(self.test())    def pop_up1(self):        '''Displays a pop_up with a spinning wheel'''        if not self.dialog:            self.dialog = MDDialog(                size_hint=(.45, None),                auto_dismiss=True,                type="custom",                content_cls=Content(),            )            self.dialog.open()    def test(self):        '''Counts to 1000000 and then it closes pop_up1 and opens pop_up2'''        for number in range(1000000):            print(number)        self.dismiss()        self.pop_up2()    def pop_up2(self):        if not self.dialog:            self.dialog = MDDialog(                title="Done",                size_hint=(.6, None),                text="Done",                    )        self.dialog.open()    def dismiss(self, *args):        self.dialog.dismiss()Example().run()
查看完整描述

1 回答

?
搖曳的薔薇

TA貢獻1793條經(jīng)驗 獲得超6個贊

您的代碼有幾個問題。首先,當您使用該構造時:


    with concurrent.futures.ThreadPoolExecutor() as executor:

        f1 = executor.submit(self.pop_up1())

        f2 = executor.submit(self.test())

有一個隱含的executor.sutdown()調用,等待提交的任務完成。請參閱文檔。由于您在主線程上使用此構造,因此所有 GUI 更新都將被阻止,直到所有提交的任務完成。因此,您通常不應在 kivy 應用程序的主線程上使用該構造。


第二個問題是 an 的創(chuàng)建和顯示MDDialog應該在主線程上完成,因此提交類似 的方法pop_up1()可能executor會出現(xiàn)問題。


第三個問題是您嘗試重用MDDialogwithif not self.dialog:條件。無論如何你都不能重復使用MDDialog。


因此,考慮到所有這些,這里是代碼的修改版本,可以實現(xiàn)我認為您想要的功能:


from kivy.clock import Clock, mainthread

from kivy.lang import Builder

from kivy.uix.boxlayout import BoxLayout

from kivymd.app import MDApp

from kivymd.uix.dialog import MDDialog

import concurrent.futures


KV = '''

<Content>

    orientation: "vertical"

    spacing: -40

    

    MDSpinner:

        size_hint: None, None

        size: dp(46), dp(46)

        pos_hint: {'center_x': 0.1}

        active: True

    MDLabel:

        text: "Processing..."

        pos_hint: {'center_x': .7}


FloatLayout:


    MDFlatButton:

        text: "ALERT DIALOG"

        pos_hint: {'center_x': .5, 'center_y': .5}

        on_release: app.start()

'''



class Content(BoxLayout):

    pass


class Example(MDApp):

    dialog = None


    def build(self):

        return Builder.load_string(KV)


    def start(self):

        # this must be done on the main thread

        self.pop_up1()


        executor = concurrent.futures.ThreadPoolExecutor()

        # f1 = executor.submit(self.pop_up1)  # this must be done on the main thread

        f2 = executor.submit(self.test)



    def pop_up1(self):

        '''Displays a pop_up with a spinning wheel'''

        self.dialog = MDDialog(

            size_hint=(.45, None),

            auto_dismiss=True,

            type="custom",

            content_cls=Content(),

        )

        self.dialog.open()


    def test(self):

        '''Counts to 1000000 and then it closes pop_up1 and opens pop_up2'''

        for number in range(100000):

            print(number)

        self.dismiss()

        Clock.schedule_once( self.pop_up2)  # pop_up2() must be done on the main thread


    def pop_up2(self,*args):

        self.dialog = MDDialog(

            title="Done",

            size_hint=(.6, None),

            text="Done",

                )

        self.dialog.open()


    @mainthread

    def dismiss(self, *args):

        self.dialog.dismiss()



Example().run()



查看完整回答
反對 回復 2023-11-09
  • 1 回答
  • 0 關注
  • 229 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號