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?
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')