MCPcopy
hub / github.com/PySimpleGUI/PySimpleGUI / ButtonCallBack

Method ButtonCallBack

PySimpleGUI/PySimpleGUI.py:5030–5155  ·  view source on GitHub ↗

Not user callable! Called by tkinter when a button is clicked. This is where all the fun begins!

(self)

Source from the content-addressed store, hash-verified

5028
5029 # ------- Button Callback ------- #
5030 def ButtonCallBack(self):
5031 """
5032 Not user callable! Called by tkinter when a button is clicked. This is where all the fun begins!
5033 """
5034
5035 if self.Disabled == BUTTON_DISABLED_MEANS_IGNORE:
5036 return
5037 target_element, strvar, should_submit_window = self._find_target()
5038
5039 filetypes = FILE_TYPES_ALL_FILES if self.FileTypes is None else self.FileTypes
5040
5041 if self.BType == BUTTON_TYPE_BROWSE_FOLDER:
5042 if running_mac(): # macs don't like seeing the parent window (go firgure)
5043 folder_name = tk.filedialog.askdirectory(initialdir=self.InitialFolder) # show the 'get folder' dialog box
5044 else:
5045 folder_name = tk.filedialog.askdirectory(initialdir=self.InitialFolder, parent=self.ParentForm.TKroot) # show the 'get folder' dialog box
5046 if folder_name:
5047 try:
5048 strvar.set(folder_name)
5049 self.TKStringVar.set(folder_name)
5050 except:
5051 pass
5052 else: # if "cancel" button clicked, don't generate an event
5053 should_submit_window = False
5054 elif self.BType == BUTTON_TYPE_BROWSE_FILE:
5055 if running_mac():
5056 # Workaround for the "*.*" issue on Mac
5057 is_all = [(x, y) for (x, y) in filetypes if all(ch in '* .' for ch in y)]
5058 if not len(set(filetypes)) > 1 and (len(is_all) != 0 or filetypes == FILE_TYPES_ALL_FILES):
5059 file_name = tk.filedialog.askopenfilename(initialdir=self.InitialFolder)
5060 else:
5061 file_name = tk.filedialog.askopenfilename(initialdir=self.InitialFolder, filetypes=filetypes) # show the 'get file' dialog box
5062 # elif _mac_allow_filetypes():
5063 # file_name = tk.filedialog.askopenfilename(initialdir=self.InitialFolder, filetypes=filetypes) # show the 'get file' dialog box
5064 # else:
5065 # file_name = tk.filedialog.askopenfilename(initialdir=self.InitialFolder) # show the 'get file' dialog box
5066 else:
5067 file_name = tk.filedialog.askopenfilename(filetypes=filetypes, initialdir=self.InitialFolder, parent=self.ParentForm.TKroot) # show the 'get file' dialog box
5068
5069 if file_name:
5070 strvar.set(file_name)
5071 self.TKStringVar.set(file_name)
5072 else: # if "cancel" button clicked, don't generate an event
5073 should_submit_window = False
5074 elif self.BType == BUTTON_TYPE_COLOR_CHOOSER:
5075 color = tk.colorchooser.askcolor(parent=self.ParentForm.TKroot, color=self.default_color) # show the 'get file' dialog box
5076 color = color[1] # save only the #RRGGBB portion
5077 if color is not None:
5078 strvar.set(color)
5079 self.TKStringVar.set(color)
5080 elif self.BType == BUTTON_TYPE_BROWSE_FILES:
5081 if running_mac():
5082 # Workaround for the "*.*" issue on Mac
5083 is_all = [(x, y) for (x, y) in filetypes if all(ch in '* .' for ch in y)]
5084 if not len(set(filetypes)) > 1 and (len(is_all) != 0 or filetypes == FILE_TYPES_ALL_FILES):
5085 file_name = tk.filedialog.askopenfilenames(initialdir=self.InitialFolder)
5086 else:
5087 file_name = tk.filedialog.askopenfilenames(filetypes=filetypes, initialdir=self.InitialFolder)

Callers 1

_ReturnKeyHandlerMethod · 0.80

Calls 6

_find_targetMethod · 0.95
running_macFunction · 0.85
_exit_mainloopFunction · 0.85
_CloseMethod · 0.80
_DecrementOpenCountMethod · 0.80
setMethod · 0.45

Tested by

no test coverage detected