def messagebox(msg, mode=0): """ Draw a `Message Box` with :attr:`msg` in a specified type defined by :attr:`mode`. .. note:: This is a `static` method. :param msg: The information to be displayed in message box. :type msg: str :param mode: A flag indicating the type of message box to be displayed. The default value of `mode` is `0`. ==== =========================================================== mode Message Box Type ==== =========================================================== 0 A simple message box showing a message without any buttons. 1 A message box with an `OK` button for user to confirm. 2 A message box with `OK` and `Cancel` buttons for user to choose. ==== =========================================================== :type mode: int :return: A flag indicating the choice made by user. ====== ===================================================== Return Condition ====== ===================================================== 0 #. No button is pressed while :attr:`mode` is `0`. #. `OK` button pressed while :attr:`mode` is `1`. #. `Cancel` button pressed while :attr:`mode` is `2`. 1 `OK` button pressed while :attr:`mode` is `2`. ====== ===================================================== :rtype: int """ pos_x = 24 if mode == 0 else 20 pos_y = 10 width = 30 if mode == 0 else 40 height = 2 messages = CommonUtil.cut_message(msg, width - 4) height += len(messages) if mode: height += 2 # Draw Shadow shadow = curses.newwin(height, width, pos_y + 1, pos_x + 1) shadow.bkgd(' ', curses.color_pair(8)) shadow.refresh() # Draw Subwindow screen = curses.newwin(height, width, pos_y, pos_x) screen.box() screen.bkgd(' ', curses.color_pair(2)) screen.keypad(1) # Set local variable normal = curses.A_NORMAL select = curses.A_REVERSE # Insert messages for i in range(len(messages)): screen.addstr(1 + i, 2, messages[i].center(width - 4), normal) if mode == 0: screen.refresh() else: # Draw subwindow frame line_height = 1 + len(messages) screen.hline(line_height, 1, curses.ACS_HLINE, width - 2) screen.addch(line_height, 0, curses.ACS_SSSB) screen.addch(line_height, width - 1, curses.ACS_SBSS) tab = 0 key_in = None while key_in != 27: if mode == 1: choices = ["OK"] elif mode == 2: choices = ["OK", "Cancel"] else: return 0 for i, item in enumerate(choices): item_str = ''.join(['[', item, ']']) tab_pos_x = 6 + 20 * i if mode == 2 else 18 screen.addstr(line_height + 1, tab_pos_x, item_str, select if i == tab else normal) screen.refresh() key_in = screen.getch() if mode == 2: # OK or Cancel if key_in in [9, curses.KEY_LEFT, curses.KEY_RIGHT]: tab = [1, 0][tab] if key_in in [ord('a'), ord('c')]: key_in -= (ord('a') - ord('A')) if key_in in [ord('C'), ord('O')]: return [ord('C'), ord('O')].index(key_in) if key_in in [10, 32]: return not tab return 0