def create_images(self, filenames): """ Create one or more images in the dialog. :param filenames: May be a filename (which will generate a single image), a list of filenames (which will generate a row of images), or a list of list of filename (which will create a 2D array of buttons. :return: """ if filenames is None: return # Convert to a list of lists of filenames regardless of input if is_string(filenames): filenames = [ [ filenames, ], ] elif is_sequence(filenames) and is_string(filenames[0]): filenames = [ filenames, ] elif is_sequence(filenames) and is_sequence( filenames[0]) and is_string(filenames[0][0]): pass else: raise ValueError("Incorrect images argument.") images = list() for _r, images_row in enumerate(filenames): row_number = len(filenames) - _r for column_number, filename in enumerate(images_row): this_image = dict() try: this_image['tk_image'] = ut.load_tk_image(filename) except Exception as e: print(e) this_image['tk_image'] = None this_image['widget'] = tk.Button(self.imagesFrame, takefocus=1, compound=tk.TOP) if this_image['widget'] is not None: this_image['widget'].configure( image=this_image['tk_image']) fn = lambda text=filename, row=_r, column=column_number: self.button_pressed( text, (row, column)) this_image['widget'].configure(command=fn) sticky_dir = tk.N + tk.S + tk.E + tk.W this_image['widget'].grid(row=row_number, column=column_number, sticky=sticky_dir, padx='1m', pady='1m', ipadx='2m', ipady='1m') self.imagesFrame.rowconfigure(row_number, weight=10, minsize='10m') self.imagesFrame.columnconfigure(column_number, weight=10) images.append(this_image) self._images = images # Image objects must live, so place them in self. Otherwise, they will be deleted.
def create_images(self, filenames): """ Create one or more images in the dialog. :param filenames: May be a filename (which will generate a single image), a list of filenames (which will generate a row of images), or a list of list of filename (which will create a 2D array of buttons. :return: """ if filenames is None: return # Convert to a list of lists of filenames regardless of input if is_string(filenames): filenames = [[filenames,],] elif is_sequence(filenames) and is_string(filenames[0]): filenames = [filenames,] elif is_sequence(filenames) and is_sequence(filenames[0]) and is_string(filenames[0][0]): pass else: raise ValueError("Incorrect images argument.") images = list() for _r, images_row in enumerate(filenames): row_number = len(filenames) - _r for column_number, filename in enumerate(images_row): this_image = dict() try: this_image['tk_image'] = ut.load_tk_image(filename) except Exception as e: print(e) this_image['tk_image'] = None this_image['widget'] = tk.Button( self.imagesFrame, takefocus=1, compound=tk.TOP) if this_image['widget'] is not None: this_image['widget'].configure(image=this_image['tk_image']) fn = lambda text=filename, row=_r, column=column_number: self.button_pressed(text, (row, column)) this_image['widget'].configure(command=fn) sticky_dir = tk.N+tk.S+tk.E+tk.W this_image['widget'].grid(row=row_number, column=column_number, sticky=sticky_dir, padx='1m', pady='1m', ipadx='2m', ipady='1m') self.imagesFrame.rowconfigure(row_number, weight=10, minsize='10m') self.imagesFrame.columnconfigure(column_number, weight=10) images.append(this_image) self._images = images # Image objects must live, so place them in self. Otherwise, they will be deleted.
def __fillablebox(msg, title="", default="", mask=None, image=None, root=None): """ Show a box in which a user can enter some text. You may optionally specify some default text, which will appear in the enterbox when it is displayed. Returns the text that the user entered, or None if he cancels the operation. """ global boxRoot, __enterboxText, __enterboxDefaultText global cancelButton, entryWidget, okButton if title is None: title = "" if default is None: default = "" __enterboxDefaultText = default __enterboxText = __enterboxDefaultText if root: root.withdraw() boxRoot = tk.Toplevel(master=root) boxRoot.withdraw() else: boxRoot = tk.Tk() boxRoot.withdraw() boxRoot.protocol('WM_DELETE_WINDOW', __enterboxQuit) boxRoot.title(title) boxRoot.iconname('Dialog') boxRoot.geometry(global_state.window_position) boxRoot.bind("<Escape>", __enterboxCancel) # ------------- define the messageFrame --------------------------------- messageFrame = tk.Frame(master=boxRoot) messageFrame.pack(side=tk.TOP, fill=tk.BOTH) # ------------- define the imageFrame --------------------------------- try: tk_Image = ut.load_tk_image(image) except Exception as inst: print(inst) tk_Image = None if tk_Image: imageFrame = tk.Frame(master=boxRoot) imageFrame.pack(side=tk.TOP, fill=tk.BOTH) label = tk.Label(imageFrame, image=tk_Image) label.image = tk_Image # keep a reference! label.pack(side=tk.TOP, expand=tk.YES, fill=tk.X, padx='1m', pady='1m') # ------------- define the buttonsFrame --------------------------------- buttonsFrame = tk.Frame(master=boxRoot) buttonsFrame.pack(side=tk.TOP, fill=tk.BOTH) # ------------- define the entryFrame --------------------------------- entryFrame = tk.Frame(master=boxRoot) entryFrame.pack(side=tk.TOP, fill=tk.BOTH) # ------------- define the buttonsFrame --------------------------------- buttonsFrame = tk.Frame(master=boxRoot) buttonsFrame.pack(side=tk.TOP, fill=tk.BOTH) # -------------------- the msg widget ---------------------------- messageWidget = tk.Message(messageFrame, width="4.5i", text=msg) messageWidget.configure(font=(global_state.PROPORTIONAL_FONT_FAMILY, global_state.PROPORTIONAL_FONT_SIZE)) messageWidget.pack(side=tk.RIGHT, expand=1, fill=tk.BOTH, padx='3m', pady='3m') # --------- entryWidget ---------------------------------------------- entryWidget = tk.Entry(entryFrame, width=40) bindArrows(entryWidget) entryWidget.configure(font=(global_state.PROPORTIONAL_FONT_FAMILY, global_state.TEXT_ENTRY_FONT_SIZE)) if mask: entryWidget.configure(show=mask) entryWidget.pack(side=tk.LEFT, padx="3m") entryWidget.bind("<Return>", __enterboxGetText) entryWidget.bind("<Escape>", __enterboxCancel) # put text into the entryWidget entryWidget.insert(0, __enterboxDefaultText) # ------------------ ok button ------------------------------- okButton = tk.Button(buttonsFrame, takefocus=1, text="OK") bindArrows(okButton) okButton.pack(expand=1, side=tk.LEFT, padx='3m', pady='3m', ipadx='2m', ipady='1m') # for the commandButton, bind activation events to the activation event # handler commandButton = okButton handler = __enterboxGetText for selectionEvent in global_state.STANDARD_SELECTION_EVENTS: commandButton.bind("<{}>".format(selectionEvent), handler) # ------------------ cancel button ------------------------------- cancelButton = tk.Button(buttonsFrame, takefocus=1, text="Cancel") bindArrows(cancelButton) cancelButton.pack(expand=1, side=tk.RIGHT, padx='3m', pady='3m', ipadx='2m', ipady='1m') # for the commandButton, bind activation events to the activation event # handler commandButton = cancelButton handler = __enterboxCancel for selectionEvent in global_state.STANDARD_SELECTION_EVENTS: commandButton.bind("<{}>".format(selectionEvent), handler) # ------------------- time for action! ----------------- entryWidget.focus_force() # put the focus on the entryWidget boxRoot.deiconify() boxRoot.mainloop() # run it! # -------- after the run has completed ---------------------------------- if root: root.deiconify() boxRoot.destroy() # button_click didn't destroy boxRoot, so we do it now return __enterboxText
def __fillablebox(msg, title="", default="", mask=None, image=None, root=None): """ Show a box in which a user can enter some text. You may optionally specify some default text, which will appear in the enterbox when it is displayed. Returns the text that the user entered, or None if he cancels the operation. """ global boxRoot, __enterboxText, __enterboxDefaultText global cancelButton, entryWidget, okButton if title is None: title = "" if default is None: default = "" __enterboxDefaultText = default __enterboxText = __enterboxDefaultText if root: root.withdraw() boxRoot = tk.Toplevel(master=root) boxRoot.withdraw() else: boxRoot = tk.Tk() boxRoot.withdraw() boxRoot.protocol('WM_DELETE_WINDOW', __enterboxQuit) boxRoot.title(title) boxRoot.iconname('Dialog') boxRoot.geometry(global_state.window_position) boxRoot.bind("<Escape>", __enterboxCancel) # ------------- define the messageFrame --------------------------------- messageFrame = tk.Frame(master=boxRoot) messageFrame.pack(side=tk.TOP, fill=tk.BOTH) # ------------- define the imageFrame --------------------------------- try: tk_Image = ut.load_tk_image(image) except Exception as inst: print(inst) tk_Image = None if tk_Image: imageFrame = tk.Frame(master=boxRoot) imageFrame.pack(side=tk.TOP, fill=tk.BOTH) label = tk.Label(imageFrame, image=tk_Image) label.image = tk_Image # keep a reference! label.pack(side=tk.TOP, expand=tk.YES, fill=tk.X, padx='1m', pady='1m') # ------------- define the buttonsFrame --------------------------------- buttonsFrame = tk.Frame(master=boxRoot) buttonsFrame.pack(side=tk.TOP, fill=tk.BOTH) # ------------- define the entryFrame --------------------------------- entryFrame = tk.Frame(master=boxRoot) entryFrame.pack(side=tk.TOP, fill=tk.BOTH) # ------------- define the buttonsFrame --------------------------------- buttonsFrame = tk.Frame(master=boxRoot) buttonsFrame.pack(side=tk.TOP, fill=tk.BOTH) # -------------------- the msg widget ---------------------------- messageWidget = tk.Message(messageFrame, width="4.5i", text=msg) messageWidget.configure( font=(global_state.PROPORTIONAL_FONT_FAMILY, global_state.PROPORTIONAL_FONT_SIZE)) messageWidget.pack( side=tk.RIGHT, expand=1, fill=tk.BOTH, padx='3m', pady='3m') # --------- entryWidget ---------------------------------------------- entryWidget = tk.Entry(entryFrame, width=40) bindArrows(entryWidget) entryWidget.configure( font=(global_state.PROPORTIONAL_FONT_FAMILY, global_state.TEXT_ENTRY_FONT_SIZE)) if mask: entryWidget.configure(show=mask) entryWidget.pack(side=tk.LEFT, padx="3m") entryWidget.bind("<Return>", __enterboxGetText) entryWidget.bind("<Escape>", __enterboxCancel) # put text into the entryWidget entryWidget.insert(0, __enterboxDefaultText) # ------------------ ok button ------------------------------- okButton = tk.Button(buttonsFrame, takefocus=1, text="OK") bindArrows(okButton) okButton.pack( expand=1, side=tk.LEFT, padx='3m', pady='3m', ipadx='2m', ipady='1m') # for the commandButton, bind activation events to the activation event # handler commandButton = okButton handler = __enterboxGetText for selectionEvent in global_state.STANDARD_SELECTION_EVENTS: commandButton.bind("<{}>".format(selectionEvent), handler) # ------------------ cancel button ------------------------------- cancelButton = tk.Button(buttonsFrame, takefocus=1, text="Cancel") bindArrows(cancelButton) cancelButton.pack( expand=1, side=tk.RIGHT, padx='3m', pady='3m', ipadx='2m', ipady='1m') # for the commandButton, bind activation events to the activation event # handler commandButton = cancelButton handler = __enterboxCancel for selectionEvent in global_state.STANDARD_SELECTION_EVENTS: commandButton.bind("<{}>".format(selectionEvent), handler) # ------------------- time for action! ----------------- entryWidget.focus_force() # put the focus on the entryWidget boxRoot.deiconify() boxRoot.mainloop() # run it! # -------- after the run has completed ---------------------------------- if root: root.deiconify() boxRoot.destroy() # button_click didn't destroy boxRoot, so we do it now return __enterboxText