弹出自动关闭

2024-09-30 03:26:02 发布

您现在位置:Python中文网/ 问答频道 /正文

环境:

我有一个主屏幕和两个弹出窗口。当点击主屏幕上的按钮时,会出现第一个弹出窗口。在第一个弹出窗口中选择选项并按下该弹出窗口的按钮应关闭第一个弹出窗口并打开第二个弹出窗口。到这里为止是成功的。你知道吗

多个_弹出.py你知道吗

import kivy
kivy.require('1.11.0')
import os
os.environ['KIVY_GL_BACKEND'] = 'gl'
import time
from kivy.app import App
from kivy.core.text import LabelBase
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock

class MainScreen(BoxLayout):

    sw_switch = 'none'  

    def process_1(self):

        print("The process will be running here")
        time.sleep(5)
        self.sw_switch = 'over' 


    def setName(self,*args):
        FirstPopup().open() 


    def setName1(self,*args):
        self.sw_switch = 'true1' 
        SecondPopup().open()    

#------------------------------------
class FirstPopup(Popup):

    def popup1_but(self):
        self.dismiss()

class SecondPopup(Popup):

    def popup2_but(self):
        self.dismiss()
    pass
#------------------------------------

class MultPopApp(App):
    def build(self):
        Clock.schedule_interval(lambda dt: self.update_time(), 1)
        return MainScreen()

    def update_time(self):
        if self.root.sw_switch == 'true':
            self.root.process_1()

        if self.root.sw_switch == 'over':
            x = SecondPopup()
            x.popup2_but()
            self.root.sw_switch = 'none'


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

你知道吗multpop.kv电压你知道吗

#: kivy 1.11.0

<MainScreen>:
    orientation: 'vertical'
    Button:
        text: 'Options'
        on_release: root.setName(*args)

<FirstPopup>:
    title: 'Options Window'
    size_hint: None, None
    size: 400,370
    BoxLayout:
        orientation : 'vertical'
        Label:  
            text : "Checkbox options listed here"
        Button:
            text: "OK"
            on_press: 
                app.root.setName1(*args)
                app.root.sw_switch = 'true'
                root.popup1_but()


<SecondPopup>:
    title: 'Please wait..'
    size_hint: None, None
    size: 400,370
    BoxLayout:
        orientation : 'vertical'
        size_hint: 1.0,1.0

        Label:  
            text : "Process Initiated"
            size_hint: 1.0, 1.0

现在我需要这第二个弹出自动关闭后,一个后台函数启动了这第二个弹出。这段代码中的解决方案是我在尝试了其他一些东西之后得到的。非常感谢您的指导。非常感谢。你知道吗

python和kivy文件都已发布


Tags: textfromimportselfsizetimedefargs
1条回答
网友
1楼 · 发布于 2024-09-30 03:26:02

第一个弹出窗口-^{}

默认情况下,auto_dismiss设置为True,即弹出窗口外的任何单击都将取消/关闭弹出窗口。如果您不想这样,可以将auto_dismiss设置为False。你知道吗

代码段-kv

<FirstPopup>:
    title: 'Options Window'
    auto_dismiss: False

第二个弹出窗口-auto_dismiss: True

Now I need this second popup to auto dismiss after a background function initiated alongside this second pop.

解决方案

为了能够访问第二个弹出窗口小部件,请执行以下操作

  • 添加导入语句from kivy.properties import ObjectProperty
  • 声明类型为^{}的类属性popup2,并初始化为None
  • SecondPopup()的实例化赋给self.popup2
  • 使用self.popup2.open()打开弹出窗口
  • 您可以使用self.popup2.dismiss()自动取消SecondPopup()。在后台函数启动、完成或使用Clock计划回调时使用此选项。你知道吗

片段-py

from kivy.properties import ObjectProperty


class MainScreen(BoxLayout):
    popup2 = ObjectProperty(None)

    ...

    def setName1(self, *args):
        self.sw_switch = 'true1'
        self.popup2 = SecondPopup()
        self.popup2.open()
        Clock.schedule_once(lambda dt: self.dismiss_popup2(), 5)

    def dismiss_popup2(self):
        print(f"\ndismiss_popup2: SecondPopup dismissed")
        self.popup2.dismiss()

Kivy App - Avoid sleep() or infinite loops

In Kivy applications, you have to avoid long/infinite loops or sleeping.

while True:
    animate_something()
    time.sleep(.10)

When you run this, the program will never exit your loop, preventing Kivy from doing all of the other things that need doing. As a result, all you’ll see is a black window which you won’t be able to interact with. Instead, you need to “schedule” your animate_something() function to be called repeatedly.

相关问题 更多 >

    热门问题