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