• R/O
  • SSH

tkpane: Commit

Default repository for tkpane.py.


Commit MetaInfo

Revisão39453a474616b42534d28227e1029ae601b6e7c6 (tree)
Hora2018-03-02 23:06:43
AutorDreas Nielsen <dreas.nielsen@gmai...>
CommiterDreas Nielsen

Mensagem de Log

Added 'set_entry_validator()' method to EntryPane in lib.py. Added InputFilePane2 to lib.py.

Mudança Sumário

Diff

diff -r f3331a1787ee -r 39453a474616 tkpane/lib.py
--- a/tkpane/lib.py Thu Mar 01 16:08:38 2018 -0800
+++ b/tkpane/lib.py Fri Mar 02 06:06:43 2018 -0800
@@ -24,7 +24,7 @@
2424 for creation of other custom pane classes.
2525 """
2626
27-__version__ = "0.10.1"
27+__version__ = "0.11.0"
2828
2929
3030 try:
@@ -612,12 +612,14 @@
612612 Custom method:
613613
614614 * set_key
615+ * set_entry_validator
615616 """
616617
617618 def __init__(self, parent, pane_name, prompt, key_name=None):
618619 tkpane.TkPane.__init__(self, parent, pane_name, frame_config_opts(), frame_grid_opts())
619620 self.datakeyname = "entry" if key_name is None else key_name
620621 self.datakeylist = [self.datakeyname]
622+ self.entry_validator = None
621623 self.prompt = ttk.Label(self, text=prompt, width=max(12, len(prompt)), anchor=tk.E)
622624 self.entry_var = tk.StringVar()
623625 self.entrywidget = ttk.Entry(self, textvariable=self.entry_var, exportselection=False)
@@ -639,8 +641,14 @@
639641
640642 def valid_data(self, entry_widget=None):
641643 text = self.entry_var.get()
642- return not (text == "" and self.required)
643-
644+ if text == "":
645+ return not self.required
646+ else:
647+ if self.entry_validator is not None:
648+ return self.entry_validator(text)
649+ else:
650+ return True
651+
644652 def save_data(self, is_valid, entry_widget):
645653 """Update the pane's data dictionary with data from the Entry widget."""
646654 text = self.entry_var.get()
@@ -699,6 +707,14 @@
699707 self.datakeyname = key_name
700708 self.datakeylist = [key_name]
701709
710+ def set_entry_validator(self, fn):
711+ """Set the callback function that will be used to check the entered value.
712+
713+ This function must take the entry value as an argument and return a Boolean.
714+ """
715+ self.entry_validator = fn
716+
717+
702718
703719 class InputFilePane(tkpane.TkPane):
704720 """Get and display an input filename.
@@ -720,14 +736,19 @@
720736 * set_style
721737 * focus
722738 * set_data
739+
740+ Custom methods:
741+
742+ * set_key
743+ * set_filename_validator
723744 """
724745
725746 def __init__(self, parent, optiondict=None):
726747 tkpane.TkPane.__init__(self, parent, "Input filename", frame_config_opts(), frame_grid_opts())
727748 # Customize attributes
728749 self.optiondict = {} if optiondict is None else optiondict
729- self.datakey = "input_filename"
730- self.datakeylist = [self.datakey]
750+ self.datakeyname = "input_filename"
751+ self.datakeylist = [self.datakeyname]
731752 self.filename_validator = None
732753 # Create, configure, and place widgets.
733754 self.dir_label = ttk.Label(self, text='Input file:', width=12, anchor=tk.E)
@@ -779,7 +800,7 @@
779800 if filename == "":
780801 self.clear_own()
781802 else:
782- self.datadict[self.datakey] = filename
803+ self.datadict[self.datakeyname] = filename
783804 else:
784805 self.clear_own()
785806
@@ -805,11 +826,165 @@
805826
806827 Special key supported: 'input_filename' changes the filename in the entry widget.
807828 """
808- if self.datakey in data:
809- self.file_var.set(data[self.datakey])
829+ if "input_filename" in data.keys():
830+ self.file_var.set(data["input_filename"])
810831 self.handle_change_validity(True, self.file_display)
811832 self.send_status_message(True)
812- self.set_allbut(data, [self.datakey])
833+ self.set_allbut(data, ["input_filename"])
834+
835+ #---------------------------------------------------------------------------
836+ # Custom methods.
837+ #...........................................................................
838+
839+ def set_key(self, key_name):
840+ """Change the name of the data key used for the entered data.
841+
842+ :param key_name: New name for the data key.
843+
844+ This method allows the name of the data key to be customized to
845+ eliminate conflicts with other InputFilePane objects on the same UI.
846+ """
847+ if self.datakeyname in self.datadict:
848+ self.datadict[key_name] = self.datadict[self.datakeyname]
849+ del self.datadict[self.datakeyname]
850+ self.datakeyname = key_name
851+ self.datakeylist = [key_name]
852+
853+ def check_entrychange(self, *args):
854+ self.handle_change_validity(self.valid_data(None), self.file_display)
855+
856+ def set_inputfile(self):
857+ fn = tk_file.askopenfilename(**self.optiondict)
858+ if fn != "":
859+ # The order of the following steps is important.
860+ self.file_var.set(fn)
861+ self.handle_change_validity(self.valid_data(None), self.file_display)
862+ self.send_status_message(self.valid_data(None))
863+
864+ def set_filename_validator(self, fn):
865+ """Set the callback function that will be used to check the entered filename.
866+
867+ This function must take the filename as an argument and return a Boolean.
868+ """
869+ self.filename_validator = fn
870+
871+
872+class InputFilePane2(tkpane.TkPane):
873+ """Get and display an input filename. This class accepts a prompt and uses a different widget layout than InputFilePane.
874+
875+ :param prompt: A prompt presented above the text box for entry of the filename (optional; default="Input file:").
876+ :param optiondict: A dictionary of option names and values for the Tkinter 'askopenfilename' method (optional).
877+ :key_name: A name to use as a key for the data value (filename) managed by this pane (optional default="input_filename").
878+
879+ Data key managed by this pane: "input_filename" or the key name specified during initialization.
880+
881+ Name used by this pane: "Input filename".
882+
883+ Overridden methods:
884+
885+ * entry_widgets
886+ * valid_data
887+ * save_data
888+ * clear_pane
889+ * disable_pane
890+ * enable_pane
891+ * set_style
892+ * focus
893+ * set_data
894+
895+ Custom methods:
896+
897+ * set_key
898+ * set_filename_validator
899+ """
900+
901+ def __init__(self, parent, prompt="Input file:", optiondict=None, key_name=None):
902+ tkpane.TkPane.__init__(self, parent, "Input filename", frame_config_opts(), frame_grid_opts())
903+ # Customize attributes
904+ self.optiondict = {} if optiondict is None else optiondict
905+ self.datakeyname = "input_filename" if not key_name else key_name
906+ self.datakeylist = [self.datakeyname]
907+ self.filename_validator = None
908+ # Create, configure, and place widgets.
909+ self.dir_label = ttk.Label(self, text='Input file:', width=12, anchor=tk.W)
910+ self.file_var = tk.StringVar()
911+ self.file_var.trace("w", self.check_entrychange)
912+ self.file_display = ttk.Entry(self, textvariable=self.file_var)
913+ self.valid_color = self.file_display.cget("background")
914+ self.browse_button = ttk.Button(self, text='Browse', width=8, command=self.set_inputfile)
915+ self.dir_label.grid(row=0, column=0, padx=3, pady=(3,2), sticky=tk.W)
916+ self.file_display.grid(row=1, column=0, padx=(3,2), pady=(2,3), sticky=tk.EW)
917+ self.browse_button.grid(row=1, column=1, padx=(2,3), pady=3, sticky=tk.W)
918+ self.columnconfigure(0, weight=1)
919+ self.columnconfigure(1, weight=0)
920+ self.rowconfigure(1, weight=0)
921+ parent.rowconfigure(0, weight=0)
922+ parent.rowconfigure(0, weight=1)
923+ parent.columnconfigure(0, weight=1)
924+
925+ #---------------------------------------------------------------------------
926+ # Overrides of class methods.
927+ #...........................................................................
928+
929+ def entry_widgets(self):
930+ """Return a list of widgets used for data entry."""
931+ return [self.file_display]
932+
933+ def valid_data(self, widget=None):
934+ """Return True or False indicating the validity of the filename entry.
935+
936+ Overrides TkPane class method.
937+ """
938+ import os.path
939+ filename = self.file_display.get()
940+ if filename == "":
941+ return not self.required
942+ else:
943+ if self.filename_validator is not None:
944+ return self.filename_validator(filename)
945+ else:
946+ return os.path.isfile(filename)
947+
948+ def save_data(self, is_valid, entry_widget):
949+ """Update the pane's data dictionary with data from the entry widget.
950+
951+ Overrides TkPane class method.
952+ """
953+ filename = self.file_display.get()
954+ if is_valid:
955+ if filename == "":
956+ self.clear_own()
957+ else:
958+ self.datadict[self.datakeyname] = filename
959+ else:
960+ self.clear_own()
961+
962+ def clear_pane(self):
963+ self.file_var.set(u'')
964+
965+ def enable_pane(self):
966+ self._enablewidgets([self.dir_label, self.file_display, self.browse_button])
967+
968+ def disable_pane(self):
969+ self._disablewidgets([self.dir_label, self.file_display, self.browse_button])
970+
971+ def set_style(self, ttk_style):
972+ self._setstyle([self.dir_label, self.file_display, self.browse_button], ttk_style)
973+
974+ def focus(self):
975+ """Set the focus to the entry."""
976+ self.file_display.focus_set()
977+
978+ def set_data(self, data):
979+ """Update the pane's data dictionary with the provided data.
980+
981+ Special key supported: 'input_filename' changes the filename in the entry widget.
982+ """
983+ if "input_filename" in data.keys():
984+ self.file_var.set(data["input_filename"])
985+ self.handle_change_validity(True, self.file_display)
986+ self.send_status_message(True)
987+ self.set_allbut(data, ["input_filename"])
813988
814989 #---------------------------------------------------------------------------
815990 # Custom methods.
diff -r f3331a1787ee -r 39453a474616 tkpane/tkpane.py
--- a/tkpane/tkpane.py Thu Mar 01 16:08:38 2018 -0800
+++ b/tkpane/tkpane.py Fri Mar 02 06:06:43 2018 -0800
@@ -27,7 +27,7 @@
2727 that pass specific data to allow communication between panes, and callback methods
2828 for reporting status and progress."""
2929
30-__version__ = "0.22.0"
30+__version__ = "0.23.0"
3131
3232
3333 try:
Show on old repository browser