似乎应该有一个内置的小部件。然而,基于 enaml 附带的 FileDialog 示例,这里有一些可行的方法,并且该模式很容易扩展。
from enaml.layout.api import vbox, align
from enaml.widgets.api import Window, Container, Label, PushButton
enamldef Main(Window): main_win:
title = 'Main'
attr dlg_result : str = 'waiting'
Container:
constraints = [
vbox(pb, lbl),
align('h_center', lbl, pb),
]
Label: lbl:
align = 'center'
text << main_win.dlg_result
PushButton: pb:
text = 'Dialog'
clicked ::
session.add_window( TheDialog(listener=main_win,result='dlg_result') )
enamldef TheDialog(Window): dlg_win:
title = 'Dialog'
modality = 'application_modal' # one of ['non_modal', 'window_modal', 'application_modal']
attr listener
attr result
Container:
constraints = []
PushButton: ok_btn:
text = 'Okay'
clicked ::
setattr(listener, result, 'Okay')
dlg_win.close()
PushButton: cancel_btn:
text = 'Cancel'
clicked ::
setattr(listener, result, 'Cancel')
dlg_win.close()