Table Element Update - added new num_rows parm. Removed the _my_windows global variable. Using static class variables instead

This commit is contained in:
MikeTheWatchGuy 2018-12-08 15:01:29 -05:00
parent d7f1266671
commit c8b4bec9bf
1 changed files with 73 additions and 55 deletions

View File

@ -187,25 +187,6 @@ MENU_DISABLED_CHARACTER = '!'
MENU_KEY_SEPARATOR = '::'
# a shameful global variable. This represents the top-level window information. Needed because opening a second window is different than opening the first.
class MyWindows():
def __init__(self):
self.NumOpenWindows = 0
self.user_defined_icon = None
self.hidden_master_root = None
def Decrement(self):
self.NumOpenWindows -= 1 * (self.NumOpenWindows != 0) # decrement if not 0
# print('---- DECREMENTING Num Open Windows = {} ---'.format(self.NumOpenWindows))
def Increment(self):
self.NumOpenWindows += 1
# print('++++ INCREMENTING Num Open Windows = {} ++++'.format(self.NumOpenWindows))
_my_windows = MyWindows() # terrible hack using globals... means need a class for collecing windows
# ====================================================================== #
# One-liner functions that are handy as f_ck #
# ====================================================================== #
@ -1408,7 +1389,7 @@ class Button(Element):
# ------- Button Callback ------- #
def ButtonCallBack(self):
global _my_windows
# global _my_windows
# print('Button callback')
@ -1494,7 +1475,8 @@ class Button(Element):
self.ParentForm.TKroot.quit()
if self.ParentForm.NonBlocking:
self.ParentForm.TKroot.destroy()
_my_windows.Decrement()
# _my_windows.Decrement()
Window.DecrementOpenCount()
elif self.BType == BUTTON_TYPE_READ_FORM: # LEAVE THE WINDOW OPEN!! DO NOT CLOSE
# first, get the results table built
# modify the Results table in the parent FlexForm object
@ -1509,7 +1491,8 @@ class Button(Element):
self.ParentForm._Close()
if self.ParentForm.NonBlocking:
self.ParentForm.TKroot.destroy()
_my_windows.Decrement()
Window.DecrementOpenCount()
# _my_windows.Decrement()
elif self.BType == BUTTON_TYPE_CALENDAR_CHOOSER: # this is a return type button so GET RESULTS and destroy window
should_submit_window = False
root = tk.Toplevel()
@ -1613,7 +1596,8 @@ class ProgressBar(Element):
try:
self.ParentForm.TKroot.update()
except:
_my_windows.Decrement()
Window.DecrementOpenCount()
# _my_windows.Decrement()
return False
return True
@ -2792,7 +2776,7 @@ class Table(Element):
size=size, pad=pad, key=key, tooltip=tooltip, visible=visible)
return
def Update(self, values=None, visible=None):
def Update(self, values=None, num_rows=None, visible=None):
if values is not None:
children = self.TKTreeview.get_children()
for i in children:
@ -2812,6 +2796,8 @@ class Table(Element):
self.TKTreeview.pack_forget()
elif visible is True:
self.TKTreeview.pack()
if num_rows is not None:
self.TKTreeview.config(height=num_rows)
def treeview_selected(self, event):
@ -3013,6 +2999,9 @@ Stretch = ErrorElement
# Window CLASS #
# ------------------------------------------------------------------------- #
class Window:
NumOpenWindows = 0
user_defined_icon = None
hidden_master_root = None
def __init__(self, title, default_element_size=DEFAULT_ELEMENT_SIZE, default_button_element_size=(None, None),
auto_size_text=None, auto_size_buttons=None, location=(None, None), size=(None, None), element_padding=None, button_color=None, font=None,
@ -3062,7 +3051,7 @@ class Window:
self.Font = font if font else DEFAULT_FONT
self.RadioDict = {}
self.BorderDepth = border_depth
self.WindowIcon = icon if icon is not None else _my_windows.user_defined_icon
self.WindowIcon = icon if icon is not None else Window.user_defined_icon
self.AutoClose = auto_close
self.NonBlocking = False
self.TKroot = None
@ -3100,6 +3089,15 @@ class Window:
self.XFound = False
self.ElementPadding = element_padding or DEFAULT_ELEMENT_PADDING
@staticmethod
def IncrementOpenCount():
Window.NumOpenWindows += 1
# print('+++++ INCREMENTING Num Open Windows = {} ---'.format(Window.NumOpenWindows))
@staticmethod
def DecrementOpenCount():
Window.NumOpenWindows -= 1 * (Window.NumOpenWindows != 0) # decrement if not 0
# print('----- DECREMENTING Num Open Windows = {} ---'.format(Window.NumOpenWindows))
# ------------------------- Add ONE Row to Form ------------------------- #
def AddRow(self, *args):
@ -3246,7 +3244,8 @@ class Window:
rc = self.TKroot.update()
except:
self.TKrootDestroyed = True
_my_windows.Decrement()
Window.DecrementOpenCount()
# _my_windows.Decrement()
# print('ROOT Destroyed')
results = BuildResults(self, False, self)
if results[0] != None and results[0] != timeout_key:
@ -3286,12 +3285,14 @@ class Window:
if self.RootNeedsDestroying:
# print('*** DESTROYING LATE ***')
self.TKroot.destroy()
_my_windows.Decrement()
Window.DecrementOpenCount()
# _my_windows.Decrement()
self.LastButtonClicked = None
return None, None
# if form was closed with X
if self.LastButtonClicked is None and self.LastKeyboardEvent is None and self.ReturnValues[0] is None:
_my_windows.Decrement()
Window.DecrementOpenCount()
# _my_windows.Decrement()
# Determine return values
if self.LastKeyboardEvent is not None or self.LastButtonClicked is not None:
results = BuildResults(self, False, self)
@ -3322,13 +3323,15 @@ class Window:
rc = self.TKroot.update()
except:
self.TKrootDestroyed = True
_my_windows.Decrement()
Window.DecrementOpenCount()
# _my_windows.Decrement()
# print("read failed")
# return None, None
if self.RootNeedsDestroying:
# print('*** DESTROYING LATE ***', self.ReturnValues)
self.TKroot.destroy()
_my_windows.Decrement()
Window.DecrementOpenCount()
# _my_windows.Decrement()
self.Values = None
self.LastButtonClicked = None
return None, None
@ -3343,7 +3346,8 @@ class Window:
rc = self.TKroot.update()
except:
self.TKrootDestroyed = True
_my_windows.Decrement()
Window.DecrementOpenCount()
# _my_windows.Decrement()
# return None, None
return self
@ -3473,7 +3477,8 @@ class Window:
return
try:
self.TKroot.destroy()
_my_windows.Decrement()
Window.DecrementOpenCount()
# _my_windows.Decrement()
except:
pass
@ -3482,7 +3487,7 @@ class Window:
# IT FINALLY WORKED! 29-Oct-2018 was the first time this damned thing got called
def OnClosingCallback(self):
global _my_windows
# global _my_windows
# print('Got closing callback', self.DisableClose)
if self.DisableClose:
return
@ -5217,21 +5222,28 @@ def ConvertFlexToTK(MyFlexForm):
# ----====----====----====----====----==== STARTUP TK ====----====----====----====----====----#
def StartupTK(my_flex_form):
global _my_windows
ow = _my_windows.NumOpenWindows
# global _my_windows
# ow = _my_windows.NumOpenWindows
ow = Window.NumOpenWindows
print(ow)
# print('Starting TK open Windows = {}'.format(ow))
if not ow and not my_flex_form.ForceTopLevel:
# if first window being created, make a throwaway, hidden master root. This stops one user
# window from becoming the child of another user window. All windows are children of this
# hidden window
_my_windows.Increment()
_my_windows.hidden_master_root = tk.Tk()
_my_windows.hidden_master_root.attributes('-alpha', 0) # HIDE this window really really really good
_my_windows.hidden_master_root.wm_overrideredirect(True) # damn, what did this do again?
_my_windows.hidden_master_root.withdraw() # no, REALLY hide it
# root = tk.Tk() # users windows are no longer using tk.Tk. They are all Toplevel windows
print("******")
Window.IncrementOpenCount()
Window.hidden_master_root = tk.Tk()
Window.hidden_master_root.attributes('-alpha', 0) # HIDE this window really really really
Window.hidden_master_root.wm_overrideredirect(True)
Window.hidden_master_root.withdraw()
# good
# _my_windows.Increment()
# _my_windows.hidden_master_root = tk.Tk()
# _my_windows.hidden_master_root.attributes('-alpha', 0) # HIDE this window really really really good
# _my_windows.hidden_master_root.wm_overrideredirect(True) # damn, what did this do again?
# _my_windows.hidden_master_root.withdraw() # no, REALLY hide it
root = tk.Toplevel()
else:
root = tk.Toplevel()
@ -5240,10 +5252,10 @@ def StartupTK(my_flex_form):
root.attributes('-alpha', 0) # hide window while building it. makes for smoother 'paint'
except:
pass
# root.wm_overrideredirect(True)
if my_flex_form.BackgroundColor is not None and my_flex_form.BackgroundColor != COLOR_SYSTEM_DEFAULT:
root.configure(background=my_flex_form.BackgroundColor)
_my_windows.Increment()
Window.IncrementOpenCount()
# _my_windows.Increment()
my_flex_form.TKroot = root
# Make moveable window
@ -5296,7 +5308,8 @@ def StartupTK(my_flex_form):
my_flex_form.TimerCancelled = True
# print('..... BACK from MainLoop')
if not my_flex_form.FormRemainedOpen:
_my_windows.Decrement()
Window.DecrementOpenCount()
# _my_windows.Decrement()
if my_flex_form.RootNeedsDestroying:
my_flex_form.TKroot.destroy()
my_flex_form.RootNeedsDestroying = False
@ -5392,7 +5405,7 @@ def _ProgressMeterUpdate(bar, value, text_elem, *args):
:param value: int
:return: True if not cancelled, OK....False if Error
'''
global _my_windows
# global _my_windows
if bar == None: return False
if bar.BarExpired: return False
message, w, h = ConvertArgsToSingleString(*args)
@ -5404,7 +5417,8 @@ def _ProgressMeterUpdate(bar, value, text_elem, *args):
bar.BarExpired = True
bar.ParentForm._Close()
if rc: # if update was OK but bar expired, decrement num windows
_my_windows.Decrement()
# _my_windows.Decrement()
Window.DecrementOpenCount()
if bar.ParentForm.RootNeedsDestroying:
try:
bar.ParentForm.TKroot.destroy()
@ -5739,14 +5753,15 @@ ScrolledTextBox = PopupScrolled
# Sets the icon to be used by default #
# ===================================================#
def SetGlobalIcon(icon):
global _my_windows
# global _my_windows
try:
with open(icon, 'r') as icon_file:
pass
except:
raise FileNotFoundError
_my_windows.user_defined_icon = icon
# _my_windows.user_defined_icon = icon
Window.user_defined_icon = icon
return True
@ -5796,7 +5811,7 @@ def SetOptions(icon=None, button_color=None, element_size=(None, None), button_e
global DEFAULT_INPUT_TEXT_COLOR
global DEFAULT_TOOLTIP_TIME
global DEFAULT_ERROR_BUTTON_COLOR
global _my_windows
# global _my_windows
if icon:
try:
@ -5804,7 +5819,8 @@ def SetOptions(icon=None, button_color=None, element_size=(None, None), button_e
pass
except:
raise FileNotFoundError
_my_windows.user_defined_icon = icon
Window.user_defined_icon = icon
# _my_windows.user_defined_icon = icon
if button_color != None:
DEFAULT_BUTTON_COLOR = button_color
@ -6715,10 +6731,11 @@ def PopupGetFolder(message, title=None, default_path='', no_window=False, size=(
:return: Contents of text field. None if closed using X or cancelled
"""
global _my_windows
# global _my_windows
if no_window:
if _my_windows.NumOpenWindows:
# if _my_windows.NumOpenWindows:
if Window.NumOpenWindows:
root = tk.Toplevel()
else:
root = tk.Tk()
@ -6775,10 +6792,11 @@ def PopupGetFile(message, title=None, default_path='', default_extension='', sav
:return: string representing the path chosen, None if cancelled or window closed with X
"""
global _my_windows
# global _my_windows
if no_window:
if _my_windows.NumOpenWindows:
# if _my_windows.NumOpenWindows:
if Window.NumOpenWindows:
root = tk.Toplevel()
else:
root = tk.Tk()