Merge pull request #380 from MikeTheWatchGuy/Dev-latest

New window methods - Hide, UnHide!  Fix for menu letter underlining, …
This commit is contained in:
MikeTheWatchGuy 2018-09-29 02:03:42 -04:00 committed by GitHub
commit 3e676c94c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 96 additions and 91 deletions

View File

@ -2353,6 +2353,12 @@ class Window:
def Enable(self): def Enable(self):
self.TKroot.grab_release() self.TKroot.grab_release()
def Hide(self):
self.TKroot.withdraw()
def UnHide(self):
self.TKroot.deiconify()
def __enter__(self): def __enter__(self):
return self return self
@ -2760,18 +2766,14 @@ def AddMenuItem(top_menu, sub_menu_info, element, is_sub_menu=False, skip=False)
if type(sub_menu_info) is str: if type(sub_menu_info) is str:
if not is_sub_menu and not skip: if not is_sub_menu and not skip:
# print(f'Adding command {sub_menu_info}') # print(f'Adding command {sub_menu_info}')
pos = sub_menu_info.find('_&') pos = sub_menu_info.find('&')
if pos != -1: if pos != -1:
_ = sub_menu_info[:pos] if pos == 0 or sub_menu_info[pos-1] != "\\":
try: sub_menu_info = sub_menu_info[:pos] + sub_menu_info[pos+1:]
_ += sub_menu_info[pos+2:]
except Exception as e:
print(e)
sub_menu_info = _
if sub_menu_info == '---': if sub_menu_info == '---':
top_menu.add('separator') top_menu.add('separator')
else: else:
top_menu.add_command(label=sub_menu_info, underline=pos-1, command=lambda: Menu.MenuItemChosenCallback(element, sub_menu_info)) top_menu.add_command(label=sub_menu_info, underline=pos, command=lambda: Menu.MenuItemChosenCallback(element, sub_menu_info))
else: else:
i = 0 i = 0
while i < (len(sub_menu_info)): while i < (len(sub_menu_info)):
@ -3246,17 +3248,14 @@ def PackFormIntoFrame(form, containing_frame, toplevel_form):
element.TKMenu = tk.Menu(toplevel_form.TKroot, tearoff=element.Tearoff) # create the menubar element.TKMenu = tk.Menu(toplevel_form.TKroot, tearoff=element.Tearoff) # create the menubar
menubar = element.TKMenu menubar = element.TKMenu
for menu_entry in menu_def: for menu_entry in menu_def:
# print(f'Adding a Menubar ENTRY') # print(f'Adding a Menubar ENTRY {menu_entry}')
baritem = tk.Menu(menubar, tearoff=element.Tearoff) baritem = tk.Menu(menubar, tearoff=element.Tearoff)
pos = menu_entry[0].find('_&') pos = menu_entry[0].find('&')
# print(pos)
if pos != -1: if pos != -1:
_ = menu_entry[0][:pos] if pos == 0 or menu_entry[0][pos-1] != "\\":
try: menu_entry[0] = menu_entry[0][:pos] + menu_entry[0][pos+1:]
_ += menu_entry[0][pos+2:] menubar.add_cascade(label=menu_entry[0], menu=baritem, underline = pos)
except:
pass
menu_entry[0] = _
menubar.add_cascade(label=menu_entry[0], menu=baritem, underline = pos-1)
if len(menu_entry) > 1: if len(menu_entry) > 1:
AddMenuItem(baritem, menu_entry[1], element) AddMenuItem(baritem, menu_entry[1], element)
toplevel_form.TKroot.configure(menu=element.TKMenu) toplevel_form.TKroot.configure(menu=element.TKMenu)
@ -3947,18 +3946,20 @@ def PopupGetFolder(message, default_path='', no_window=False, size=(None,None),
root.destroy() root.destroy()
return folder_name return folder_name
with Window(title=message, icon=icon, auto_size_text=True, button_color=button_color, background_color=background_color, layout = [[Text(message, auto_size_text=True, text_color=text_color, background_color=background_color)],
font=font, no_titlebar=no_titlebar, grab_anywhere=grab_anywhere, keep_on_top=keep_on_top, location=location) as form: [InputText(default_text=default_path, size=size), FolderBrowse()],
layout = [[Text(message, auto_size_text=True, text_color=text_color, background_color=background_color)], [Ok(), Cancel()]]
[InputText(default_text=default_path, size=size), FolderBrowse()],
[Ok(), Cancel()]]
(button, input_values) = form.LayoutAndRead(layout) window = Window(title=message, icon=icon, auto_size_text=True, button_color=button_color, background_color=background_color,
if button != 'Ok': font=font, no_titlebar=no_titlebar, grab_anywhere=grab_anywhere, keep_on_top=keep_on_top, location=location)
return None
else: (button, input_values) = window.LayoutAndRead(layout)
path = input_values[0]
return path if button != 'Ok':
return None
else:
path = input_values[0]
return path
##################################### #####################################
# PopupGetFile # # PopupGetFile #
@ -3999,18 +4000,19 @@ def PopupGetFile(message, default_path='', default_extension='', save_as=False,
browse_button = SaveAs(file_types=file_types) if save_as else FileBrowse(file_types=file_types) browse_button = SaveAs(file_types=file_types) if save_as else FileBrowse(file_types=file_types)
with Window(title=message, icon=icon, auto_size_text=True, button_color=button_color, font=font, background_color=background_color, layout = [[Text(message, auto_size_text=True, text_color=text_color, background_color=background_color)],
no_titlebar=no_titlebar, grab_anywhere=grab_anywhere, keep_on_top=keep_on_top, location=location) as form: [InputText(default_text=default_path, size=size), browse_button],
layout = [[Text(message, auto_size_text=True, text_color=text_color, background_color=background_color)], [Ok(), Cancel()]]
[InputText(default_text=default_path, size=size), browse_button],
[Ok(), Cancel()]]
(button, input_values) = form.LayoutAndRead(layout) window = Window(title=message, icon=icon, auto_size_text=True, button_color=button_color, font=font, background_color=background_color,
if button != 'Ok': no_titlebar=no_titlebar, grab_anywhere=grab_anywhere, keep_on_top=keep_on_top, location=location)
return None
else: (button, input_values) = window.Layout(layout).Read()
path = input_values[0] if button != 'Ok':
return path return None
else:
path = input_values[0]
return path
##################################### #####################################
# PopupGetText # # PopupGetText #
@ -4033,17 +4035,20 @@ def PopupGetText(message, default_text='', password_char='', size=(None,None), b
:param location: :param location:
:return: Text entered or None if window was closed :return: Text entered or None if window was closed
""" """
with Window(title=message, icon=icon, auto_size_text=True, button_color=button_color, no_titlebar=no_titlebar,
background_color=background_color, grab_anywhere=grab_anywhere, keep_on_top=keep_on_top, location=location) as form:
layout = [[Text(message, auto_size_text=True, text_color=text_color, background_color=background_color, font=font)],
[InputText(default_text=default_text, size=size, password_char=password_char)],
[Ok(), Cancel()]]
(button, input_values) = form.LayoutAndRead(layout) layout = [[Text(message, auto_size_text=True, text_color=text_color, background_color=background_color, font=font)],
if button != 'Ok': [InputText(default_text=default_text, size=size, password_char=password_char)],
return None [Ok(), Cancel()]]
else:
return input_values[0] window = Window(title=message, icon=icon, auto_size_text=True, button_color=button_color, no_titlebar=no_titlebar,
background_color=background_color, grab_anywhere=grab_anywhere, keep_on_top=keep_on_top, location=location)
(button, input_values) = window.Layout(layout).Read()
if button != 'Ok':
return None
else:
return input_values[0]
# ============================== SetGlobalIcon ======# # ============================== SetGlobalIcon ======#
@ -4389,50 +4394,50 @@ def Popup(*args, button_color=None, background_color=None, text_color=None, butt
else: else:
local_line_width = MESSAGE_BOX_LINE_WIDTH local_line_width = MESSAGE_BOX_LINE_WIDTH
title = args_to_print[0] if args_to_print[0] is not None else 'None' title = args_to_print[0] if args_to_print[0] is not None else 'None'
with Window(title, auto_size_text=True, background_color=background_color, button_color=button_color, auto_close=auto_close, auto_close_duration=auto_close_duration, icon=icon, font=font, no_titlebar=no_titlebar, grab_anywhere=grab_anywhere, keep_on_top=keep_on_top, location=location) as form: form = Window(title, auto_size_text=True, background_color=background_color, button_color=button_color, auto_close=auto_close, auto_close_duration=auto_close_duration, icon=icon, font=font, no_titlebar=no_titlebar, grab_anywhere=grab_anywhere, keep_on_top=keep_on_top, location=location)
max_line_total, total_lines = 0,0 max_line_total, total_lines = 0,0
for message in args_to_print: for message in args_to_print:
# fancy code to check if string and convert if not is not need. Just always convert to string :-) # fancy code to check if string and convert if not is not need. Just always convert to string :-)
# if not isinstance(message, str): message = str(message) # if not isinstance(message, str): message = str(message)
message = str(message) message = str(message)
if message.count('\n'): if message.count('\n'):
message_wrapped = message message_wrapped = message
else: else:
message_wrapped = textwrap.fill(message, local_line_width) message_wrapped = textwrap.fill(message, local_line_width)
message_wrapped_lines = message_wrapped.count('\n')+1 message_wrapped_lines = message_wrapped.count('\n')+1
longest_line_len = max([len(l) for l in message.split('\n')]) longest_line_len = max([len(l) for l in message.split('\n')])
width_used = min(longest_line_len, local_line_width) width_used = min(longest_line_len, local_line_width)
max_line_total = max(max_line_total, width_used) max_line_total = max(max_line_total, width_used)
# height = _GetNumLinesNeeded(message, width_used) # height = _GetNumLinesNeeded(message, width_used)
height = message_wrapped_lines height = message_wrapped_lines
form.AddRow(Text(message_wrapped, auto_size_text=True, text_color=text_color, background_color=background_color)) form.AddRow(Text(message_wrapped, auto_size_text=True, text_color=text_color, background_color=background_color))
total_lines += height total_lines += height
pad = max_line_total-15 if max_line_total > 15 else 1 pad = max_line_total-15 if max_line_total > 15 else 1
pad =1 pad =1
if non_blocking: if non_blocking:
PopupButton = DummyButton PopupButton = DummyButton
else: else:
PopupButton = SimpleButton PopupButton = SimpleButton
# show either an OK or Yes/No depending on paramater # show either an OK or Yes/No depending on paramater
if button_type is POPUP_BUTTONS_YES_NO: if button_type is POPUP_BUTTONS_YES_NO:
form.AddRow(Text('', size=(pad, 1), auto_size_text=False, text_color=text_color, background_color=background_color), PopupButton('Yes', button_color=button_color, focus=True, bind_return_key=True), PopupButton('No', button_color=button_color)) form.AddRow(Text('', size=(pad, 1), auto_size_text=False, text_color=text_color, background_color=background_color), PopupButton('Yes', button_color=button_color, focus=True, bind_return_key=True), PopupButton('No', button_color=button_color))
elif button_type is POPUP_BUTTONS_CANCELLED: elif button_type is POPUP_BUTTONS_CANCELLED:
form.AddRow(Text('', size=(pad, 1), auto_size_text=False, text_color=text_color, background_color=background_color), PopupButton('Cancelled', button_color=button_color, focus=True, bind_return_key=True)) form.AddRow(Text('', size=(pad, 1), auto_size_text=False, text_color=text_color, background_color=background_color), PopupButton('Cancelled', button_color=button_color, focus=True, bind_return_key=True))
elif button_type is POPUP_BUTTONS_ERROR: elif button_type is POPUP_BUTTONS_ERROR:
form.AddRow(Text('', size=(pad, 1), auto_size_text=False, text_color=text_color, background_color=background_color), PopupButton('Error', size=(6, 1), button_color=button_color, focus=True, bind_return_key=True)) form.AddRow(Text('', size=(pad, 1), auto_size_text=False, text_color=text_color, background_color=background_color), PopupButton('Error', size=(6, 1), button_color=button_color, focus=True, bind_return_key=True))
elif button_type is POPUP_BUTTONS_OK_CANCEL: elif button_type is POPUP_BUTTONS_OK_CANCEL:
form.AddRow(Text('', size=(pad, 1), auto_size_text=False, text_color=text_color, background_color=background_color), PopupButton('OK', size=(5, 1), button_color=button_color, focus=True, bind_return_key=True), form.AddRow(Text('', size=(pad, 1), auto_size_text=False, text_color=text_color, background_color=background_color), PopupButton('OK', size=(5, 1), button_color=button_color, focus=True, bind_return_key=True),
PopupButton('Cancel', size=(5, 1), button_color=button_color)) PopupButton('Cancel', size=(5, 1), button_color=button_color))
elif button_type is POPUP_BUTTONS_NO_BUTTONS: elif button_type is POPUP_BUTTONS_NO_BUTTONS:
pass pass
else: else:
form.AddRow(Text('', size=(pad, 1), auto_size_text=False, background_color=background_color), PopupButton('OK', size=(5, 1), button_color=button_color, focus=True, bind_return_key=True)) form.AddRow(Text('', size=(pad, 1), auto_size_text=False, background_color=background_color), PopupButton('OK', size=(5, 1), button_color=button_color, focus=True, bind_return_key=True))
if non_blocking: if non_blocking:
button, values = form.ReadNonBlocking() button, values = form.ReadNonBlocking()
else: else:
button, values = form.Show() button, values = form.Show()
return button return button