1
$\begingroup$

I am designing a simple popup msg box having two buttons, OK and Cancel, as shown below using python. I am using invoke_props_dialog which comes with an OK button. I have added a Close button myself using an operator, but I have no idea how to actually close the msg on click of this cancel button. Can someone please help me?

Simple Popup Msg

The code I have used is given below. You can copy-paste this code in a text editor and execute it immediately to see this msg box.

P.S.: I could not find any similar question on this site. The closest I see is this question, but it uses invoke_popup instead of invoke_props_dialog (the text in the question is actually wrong). While invoke_popup has a close-down behavior, and based on that a mouse-driven solution was suggested, invoke_props_dialog does not have this. Also, that mouse-driven solution is hacky in nature and I am looking for something better. That solution does not even work here, we have to simulate a mouse-click event in addition to mouse-move. That will be a very bad design and I want to avoid that.

import bpy
from bpy.types import Operator, UILayout

class WM_OT_Show_Popup(bpy.types.Operator):
    bl_idname = "wm.show_popup"
    bl_label = ""
    bl_options = {'REGISTER', 'INTERNAL'}
    
    def draw(self, context):

        layout = self.layout
        
        row = layout.row(align=True)
        row.alignment = 'CENTER'
        row.label(text="The cancel button does not close this msg.")

        layout.separator()
        row = layout.row(align=True)
        row.alignment = 'CENTER'
        row.scale_x = 4
        row.operator(Object_OT_Close_Popup.bl_idname, text="Cancel")

    def invoke(self, context, event):

        return_code = context.window_manager.invoke_props_dialog(self, width = 300)
        return return_code

    def execute(self, context):
        
        # The main functionality will go here.
        
        # Tell Blender that the operator finished successfully.
        return {'FINISHED'}

# This operator is used to close the msg.
class Object_OT_Close_Popup(Operator):
    bl_idname = "object.close_popup"
    bl_label = ""
    bl_options = {'INTERNAL'}

    def execute(self, context):        

        # I need help to code right here, how do we close the popup msg box?
        
        # Tell Blender that the operator was cancelled since no action is taken.
        return {'CANCELLED'}

def register():
    bpy.utils.register_class(WM_OT_Show_Popup)
    bpy.utils.register_class(Object_OT_Close_Popup)

def unregister():
    bpy.utils.unregister_class(Object_OT_Close_Popup)
    bpy.utils.unregister_class(WM_OT_Show_Popup)

if __name__ == "__main__":
    register()
    
bpy.ops.wm.show_popup('INVOKE_DEFAULT')
$\endgroup$

1 Answer 1

0
$\begingroup$

here is a link to a similar question/original answer using just a popup instead of a dialog popup. it is still a workaround since I, and as it looks like others also have not found the proper way to close popups, or it might not exist. Link to original answer by bonjorno7

basically, you can incorporate that workaround code in the invoke function of the cancel operator call and it will close the popup panel. with popup dialog it is not enough to just move the mouse outside the popup so either you modify the code to do some action that forces the dialog popup to close or you can just use popup if it suits you.

$\endgroup$
1
  • $\begingroup$ Thank you so much for the suggestions. I cannot use a simple popup because it gets closed when the user moves his mouse outside the popup. There is a very high possibility that it won't even get displayed if the user is moving his mouse (even slight vibrations also count here) when the popup was just about to open. This is almost useless. One possible fix is to artificially move the mouse inside the popup, but it feels very odd. For the other workaround you suggested, triggering a click action on the mouse outside the popup can have fatal effects if there is some UI control or an object there. $\endgroup$
    – Jeet GT
    Commented Sep 20, 2023 at 8:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .