简体   繁体   中英

I want to call a Kivy popup window in specific condition in python file

I have a function that will display a main popup, and when the user close the popup by tapping any where another popup will appear depending on the passed value as a parameter for the function, the problem is the style of popup content, when I use .kv file is looks perfect but, when I try to create the same style using .py file there is so many problems appear to me, so there is any way to call the popup window from .kv file and use it in .py file?

My function in .py file:

def show_popup(result):
    def show_second_popup(self, *args):
        if result == True:
            # Show popup 1 

        elif result == False:
            # Show popup 2
        

        # Show the main popup
        str = Label(markup= True, text_size=(190, 160), text= "This is the main popup" , 
                 halign="center", valign="center", color= (0,0,0,1),font_size= 24)
        popupWindow = Popup( 
                        auto_dismiss= True,
                        content= str, 
                        size_hint= (0.8,0.4), 
                        pos_hint= {'x': 0.15,'y': 0.3}, 
                        title= "", separator_height= 0
                        )
        
        popupWindow.open()

        popupWindow.bind(on_dismiss= show_second_popup)

The popup style in .kv file:

<PopupTrue@Popup>
    auto_dismiss: False
    title: ""
    separator_height: 0
    size_hint: 0.8, 0.6
    background_color: (0,0,0,0)
    background_normal: ''

    


    BoxLayout:
        orientation: "vertical"
        size: root.width, root.height
        padding: 0, 10, 10, 10
        border: 50
        border_color: (1,1,1,1)

        canvas.before:
            Color:
                rgba:(255/255,255/255,255/255,1)

            RoundedRectangle:
                pos: self.x - 20, self.y - 10
                size: self.width + 40, self.height - 60
                radius: [40]   

        Image:
            source: 'icon.png'



        Label: 
            text: "This popup appear if the condition was TRUE"
            color: 0,0,0,1
            font_size: 25
            

            
        CloseButton:
            text: "Close"
            color: 0,0,0,1
            size_hint: (None , None)
            width: 105
            height: 40
            pos_hint: {'center_x':0.5}
            on_release: root.dismiss() 



<PopupFalse@Popup>
    auto_dismiss: False
    title: ""
    separator_height: 0
    size_hint: 0.8, 0.6
    background_color: (0,0,0,0)
    background_normal: ''

    


    BoxLayout:
        orientation: "vertical"
        size: root.width, root.height
        padding: 0, 10, 10, 10
        border: 50
        border_color: (1,1,1,1)

        canvas.before:
            Color:
                rgba:(255/255,255/255,255/255,1)

            RoundedRectangle:
                pos: self.x - 20, self.y - 10
                size: self.width + 40, self.height - 60
                radius: [40]   

        Image:
            source: 'icon.png'



        Label: 
            text: "This popup appear if the condition was False"
            color: 0,0,0,1
            font_size: 25
            

            
        CloseButton:
            text: "Close"
            color: 0,0,0,1
            size_hint: (None , None)
            width: 105
            height: 40
            pos_hint: {'center_x':0.5}
            on_release: root.dismiss() 

I want the main popup appear from python file, and call another popup from.kv file, because my.kv file popup has canvas for the button and popup layout, and I faced a problem with writing the canvas part.

Ok I find the answer, I tried to create a two classes with a Popup parameter, and I named the classes as the name in the.kv file, finally I just call the class with open() function. :)

class PopupTrue(Popup):
      pass 
            
class PopupFalse(Popup):
      pass 
        
def show_popup(result):
     def show_second_popup(self, *args):
          if result == True:
              # Show popup 1 
              PopupTrue().open()
        
          elif result == False:
              # Show popup 2
              PopupFalse().open()
    



     

  # Show the main popup
    str = Label(markup= True, text_size=(190, 160), text= "This is the main popup" , 
                 halign="center", valign="center", color= (0,0,0,1),font_size= 24)
    popupWindow = Popup( 
                    auto_dismiss= True,
                    content= str, 
                    size_hint= (0.8,0.4), 
                    pos_hint= {'x': 0.15,'y': 0.3}, 
                    title= "", separator_height= 0
                    )
    
    popupWindow.open()

    popupWindow.bind(on_dismiss= show_second_popup)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM