Документ взят из кэша поисковой машины. Адрес
оригинального документа
: http://www.apo.nmsu.edu/35m_operations/TUI/Scripts/ScriptingTutorial/UserInput.html
Дата изменения: Sat Sep 6 02:16:02 2014 Дата индексирования: Sun Apr 10 07:19:13 2016 Кодировка: Поисковые слова: ic 4603 |
This example shows how to add a few input widgets to a script and how to get data from them. This is a fairly artificial example in that one would normally just use the DIS Expose window for this purpose. However, it demonstrates a few useful techniques, including:
import RO.Wdg import Tkinter import TUI.Inst.ExposeModel as ExposeModel from TUI.Inst.ExposeStatusWdg import ExposeStatusWdg class ScriptClass(object): """Take a series of DIS darks with user input. """ def __init__(self, sr): """Display exposure status and a few user input widgets. """ # if True, run in debug-only mode (which doesn't DO anything, it just pretends) sr.debug = False expStatusWdg = ExposeStatusWdg( master = sr.master, instName = "DIS", ) expStatusWdg.grid(row=0, column=0, sticky="w") wdgFrame = Tkinter.Frame(sr.master) gr = RO.Wdg.Gridder(wdgFrame) self.expModel = ExposeModel.getModel("DIS") timeUnitsVar = Tkinter.StringVar() self.timeWdg = RO.Wdg.DMSEntry ( master = wdgFrame, minValue = self.expModel.instInfo.minExpTime, maxValue = self.expModel.instInfo.maxExpTime, isRelative = True, isHours = True, unitsVar = timeUnitsVar, width = 10, helpText = "Exposure time", ) gr.gridWdg("Time", self.timeWdg, timeUnitsVar) self.numExpWdg = RO.Wdg.IntEntry( master = wdgFrame, defValue = 1, minValue = 1, maxValue = 999, helpText = "Number of exposures in the sequence", ) gr.gridWdg("#Exp", self.numExpWdg) wdgFrame.grid(row=1, column=0, sticky="w") def run(self, sr): """Take a series of DIS darks""" expType = "dark" expTime = self.timeWdg.getNum() numExp = self.numExpWdg.getNum() fileName = "dis_" + expType if expTime <= 0: raise sr.ScriptError("Specify exposure time") cmdStr = self.expModel.formatExpCmd( expType = expType, expTime = expTime, fileName = fileName, numExp = numExp, ) yield sr.waitCmd( actor = "disExpose", cmdStr = cmdStr, abortCmdStr = "abort", )
Comments:
wdgFrame
to hold the input widgets. This frame is used as the master widget for the input widgets. The input widgets are gridded into the widget frame and the widget frame is gridded into sr.master. Using a hierarchy of widgets like this does have a few pitfalls:
gr = RO.Wdg.Gridder(wdgFrame)
creates a "gridder" for the widget frame. Gridders specialize in laying out rows of input and status widgets, saving you some of the headache of using Tk's raw gridder or packer.
__init__
to run
via self
.
raise sr.ScriptError(...)
will report an error to the status bar and halt your script. You can raise any other exception if you prefer, but then you will log the error message and a traceback to the error log (which is excellent for debugging unexpected errors but intrusive for reporting "normal" errors).
It is actually possible to create a gridder on sr.master and use it to lay out the status widget and the input widgets. I present the code without comment for folks who understand the Tk gridder and are willing to read the help for RO.Wdg.Gridder.
gr = RO.Wdg.Gridder(sr.master) expStatusWdg = ExposeStatusWdg( master = sr.master, instName = "DIS", ) gr.gridWdg(False, expStatusWdg, colSpan=4) sr.master.grid_columnconfigure(3, weight=1) ... gr.gridWdg("Time", timeWdg, timeUnitsVar) ... gr.gridWdg("#Exp", numExpWdg)