コード例 #1
0
class RotatePanel(CtxSubPanel):

    name = 'RotatePanel'

    def __init__(self, parent):
        CtxSubPanel.__init__(self, parent)
        self.angle = DoubleVar(self.mw.root)
        self.angle.set(0)

        label = TLabel(self.panel, image='context_R')
        label.pack(side=LEFT)
        self.entry_width = TSpinbox(self.panel,
                                    var=0,
                                    vartype=1,
                                    textvariable=self.angle,
                                    min=-360,
                                    max=360,
                                    step=1,
                                    width=6,
                                    command=self.applyRotate)
        tooltips.AddDescription(self.entry_width, _('Rotation angle'))
        self.entry_width.pack(side=LEFT, padx=5)
        b = TButton(self.panel,
                    command=self.rotLeft,
                    style='Toolbutton',
                    image='context_rotate_ccw')
        tooltips.AddDescription(b, _(u'Rotate -90°'))
        b.pack(side=LEFT)
        b = TButton(self.panel,
                    command=self.rot180,
                    style='Toolbutton',
                    image='context_rotate')
        tooltips.AddDescription(b, _(u'Rotate 180°'))
        b.pack(side=LEFT)
        b = TButton(self.panel,
                    command=self.rotRight,
                    style='Toolbutton',
                    image='context_rotate_cw')
        tooltips.AddDescription(b, _(u'Rotate 90°'))
        b.pack(side=LEFT)

    def rot180(self):
        self.rotation(180)

    def rotLeft(self):
        self.rotation(90)

    def rotRight(self):
        self.rotation(-90)

    def applyRotate(self, *arg):
        self.rotation(self.angle.get())

    def rotation(self, angle):
        if angle < 0:
            if angle < -360:
                angle += int(angle / 360) * 360
            angle += 360
        self.doc.RotateSelected(angle)
コード例 #2
0
ファイル: rotation_panel.py プロジェクト: kindlychung/sk1
class RotatePanel(CtxSubPanel):
	
	name='RotatePanel'	
	
	def __init__(self, parent):
		CtxSubPanel.__init__(self, parent)
		self.angle=DoubleVar(self.mw.root)
		self.angle.set(0)
		
		label = TLabel(self.panel, image='context_R')
		label.pack(side = LEFT)
		self.entry_width = TSpinbox(self.panel,  var=0, vartype=1, textvariable = self.angle,
						min = -360, max = 360, step = 1, width = 6, command = self.applyRotate)
		tooltips.AddDescription(self.entry_width, _('Rotation angle'))
		self.entry_width.pack(side = LEFT, padx=5)
		b = TButton(self.panel, command=self.rotLeft, style='Toolbutton', image='context_rotate_ccw')
		tooltips.AddDescription(b, _(u'Rotate -90°'))
		b.pack(side = LEFT)
		b = TButton(self.panel,  command=self.rot180, style='Toolbutton', image='context_rotate')
		tooltips.AddDescription(b, _(u'Rotate 180°'))
		b.pack(side = LEFT)
		b = TButton(self.panel,  command=self.rotRight, style='Toolbutton', image='context_rotate_cw')
		tooltips.AddDescription(b, _(u'Rotate 90°'))
		b.pack(side = LEFT)

	def rot180(self):
		self.rotation(180)
		
	def rotLeft(self):
		self.rotation(90)
		
	def rotRight(self):
		self.rotation(-90)
		
	def applyRotate(self, *arg):
		self.rotation(self.angle.get())
		
	def rotation(self, angle):
		if angle<0:
			if angle<-360:
				angle+=int(angle/360)*360
			angle+=360
		self.doc.RotateSelected(angle)
コード例 #3
0
ファイル: training_settings.py プロジェクト: fmihaich/annic
class TrainingError(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        
        training_error_laber = Label(self, text = 'Training Error: ')
        training_error_laber.pack(side = 'left')
        
        error_options = [round(0.1 + i*0.1, 2) for i in range(25)]
        self.selected_error = DoubleVar(self, value = DEFAULT_TRAINING_ERROR)
        option_menu = OptionMenu(self, self.selected_error, *error_options, 
                                 command = self._on_error_selection)
        option_menu.pack(side = 'right')
    
    def get(self):
        return self.selected_error.get()
    
    def reset(self):
        self.selected_error.set(DEFAULT_TRAINING_ERROR)
        
    def _on_error_selection(self, selected_error):
        print "Selected training error: ", selected_error
コード例 #4
0
def set_tk_var():
    global method
    method = StringVar()
    method.set(read_config("Config", "Method", "str"))
    global server
    server = StringVar()
    server.set(read_config("Config", "Server", "str"))
    global shelltype
    shelltype = StringVar()
    shelltype.set(read_config("Config", "ShellType", "str"))
    global parameter
    parameter = StringVar()
    parameter.set(read_config("Config", "Parameter", "str"))
    global thread_num
    thread_num = IntVar()
    thread_num.set(read_config("Config", "ThreadNumber", "int"))
    global req_delay
    req_delay = DoubleVar()
    req_delay.set(read_config("Config", "RequestDelay", "float"))
    global time_out
    time_out = DoubleVar()
    time_out.set(read_config("Config", "RequestTimeout", "float"))
    global random_ua
    random_ua = BooleanVar()
    random_ua.set(read_config("Config", "RandomUserAgent", "boolean"))
    global con_close
    con_close = BooleanVar()
    con_close.set(read_config("Config", "ConnectionClose", "boolean"))
    global keep_alive
    keep_alive = BooleanVar()
    keep_alive.set(read_config("Config", "KeepAlive0", "boolean"))
    global custom_hdr
    custom_hdr = BooleanVar()
    custom_hdr.set(read_config("Config", "CustomRequestHeader", "boolean"))
    global custom_hdr_data
    custom_hdr_data = StringVar()
    custom_hdr_data.set(read_config("Config", "CustomRequestHeaderData", "str"))
コード例 #5
0
ファイル: training_settings.py プロジェクト: fmihaich/annic
class TrainingError(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent

        training_error_laber = Label(self, text='Training Error: ')
        training_error_laber.pack(side='left')

        error_options = [round(0.1 + i * 0.1, 2) for i in range(25)]
        self.selected_error = DoubleVar(self, value=DEFAULT_TRAINING_ERROR)
        option_menu = OptionMenu(self,
                                 self.selected_error,
                                 *error_options,
                                 command=self._on_error_selection)
        option_menu.pack(side='right')

    def get(self):
        return self.selected_error.get()

    def reset(self):
        self.selected_error.set(DEFAULT_TRAINING_ERROR)

    def _on_error_selection(self, selected_error):
        print "Selected training error: ", selected_error
コード例 #6
0
class SystemProperties:
    """Gets the flow and contaminant properties."""
    def __init__(self, master, system):
        """Constructor method.  Defines the parameters to be obtained in this
        window."""

        self.master = master
        self.system = system
        self.fonttype = system.fonttype
        self.version = system.version
        self.superfont = get_superfont(self.fonttype)  #superscript font
        self.tframe = Frame(master.tframe)
        self.frame = Frame(master.frame)
        self.bframe = Frame(master.bframe)

        self.advs = ['None', 'Steady flow', 'Period oscillation']
        self.bios = ['None', 'Uniform', 'Depth-dependent']
        self.cons = ['None', 'Consolidation']

        self.adv = StringVar(value=self.advs[0])  #tidal flag
        self.bio = StringVar(value=self.bios[0])  #bioturbation flag
        self.con = StringVar(value=self.cons[0])  #consolidation flag

        self.hbio = DoubleVar(value=10.)  #bioturbation depth
        self.sigma = DoubleVar(value=10.)  #bioturbation depth
        self.Dbiop = DoubleVar(value=1.)  #particle bioturbation coeff
        self.Dbiopw = DoubleVar(value=100.)  #pore water bioturbation coeff
        self.Vdar = DoubleVar(value=0.)  #Darcy velocity
        self.Vtidal = DoubleVar(value=0.)  #Tidal velocity
        self.ptidal = DoubleVar(value=0.0014)  #Tidal frequency
        self.hcon = DoubleVar(value=0)  #total consolidation
        self.t90 = DoubleVar(value=1.)  #time to 90% consolidation
        self.top = None  #flag for existence of toplevel

        if system.layers[0].number == 0: self.toplayer_h = system.layers[1].h
        else: self.toplayer_h = system.layers[0].h

        self.lengthunit = system.lengthunit
        self.timeunit = system.timeunit
        self.diffunit = system.diffunit
        self.diffunits = system.diffunits

        try:
            self.adv.set(system.adv)
            self.bio.set(system.bio)
            self.con.set(system.con)
            self.Vdar.set(system.Vdar)

            self.hbio.set(system.hbio)
            self.sigma.set(system.sigma)
            self.Dbiop.set(system.Dbiop)
            self.Dbiopw.set(system.Dbiopw)

            self.Vtidal.set(system.Vtidal)
            self.ptidal.set(system.ptidal)

            self.hcon.set(system.hcon)
            self.t90.set(system.t90)
        except:
            pass

    def make_widgets(self):
        """Make the widgets for the window."""

        self.bgcolor = self.frame.cget('bg')
        self.instructions = Label(
            self.frame,
            text=
            ' Please provide the information of the following system properties:  '
        )

        self.blank1 = Label(self.frame, width=30)
        self.blank2 = Label(self.frame, width=20)
        self.blank3 = Label(self.frame, width=15)
        self.blank4 = Label(self.frame, width=20)

        self.headings1 = Label(self.frame, text='Parameter')
        self.headings2 = Label(self.frame, text='Value')
        self.headings3 = Label(self.frame, text='Units')

        self.advlabel = Label(self.frame, text='Upwelling groundwater flow')
        self.advwidget = OptionMenu(self.frame,
                                    self.adv,
                                    *self.advs,
                                    command=self.updatesystem)
        self.Vdarlabel = Label(self.frame, text='Darcy velocity:')
        self.Vtidallabel = Label(self.frame,
                                 text='Oscillation maximum velocity:')
        self.ptidallabel = Label(self.frame, text='Oscillation period:')
        self.Vdarwidget = Entry(self.frame,
                                width=10,
                                textvariable=self.Vdar,
                                justify='center')
        self.Vtidalwidget = Entry(self.frame,
                                  width=10,
                                  textvariable=self.Vtidal,
                                  justify='center')
        self.ptidalwidget = Entry(self.frame,
                                  width=10,
                                  textvariable=self.ptidal,
                                  justify='center')
        self.Vdarunits = Label(self.frame,
                               text=self.lengthunit + '/' + self.timeunit)
        self.Vtidalunits = Label(self.frame,
                                 text=self.lengthunit + '/' + self.timeunit)
        self.ptidalunits = Label(self.frame, text=self.timeunit)

        self.biolabel = Label(self.frame, text='Modeling bioturbation')
        self.biowidget = OptionMenu(self.frame,
                                    self.bio,
                                    *self.bios,
                                    command=self.updatesystem)
        self.hbiolabel = Label(self.frame, text='Bioturbation depth:')
        self.sigmalabel = Label(self.frame, text='Gaussian model coefficient:')
        self.Dbioplabel = Label(self.frame,
                                text='Particle biodiffusion coefficient:')
        self.Dbiopwlabel = Label(self.frame,
                                 text='Pore water biodiffusion coefficient:')
        self.hbiowidget = Entry(self.frame,
                                width=10,
                                textvariable=self.hbio,
                                justify='center')
        self.sigmawidget = Entry(self.frame,
                                 width=10,
                                 textvariable=self.sigma,
                                 justify='center')
        self.Dbiopwidget = Entry(self.frame,
                                 width=10,
                                 textvariable=self.Dbiop,
                                 justify='center')
        self.Dbiopwwidget = Entry(self.frame,
                                  width=10,
                                  textvariable=self.Dbiopw,
                                  justify='center')
        self.hbiounits = Label(self.frame, text=self.lengthunit)
        self.Dbiopunits = Label(self.frame, text=self.diffunits[1])
        self.Dbiopwunits = Label(self.frame, text=self.diffunits[1])

        self.conlabel = Label(self.frame, text='Modeling consolidation')
        self.conwidget = OptionMenu(self.frame,
                                    self.con,
                                    *self.cons,
                                    command=self.updatesystem)
        self.hconlabel = Label(self.frame, text='Maximum consolidation depth:')
        self.t90label = Label(self.frame, text='Time to 90% consolidation:')
        self.hconwidget = Entry(self.frame,
                                width=10,
                                textvariable=self.hcon,
                                justify='center')
        self.t90widget = Entry(self.frame,
                               width=10,
                               textvariable=self.t90,
                               justify='center')
        self.hconunits = Label(self.frame, text=self.lengthunit)
        self.t90units = Label(self.frame, text=self.timeunit)

        self.advwidget.config(width=20)
        self.biowidget.config(width=20)
        self.conwidget.config(width=20)

        #self.okbutton   = Button(self.frame, text = 'OK',         command = self.OK,       width = 20)

        #show the widgets on the grid
        self.instructions.grid(row=0,
                               column=0,
                               columnspan=3,
                               padx=6,
                               sticky='W')
        self.blank1.grid(row=1, column=0)
        self.blank2.grid(row=1, column=1)
        self.blank3.grid(row=1, column=2)

        self.row = 3

        self.focusbutton = None

        self.updatesystem()

    def updatesystem(self, event=None):

        try:
            self.advlabel.grid_forget()
            self.advwidget.grid_forget()
            self.Vdarlabel.grid_forget()
            self.Vdarwidget.grid_forget()
            self.Vdarunits.grid_forget()
            self.Vtidallabel.grid_forget()
            self.Vtidalwidget.grid_forget()
            self.Vtidalunits.grid_forget()
            self.ptidallabel.grid_forget()
            self.ptidalwidget.grid_forget()
            self.ptidalunits.grid_forget()

            self.biolabel.grid_forget()
            self.biowidget.grid_forget()
            self.sigmalabel.grid_forget()
            self.sigmawidget.grid_forget()
            self.hbiolabel.grid_forget()
            self.hbiowidget.grid_forget()
            self.hbiounits.grid_forget()
            self.Dbioplabel.grid_forget()
            self.Dbiopwidget.grid_forget()
            self.Dbiopunits.grid_forget()
            self.Dbiopwlabel.grid_forget()
            self.Dbiopwwidget.grid_forget()
            self.Dbiopwunits.grid_forget()

            self.conlabel.grid_forget()
            self.conwidget.grid_forget()
            self.hconlabel.grid_forget()
            self.hconwidget.grid_forget()
            self.hconunits.grid_forget()
            self.t90label.grid_forget()
            self.t90widget.grid_forget()
            self.t90units.grid_forget()

        except:
            pass

        row = self.row

        self.advlabel.grid(row=row, column=0, sticky='E', padx=4)
        self.advwidget.grid(row=row,
                            column=1,
                            sticky='W',
                            padx=4,
                            columnspan=2)

        row = row + 1

        if self.adv.get() == self.advs[1] or self.adv.get() == self.advs[2]:

            self.Vdarlabel.grid(row=row, column=0, sticky='E', padx=4)
            self.Vdarwidget.grid(row=row, column=1, pady=1)
            self.Vdarunits.grid(row=row, column=2, sticky='W', padx=4)

            row = row + 1

        if self.adv.get() == self.advs[2]:

            self.Vtidallabel.grid(row=row, column=0, sticky='E', padx=4)
            self.Vtidalwidget.grid(row=row, column=1, pady=1)
            self.Vtidalunits.grid(row=row, column=2, sticky='W', padx=4)

            row = row + 1

            self.ptidallabel.grid(row=row, column=0, sticky='E', padx=4)
            self.ptidalwidget.grid(row=row, column=1, pady=1)
            self.ptidalunits.grid(row=row, column=2, sticky='W', padx=4)

            row = row + 1

        self.biolabel.grid(row=row, column=0, sticky='E', padx=4)
        self.biowidget.grid(row=row,
                            column=1,
                            sticky='W',
                            padx=4,
                            columnspan=2)

        row = row + 1

        if self.bio.get() == self.bios[1]:

            self.hbiolabel.grid(row=row, column=0, sticky='E', padx=4)
            self.hbiowidget.grid(row=row, column=1, pady=1)
            self.hbiounits.grid(row=row, column=2, sticky='W', padx=4)

            row = row + 1

        if self.bio.get() == self.bios[2]:

            self.sigmalabel.grid(row=row, column=0, sticky='E', padx=4)
            self.sigmawidget.grid(row=row, column=1, pady=1)
            self.hbiounits.grid(row=row, column=2, sticky='W', padx=4)

            row = row + 1

        if self.bio.get() == self.bios[1] or self.bio.get() == self.bios[2]:

            self.Dbioplabel.grid(row=row, column=0, sticky='E', padx=4)
            self.Dbiopwidget.grid(row=row, column=1, pady=1)
            self.Dbiopunits.grid(row=row, column=2, sticky='W', padx=4)

            row = row + 1

            self.Dbiopwlabel.grid(row=row, column=0, sticky='E', padx=4)
            self.Dbiopwwidget.grid(row=row, column=1, pady=1)
            self.Dbiopwunits.grid(row=row, column=2, sticky='W', padx=4)

            row = row + 1

        self.conlabel.grid(row=row, column=0, sticky='E', padx=4)
        self.conwidget.grid(row=row,
                            column=1,
                            sticky='W',
                            padx=4,
                            columnspan=2)

        row = row + 1

        if self.con.get() == self.cons[1]:
            self.hconlabel.grid(row=row, column=0, sticky='E', padx=4)
            self.hconwidget.grid(row=row, column=1, pady=1)
            self.hconunits.grid(row=row, column=2, sticky='W', padx=4)

            row = row + 1

            self.t90label.grid(row=row, column=0, sticky='E', padx=4)
            self.t90widget.grid(row=row, column=1, pady=1)
            self.t90units.grid(row=row, column=2, sticky='W', padx=4)

            row = row + 1

        self.blank4.grid(row=row)

        row = row + 1

        self.focusbutton = None

        self.master.geometry()

    def error_check(self, event=None):
        """Finish and move on.  Checks that the number chemicals are less than the
        total number of chemicals in database."""

        error = 0
        #if self.bio.get() == self.bios[1] and self.hbio.get() > self.toplayer_h: error = 1

        return error

    def warning(self):

        tkmb.showerror(
            title=self.version,
            message='The bioturbation ' +
            'depth (%.1f cm) ' % self.hbio.get() + 'cannot exceed the ' +
            'thickness of the top layer (%.1f). ' % self.toplayer_h +
            'Automatically increasing ' +
            'the thickness of the top layer to the bioturbation ' + 'depth.')
        self.hbio.set(self.toplayer_h)
        self.focusbutton = None
        self.master.tk.lift()
コード例 #7
0
class DPSinterface:
    """
    DSPinterface is a Tk graphical interface to drive a DPS power supplier.

    """
    def __init__(self, root):
        """
        Create a DSP interface instance.

        :param root: is the Tk() interface where the DPS will be drawedstring with the prot name i.e. /dev/ttyUSB0 or COM5 for Windows
        :returns: a new instance of DPS graphical interface

        """

        self.root = root
        root.title("DPS power supplier interface")
        root.protocol("WM_DELETE_WINDOW", self.wnwcmdclose)

        self.dps = None
        self.poller = None
        self.waver = None
        self.strtme = time()
        self.dpsfwave = None
        self.maxoutv = 5
        self.maxoutc = 5

        menubar = Menu(root)

        filemenu = Menu(menubar, tearoff=0)
        filemenu.add_command(label="Exit", command=self.wnwcmdclose)
        menubar.add_cascade(label="File", menu=filemenu)

        scopemenu = Menu(menubar, tearoff=0)
        scopemenu.add_command(label="Load sampled points...",
                              command=self.mnucmdloadsmppts)
        scopemenu.add_command(label="Save sampled points as...",
                              command=self.mnucmdsavesmppts)
        menubar.add_cascade(label="Scope", menu=scopemenu)

        wavemenu = Menu(menubar, tearoff=0)
        wavemenu.add_command(label="New wave", command=self.mnucmdnewwve)
        wavemenu.add_command(label="Load wave...", command=self.mnucmdloadwve)
        wavemenu.add_command(label="Edit wave...", command=self.mnucmdedtwve)
        wavemenu.add_command(label="Save wave as...",
                             command=self.mnucmdsavewve)
        menubar.add_cascade(label="Wave", menu=wavemenu)

        memmenu = Menu(menubar, tearoff=0)
        memmenu.add_command(label="Edit memories...",
                            command=self.mnucmdedtmem)
        menubar.add_cascade(label="Memory", menu=memmenu)

        helpmenu = Menu(menubar, tearoff=0)
        helpmenu.add_command(label="Help...", command=self.mnucmdhelp)
        helpmenu.add_command(label="About...", command=self.mnucmdabout)
        menubar.add_cascade(label="Help", menu=helpmenu)

        root.config(menu=menubar)

        row = 0
        col = 0
        rowspan = 1
        colspan = 1
        insertlabelrow(root, row, col, ("Serial: ", None, "Addr,Baud: "), E)
        col += colspan
        self.svardpsport = StringVar()
        self.svardpsport.set('/dev/ttyUSB0')
        self.entryserport = Entry(root,
                                  textvariable=self.svardpsport,
                                  width=ENTRYWIDTH,
                                  justify='right')
        self.entryserport.grid(row=row, column=col, sticky=W)
        col += colspan
        col += colspan
        self.svardpsaddbrt = StringVar()
        self.svardpsaddbrt.set('1, 9600')
        self.entrydpsadd = Entry(root,
                                 textvariable=self.svardpsaddbrt,
                                 width=ENTRYWIDTH,
                                 justify='right')
        self.entrydpsadd.grid(row=row, column=col, sticky=W)
        col += colspan
        colspan = 2
        self.ivarconctd = IntVar()
        self.ivarconctd.set(0)
        Checkbutton(root,
                    variable=self.ivarconctd,
                    text='Connect',
                    command=self.butcmdconnect).grid(row=row,
                                                     column=col,
                                                     columnspan=colspan,
                                                     sticky=E + W)

        row += rowspan
        col = 0
        colspan = 1
        Separator(root, orient='horizontal').grid(row=row,
                                                  columnspan=8,
                                                  sticky=E + W,
                                                  pady=8)

        row += rowspan
        rowspan = 1
        colspan = 2
        col = 0
        self.ivarbrghtnes = IntVar()
        s = Scale(root,
                  label='Brightness',
                  variable=self.ivarbrghtnes,
                  from_=0,
                  to=5,
                  resolution=1,
                  orient="horizontal")
        s.bind("<ButtonRelease-1>", self.sclbndbrghtnss)
        s.grid(row=row, column=col, columnspan=colspan, sticky=E + W)
        col += colspan
        colspan = 1
        Label(root, text="Model: ").grid(row=row, column=col, sticky=E)
        col += colspan
        self.ivarmodel = IntVar()
        Entry(root,
              textvariable=self.ivarmodel,
              state="readonly",
              width=ENTRYWIDTH,
              justify='right').grid(row=row, column=col, sticky=W)
        col += colspan
        colspan = 2
        self.ivarsetmem = IntVar()
        s = Scale(root,
                  label='Mem Recall',
                  variable=self.ivarsetmem,
                  from_=1,
                  to=9,
                  resolution=1,
                  orient="horizontal")
        s.bind("<ButtonRelease-1>", self.sclbndmemory)
        s.grid(row=row, column=col, columnspan=colspan, sticky=E + W)

        row += rowspan
        colspan = 1
        col = 0
        insertlabelrow(
            root, row, col,
            (("Vinp [V]: ", VCOL), None, "Out Mode: ", None, "Protection: "),
            E)
        self.dvarvinp = DoubleVar()
        self.svarwrmde = StringVar()
        self.setworkmode(0)
        self.svarprot = StringVar()
        self.setprotection(0)
        insertentryrow(
            root, row, col,
            (None, self.dvarvinp, None, self.svarwrmde, None, self.svarprot),
            'right', W, 'readonly')

        colspan = 1
        row += rowspan
        col = 0
        insertlabelrow(root, row, col,
                       (("Vmax [V]: ", VCOL), None, ("Cmax [A]: ", CCOL), None,
                        ("Pmax [W]: ", PCOL)), E)
        self.dvarvmaxm0 = DoubleVar()
        self.dvarcmaxm0 = DoubleVar()
        self.dvarpmaxm0 = DoubleVar()
        entries = insertentryrow(root, row, col,
                                 (None, self.dvarvmaxm0, None, self.dvarcmaxm0,
                                  None, self.dvarpmaxm0), 'right', W)
        for e, f in zip(entries,
                        (self.entbndvmax, self.entbndcmax, self.entbndpmax)):
            e.bind('<FocusOut>', f)
            e.bind('<Return>', f)

        row += rowspan
        col = 0
        insertlabelrow(root, row, col,
                       (("Vout [V]: ", VCOL), None, ("Cout [A]: ", CCOL), None,
                        ("Pout [W]: ", PCOL)), E)
        self.dvarvout = DoubleVar()
        self.dvarcout = DoubleVar()
        self.dvarpout = DoubleVar()
        insertentryrow(
            root, row, col,
            (None, self.dvarvout, None, self.dvarcout, None, self.dvarpout),
            'right', W, 'readonly')

        row += rowspan
        col = 0
        self.scope = Scope(root, [], row, col)

        row += 9
        col = 4
        Label(root, text="Rte[s/Sa]: ").grid(row=row, column=col, sticky=E)
        col += colspan
        self.dvarsecsmp = DoubleVar()
        self.dvarsecsmp.set(self.scope.sampletime())
        e = Entry(root,
                  textvariable=self.dvarsecsmp,
                  width=ENTRYWIDTH,
                  justify='right').grid(row=row, column=col, sticky=W)

        row += rowspan
        col = 0
        colspan = 2
        self.ivaracquire = IntVar()
        self.ivaracquire.set(0)
        Checkbutton(root,
                    variable=self.ivaracquire,
                    text='Run Acquisition',
                    command=self.butcmdacquire).grid(row=row,
                                                     column=col,
                                                     columnspan=2,
                                                     sticky=E + W)
        col += colspan
        self.ivarkeylock = IntVar()
        self.ivarkeylock.set(0)
        Checkbutton(root,
                    variable=self.ivarkeylock,
                    text="Key Lock",
                    command=self.butcmdkeylock).grid(row=row,
                                                     column=col,
                                                     sticky=E + W,
                                                     columnspan=colspan)
        col += colspan
        self.ivaroutenab = IntVar()
        self.ivaroutenab.set(0)
        Checkbutton(root,
                    variable=self.ivaroutenab,
                    text="Output Enable",
                    command=self.butcmdoutenable).grid(row=row,
                                                       column=col,
                                                       sticky=E + W,
                                                       columnspan=colspan)

        row += rowspan
        col = 0
        rowspan = 1
        colspan = 3
        self.dvarvscale = DoubleVar()
        self.voltscale = Scale(root,
                               label='Vset [V]',
                               foreground=VCOL,
                               variable=self.dvarvscale,
                               from_=0,
                               to=self.maxoutv,
                               resolution=1,
                               orient="horizontal")  #, label='Vset[V]'
        self.voltscale.bind("<ButtonRelease-1>", self.sclbndvolt)
        self.voltscale.grid(row=row,
                            column=col,
                            columnspan=colspan,
                            sticky=E + W)
        col += colspan
        self.dvarcscale = DoubleVar()
        self.curntscale = Scale(root,
                                label='Cset[A]',
                                foreground=CCOL,
                                variable=self.dvarcscale,
                                from_=0,
                                to=self.maxoutc,
                                resolution=1,
                                orient="horizontal")  #,label='Cset[A]'
        self.curntscale.bind("<ButtonRelease-1>", self.sclbndcrnt)
        self.curntscale.grid(row=row,
                             column=col,
                             columnspan=colspan,
                             sticky=E + W)

        row += rowspan
        col = 0
        self.dvarvscalef = DoubleVar()
        sc = Scale(root,
                   foreground=VCOL,
                   variable=self.dvarvscalef,
                   from_=0,
                   to=0.99,
                   resolution=0.01,
                   orient="horizontal")
        sc.bind("<ButtonRelease-1>", self.sclbndvolt)
        sc.grid(row=row, column=col, columnspan=colspan, sticky=E + W)
        col += colspan
        self.dvarcscalef = DoubleVar()
        sc = Scale(root,
                   foreground=CCOL,
                   variable=self.dvarcscalef,
                   from_=0,
                   to=0.99,
                   resolution=0.01,
                   orient="horizontal")
        sc.bind("<ButtonRelease-1>", self.sclbndcrnt)
        sc.grid(row=row, column=col, columnspan=colspan, sticky=E + W)

        row += rowspan
        col = 0
        colspan = 1
        Separator(root, orient='horizontal').grid(row=row,
                                                  columnspan=6,
                                                  sticky=E + W,
                                                  pady=8)

        row += rowspan
        colspan = 1
        col = 0
        Label(root, text="Waveform: ").grid(row=row, column=col, sticky=E)
        col += colspan
        colspan = 2
        self.svarwave = StringVar()
        Entry(root,
              textvariable=self.svarwave,
              width=ENTRYWIDTH,
              justify='right',
              state='readonly').grid(row=row,
                                     column=col,
                                     columnspan=colspan,
                                     sticky=E + W)
        col += colspan
        colspan = 1
        self.ivarplaywv = IntVar()
        self.ivarplaywv.set(0)
        Checkbutton(root,
                    variable=self.ivarplaywv,
                    text='Play',
                    command=self.butcmdplaywave).grid(row=row,
                                                      column=col,
                                                      sticky=E + W)
        col += colspan
        self.ivarpausewv = IntVar()
        self.ivarpausewv.set(0)
        Checkbutton(root,
                    variable=self.ivarpausewv,
                    text='Pause',
                    command=self.butcmdpausewave).grid(row=row,
                                                       column=col,
                                                       sticky=E + W)
        col += colspan
        self.ivarloopwv = IntVar()
        self.ivarloopwv.set(0)
        Checkbutton(root, variable=self.ivarloopwv,
                    text='Loop').grid(row=row, column=col, sticky=E + W)

        self.scope.update()
        self.scope.redraw()

    def sclbndvolt(self, event):
        """
        Voltage scale bind command to set the voltage on the DSP.

        :param event: the event describing what changed

        """
        if self.isconnected():
            self.dps.set(['vset'], [self.getvscale()])

    def sclbndcrnt(self, event):
        """
        Current scale bind command to set the current on the DSP.

        :param event: the event describing what changed

        """
        if self.isconnected():
            self.dps.set(['cset'], [self.getcscale()])

    def sclbndmemory(self, event):
        """
        Memory-set bind command to set the memory on the DSP.

        :param event: the event describing what changed

        """
        if self.isconnected():
            m = self.ivarsetmem.get()
            self.dps.set(['mset'], [m])
            self.updatefields(True)

    def sclbndbrghtnss(self, event):
        """
        Brightness bind command to set the brightness on the DSP.

        :param event: the event describing what changed

        """
        if self.isconnected():
            b = self.ivarbrghtnes.get()
            self.dps.set(['brght'], [b])

    def mnucmdnewwve(self):
        """
        New wave menu command to initialize a new wave.

        """
        self.dpsfwave = Dpsfile()
        self.svarwave.set('unnamed')

    def mnucmdloadwve(self):
        """
        Load wave menu command to load a wave file.

        """
        fname = tkFileDialog.askopenfilename(initialdir=".",
                                             title="Select wave file to load",
                                             filetypes=(("dps files", "*.dps"),
                                                        ("all files", "*.*")))
        if fname:
            self.svarwave.set(os.path.basename(fname))
            self.dpsfwave = Dpsfile()
            self.dpsfwave.load(fname)

    def mnucmdedtwve(self):
        """
        Edit wave menu command to open the edit wave window.

        """
        if self.dpsfwave is not None:
            Wveinterface(self.root, self.dpsfwave.getpoints())
        else:
            tkMessageBox.showinfo('No wave loaded',
                                  'Load or create a new wave file to modify')

    def mnucmdsavewve(self):
        """
        Save wave menu command to save the current wave in memory.

        """
        if self.dpsfwave is not None:
            fname = tkFileDialog.asksaveasfilename(
                initialdir=".",
                title="Select wave file to save",
                filetypes=(("dps files", "*.dps"), ("all files", "*.*")))
            if fname:
                self.dpsfwave.save(fname)
                self.svarwave.set(os.path.basename(fname))
        else:
            tkMessageBox.showinfo('No wave in memory',
                                  'Load or create a wave file to modify')

    def mnucmdloadsmppts(self):
        """
        Load sampled points menu command to load in the graphical view sampled before.

        """
        fname = tkFileDialog.askopenfilename(initialdir=".",
                                             title="Select data file to load",
                                             filetypes=(("dps files", "*.dps"),
                                                        ("all files", "*.*")))
        if fname:
            self.scope.load(fname)

    def mnucmdsavesmppts(self):
        """
        Save sampled points menu command to save the last points sampled showed in the graphical view.

        """
        fname = tkFileDialog.asksaveasfilename(
            initialdir=".",
            title="Select data file to save",
            filetypes=(("dps files", "*.dps"), ("all files", "*.*")))
        if fname:
            self.scope.save(fname)

    def mnucmdedtmem(self):
        """
        Memory menu command to edit the values of preset memories on DSP.

        """
        if self.isconnected():
            Meminterface(self.root, self.dps, self.updatefields)

    def mnucmdabout(self):
        """
        About menu command to show the window with program information.

        """
        Txtinterface(
            self.root, 'About', """DPS interface is designed by Simone Pernice

That project was born as a textual driver to interface any DPS device.
After the driver I made also a graphical interface to manage DPS devices.

This project was born because I was not able to find any Linux
application to manage DPS devices via USB.

For question email to me: [email protected]
Version {} relesed on {}
First release on 3rd February 2019 Turin Italy
DPS interface is under licence {}

If you like this program please make a donation with PayPal to [email protected]"""
            .format(__version__, __date__, __license__))

    def mnucmdhelp(self):
        """
        Help menu command to show basic help on usage.

        """
        Txtinterface(
            self.root, 'Help',
            """This is an interface to remote controll a power supplier of DPS series.
The white fields can be edited, the gray are read only.
To connect to DPS power supplier first link it to the PC through an USB cable.
The data required to connect is on the first row of the graphical interface.
Write the serial address on the first field (COMxx for Windows or 
/dev/ttyUSBx for Linux).
Address and baudrate do not require update because they are the default 
for DPS power supplier. Turn on DPS with up key pressed to change those values.
Press 'Connect' check button and if the device is present it is linked. 
Press again the same check button to disconnect the DPS.
Once the link to DPS is in place all the data on the interface are updated and 
on the DPS the keylock is set. 
The second block of graphical interface contains all data about DPS.
The brightness set which can be changed through the scale regulation.
The model number. The memory from which recall the preset parameters.
The input voltage, the output mode cv (constant voltage) or cc (constant current).
The protection mode: none (no protection triggered), ovp (over voltage protection), 
ocp (over current protection), opp (over power protection).
The maximum voltage, current and power to provide before triggering the 
self protection.
The next row contains output voltage, current and power in textual form.
A time diagram of the DPS output voltage, current and power is avaiable. 
It is possible to play with the mouse on that screen:
- wheel press to fit in the screen all the enabled drawings
- wheel to zoom in time
- shift+wheel to zoom on Y for the highlighted curves
- ctrl+wheel to change the enabled curves
- left button drag to move the highlighted curve
The same mouse functions are available in the fields below the diagram:
- voltage per division, current per division and watt per division
- zero position for voltage, current and power
- check button to view voltage, current and power
- time: second per divisions and zero position for time
The sample time is used for the acquisition. DPS is quite slot, the minimum read
time is around 1 second. The suggested rate is to have a sample for displayed pixel.
The next buttons are:
- Run acquisition: starts a thread that read the DPS status, update the interface 
fields as well as the time diagram. 
The acquisition points can be saved and loaded to be showed lated with menu 
commands on DPS scope load/save. They can be also edited through the
wave edit window and played.
- Key lock: set or reset the DPS key lock. It should be on in order to have faster
communication. If key lock is on less fields of DPS are read since user can 
change them only through the PC interface.
- Output enable to enable the DPS output
Eventually there are the voltage and current scale. Thery are split in two:
- the first  is for coarse (1 unit/step) adjustment the unit of voltage/current 
- the second is for fine (0.01 unit/step) adjustament of voltage/current
On the last block of interface there is a waveform field showing the wave loaded.
Wave is a set of required output voltage and current at give timings. It is possible
play and pause it through the respective commands of the interface. If loop is
set when the wave play is completed it restarts.
The acquisition may slow the wave player, use low acquisition sample time 
to avoid delays.""")

    def butcmdconnect(self):
        """
        Connect check button command to connect to the DSP.

        It reads: serial port address, DPS address and serial speed from other interface fields.
        If it is capable to link to the DPS: 
        - the maximum voltage and current are read and scale maximums set accordingly
        - the DPS current data are read and set accordingly in the interface
        - the localDPS interface is locked so that the user cannot change them but has to go through the graphical interface
         if the DPS is locked the polling is faster because less data needs to be read from DPS 
        - the input fields are disabled
        """
        if self.ivarconctd.get():
            try:
                flds = self.svardpsaddbrt.get().split(',')
                if len(flds) > 0:
                    ch = int(flds[0])
                else:
                    ch = 1
                if len(flds) > 1:
                    br = int(flds[1])
                else:
                    br = 9600
                self.dps = DPSdriver(self.svardpsport.get(), ch, br)
            except Exception as e:
                tkMessageBox.showerror('Error', 'Cannot connect: ' + str(e))
                self.ivarconctd.set(0)
                self.dps = None
                return

            m = self.dps.get(['model'])
            m = m[0]
            self.ivarmodel.set(m)

            to = m / 100
            self.voltscale.config(to=to)
            self.maxoutv = to

            to = m % 100
            self.curntscale.config(to=to)
            self.maxoutv = to

            self.scope.resetpoints()

            self.ivarkeylock.set(1)
            self.butcmdkeylock()
            self.updatefields(True)

            self.entryserport.config(state='readonly')
            self.entrydpsadd.config(state='readonly')
        else:
            # Stop polling
            self.ivaracquire.set(0)
            if self.poller:
                self.poller.wake()
                time.sleep(1.)  # Wait to be sure the thread exits

            # Stop waveform generation
            self.ivarplaywv.set(0)
            if self.waver:
                self.waver.wake()
                time.sleep(1.)  # Wait to be sure the thread exits

            self.dps = None

            self.entryserport.config(state=NORMAL)
            self.entrydpsadd.config(state=NORMAL)

    def butcmdacquire(self):
        """
        Acquire check button command to manage the acquisition thread to read the DSP data.
        If the button is not selected the thread is lunched.
        If the button is selected the thread is stopped.

        """
        if self.ivaracquire.get():
            if not self.isconnected():
                self.ivaracquire.set(0)
                return
            self.scope.resetpoints()
            self.strtme = time()
            self.poller = Poller(self.ivaracquire, self.dvarsecsmp,
                                 self.updatefields)
        else:
            self.poller.wake()

    def butcmdkeylock(self):
        """
        Key lock button command to enable or disable the key lock on DPS remote interface.

        """
        if self.isconnected():
            self.dps.set(['lock'], [self.ivarkeylock.get()])
        else:
            self.ivarkeylock.set(0)

    def butcmdoutenable(self):
        """
        DPS output button command to enable or disable the DPS output power.

        """
        if self.isconnected():
            self.dps.set(['onoff'], [self.ivaroutenab.get()])
        else:
            self.ivaroutenab.set(0)

    def butcmdplaywave(self):
        """
        Wave generator  check button command to manage the wave generation thread to make a waveform on the DSP.
        If the button is not selected the thread is lunched.
        If the button is selected the thread is stopped.

        """
        if self.ivarplaywv.get():
            if not self.isconnected():
                self.ivarplaywv.set(0)
                return
            if not self.dpsfwave:
                tkMessageBox.showinfo('No wave in memory',
                                      'Load or create a wave file to modify')
                self.ivarplaywv.set(0)
                return
            if not self.ivaroutenab.get():
                self.ivaroutenab.set(1)
                self.butcmdoutenable()
            self.waver = Waver(self.setvcdps, self.ivarplaywv,
                               self.ivarpausewv, self.ivarloopwv,
                               self.dpsfwave.getpoints())
        else:
            self.waver.wake()

    def butcmdpausewave(self):
        """
        Wave generator  pause check button command to temporary pause the wave generations.

        """
        self.waver.wake()

    def wnwcmdclose(self):
        """
        DPS main window close. Before exiting the supplier is disconnected the external supplier.

        """
        if self.ivarconctd.get():
            self.ivarconctd.set(0)
            self.butcmdconnect()

        self.root.destroy()

    def entbndvmax(self, event):
        """
        Maximum voltage entry bind to set the protection maximum ouput voltage of the DSP.

        :param event: the event describing what changed

        """
        if self.isconnected():
            if self.dvarvmaxm0.get() > self.maxoutv * PROTEXCEED:
                self.dvarvmaxm0.set(self.maxoutv * PROTEXCEED)
            elif self.dvarvmaxm0.get() < 0.:
                self.dvarvmaxm0.set(0.)
            self.dps.set(['m0ovp'], [self.dvarvmaxm0.get()])

    def entbndcmax(self, event):
        """
        Maximum current entry bind to set the protection maximum output curret of the DSP.

        :param event: the event describing what changed

        """
        if self.isconnected():
            if self.dvarcmaxm0.get() > self.maxoutc * PROTEXCEED:
                self.dvarcmaxm0.set(self.maxoutc * PROTEXCEED)
            elif self.dvarcmaxm0.get() < 0.:
                self.dvarcmaxm0.set(0.)
            self.dps.set(['m0ocp'], [self.dvarcmaxm0.get()])

    def entbndpmax(self, event):
        """
        Maximum power entry bind to set the protection maximum output power of the DSP.

        :param event: the event describing what changed

        """
        if self.isconnected():
            if self.dvarpmaxm0.get(
            ) > self.maxoutv * self.maxoutc * PROTEXCEED * PROTEXCEED:
                self.dvarpmaxm0.set(self.maxoutv * self.maxoutc * PROTEXCEED *
                                    PROTEXCEED)
            elif self.dvarpmaxm0.get() < 0.:
                self.dvarcmaxm0.set(0.)
            self.dps.set(['m0opp'], [self.dvarpmaxm0.get()])

    def setvcdps(self, v, c):
        """
        Set the DPS output voltage and current moving their scales accordingly.

        :param v: the required voltage, if negative it is not changed
        :param c: the required current, if negative it is not changed

        """
        if v >= 0:
            if c >= 0:
                self.setvscale(v)
                self.setcscale(c)
                self.dps.set(['vset', 'cset'], [v, c])
            else:
                self.setvscale(v)
                self.dps.set(['vset'], [v])
        elif c >= 0:
            self.setcscale(c)
            self.dps.set(['cset'], [c])

    def isconnected(self):
        """
        Check if the DPS is connected, if not display a message.

        :returns: True if connected, False if not

        """
        if self.dps is None:
            tkMessageBox.showinfo('Not connected',
                                  'Enstablish a connection before')
            return False
        return True

    def setvscale(self, v):
        """
        Set the voltage scale, nothing is changed on the DPS.

        :param v: the voltage to set

        """
        if v > self.maxoutv: v = self.maxoutv
        elif v < 0: v = 0
        self.dvarvscale.set(int(v))
        self.dvarvscalef.set(round(v - int(v), 2))

    def getvscale(self):
        """
        Get the voltage scale set value.

        :returns: the voltage set

        """
        return self.dvarvscale.get() + self.dvarvscalef.get()

    def setcscale(self, c):
        """
        Set the current scale, nothing is changed on the DPS.

        :param c: the current to set

        """
        if c > self.maxoutc: c = self.maxoutc
        elif c < 0: c = 0
        self.dvarcscale.set(int(c))
        self.dvarcscalef.set(round(c - int(c), 2))

    def getcscale(self):
        """
        Get the current scale set value.

        :returns: the current set

        """
        return self.dvarcscale.get() + self.dvarcscalef.get()

    def updatefields(self, forcereadall=False):
        """
        Reads data stored in DPS and updates the interface fields accordingly. 
        
        In order to be as fast as possible, if keylock is enabled, reads only the fields that can change without uses access.
        If keylock is disabled all the fields are read because user may have changed something from the interface.

        :param forcereadall: if True read and update all the DPS fields regardless of the keylock status
        :returns: the point read. A point is made by (time, voltage, current, power)

        """
        if not forcereadall and self.ivarkeylock.get(
        ):  # If user keep locked fewer data are read, otherwise all
            data = self.dps.get(
                ['vout', 'cout', 'pout', 'vinp', 'lock', 'prot', 'cvcc'])
            self.dvarvout.set(data[0])
            self.dvarcout.set(data[1])
            self.dvarpout.set(data[2])
            self.dvarvinp.set(data[3])
            self.ivarkeylock.set(data[4])
            self.setprotection(data[5])
            self.setworkmode(data[6])
            vcp = data[0:3]

        else:  # All data is read
            data = self.dps.get([
                'vset', 'cset', 'vout', 'cout', 'pout', 'vinp', 'lock', 'prot',
                'cvcc', 'onoff', 'brght', 'mset', 'm0ovp', 'm0ocp', 'm0opp'
            ])
            self.setvscale(data[0])
            self.setcscale(data[1])
            self.dvarvout.set(data[2])
            self.dvarcout.set(data[3])
            self.dvarpout.set(data[4])
            self.dvarvinp.set(data[5])
            self.ivarkeylock.set(data[6])
            self.setprotection(data[7])
            self.setworkmode(data[8])
            self.ivaroutenab.set(data[9])
            self.ivarbrghtnes.set(data[10])
            self.ivarsetmem.set(data[11])
            self.dvarvmaxm0.set(data[12])
            self.dvarcmaxm0.set(data[13])
            self.dvarpmaxm0.set(data[14])
            vcp = data[2:5]

        vcp.insert(TPOS, time() - self.strtme)
        self.scope.addpoint(vcp)
        return vcp

    def setprotection(self, p):
        """
        Set the protection field with an user readable string explaining the DPS protection status.

        :param p: the protection statu returned by the DPS

        """
        self.svarprot.set({0: 'none', 1: 'ovp', 2: 'ocp', 3: 'opp'}[p])

    def setworkmode(self, wm):
        """
        Set the workmode field with an user readable string explaining the DPS work mode.

        :param wm: the working mode returned by the DPS

        """
        self.svarwrmde.set({0: 'cv', 1: 'cc'}[wm])
コード例 #8
0
ファイル: plotter.py プロジェクト: hadrianmontes/Plotter
class plotter(Frame):
    def __init__(self,mainframe):
        # Initiate t a blank figure
        self.figure_created=False  # A bool to represent if the figure was created or not
        self.mainframe=mainframe  # Asign a variable ofr the aminframe
        self.fig=Figure()  # This is a dummy figure that will be overwrited but it will grant the space for the real one
        self.canvas=FigureCanvasTkAgg(self.fig,master=mainframe)  # The canvas where all the plots will be made
        self.canvas.show()
        self.canvas.get_tk_widget().grid(column=5,row=0,rowspan=12,columnspan=4)  # Create the widget that will mange the pots

        # Create the number of columns and rows
        ttk.Label(mainframe,text="Set number of columns  ").grid(column=0,row=0)
        self.total_columns=IntVar()  # The variable that will store the number of total columns
        self.total_columns.set(1)  # We give it an initial value
        
        # Combobox with the Possible values for the total number of columns in the main figure
        self.box_total_columns=ttk.Combobox(mainframe,textvariable=self.total_columns,state="readonly",width=3)
        self.box_total_columns['values']=range(1,10)
        self.box_total_columns.grid(column=1,row=0)
        ttk.Label(mainframe,text="  Set number of rows  ").grid(column=2,row=0)  # The same taht with the columns but with the rows
        self.total_rows=IntVar()
        self.total_rows.set(1)
        self.box_total_rows=ttk.Combobox(mainframe,textvariable=self.total_rows,state="readonly",width=3)
        self.box_total_rows['values']=range(1,10)
        self.box_total_rows.grid(column=3,row=0)

        # Create the figure with the subplots
        ttk.Button(mainframe,command=self.init_program,text="Create").grid(column=4,row=0)  # This button will initiate all the features

        # The next lines will allow to change between the subplots
        ttk.Label(mainframe,text="Select Column  ").grid(column=0,row=1)
        self.current_column=IntVar()  # The variable storing the current Column

        # The next lines will setup the combobox for making this change
        self.box_current_column=ttk.Combobox(mainframe,textvariable=self.current_column,state="readonly",width=3)
        self.box_current_column["values"]=["--"]
        self.box_current_column.grid(column=1,row=1)
        ttk.Label(mainframe,text="  Select Row  ").grid(column=2,row=1)
        self.current_row=IntVar()  # The variable storing th current row
        self.box_current_row=ttk.Combobox(mainframe,textvariable=self.current_row,state="readonly",width=3)
        self.box_current_row["values"]=["--"]
        self.box_current_row.grid(column=3,row=1)
        self.box_current_row.bind("<<ComboboxSelected>>",self.change_subplot)  # Now we make that when you choose one column or a row
        self.box_current_column.bind("<<ComboboxSelected>>",self.change_subplot)  # the prgram will auto load the presets for that subplot

        # Open File
        ttk.Button(mainframe,text="Open file",command=self.open_file).grid(column=0,row=3)
        self.filename=StringVar()  # Variable to store the path to the data file
        self.entry_filename=ttk.Entry(mainframe,textvar=self.filename,width=30).grid(column=1,row=3,columnspan=3,sticky=E)

        # Options for the plot
        # The label for the legend
        self.label=StringVar()  # Variable storing the label
        ttk.Label(mainframe,text="Label").grid(column=0,row=4)
        ttk.Entry(mainframe,textvar=self.label,width=30).grid(column=1,row=4,columnspan=3,sticky=E)  # Entry to enter the label

        # Select The column where the data is stored in the file
        self.column_x=IntVar()  # This variable stores the column where the x data is in the file (0 is the first one)
        self.column_x.set(0)  # Give the variable an initial value
        self.column_y=IntVar()  # This variable stores the column where the y data is in the file (0 is the first one)
        self.column_y.set(1)  # Give the variable an initial value
        ttk.Label(text="Column for x data").grid(column=0,row=5)
        ttk.Entry(textvar=self.column_x,width=3).grid(column=1,row=5)
        ttk.Label(text="Column for y data").grid(column=2,row=5)
        ttk.Entry(textvar=self.column_y,width=3).grid(column=3,row=5)

        # Combobox for selecting the maker
        self.marker=StringVar()  # Variable storing the marker configuration
        self.marker.set("None")  # The preset value for the marker (no marker on the points)
        ttk.Label(mainframe,text="Marker").grid(column=0,row=6)
        combo_marker=ttk.Combobox(mainframe,textvariable=self.marker,state="readonly",width=5)
        combo_marker["values"]=["None",".",",","o","+","*",'x',"custom"]  # Since there is quite a lot of markers i only preload the
        combo_marker.grid(column=1,row=6)  # esentials, but with the option custom it's posible to use wichever matplotlib supports
        self.marker_custom=StringVar()  # Variable storing the marker type
        ttk.Entry(mainframe,textvar=self.marker_custom,width=5).grid(column=3,row=6,sticky=E)  # Entry for the option of the custom
        ttk.Button(mainframe,text="Custom Marker",command=self.set_custom_marker).grid(column=2,row=6)  # Button to aply the custom config

        # Combobox for the line Style
        self.linestyle=StringVar()  # Variable storing the linestyle configuration
        self.linestyle.set("-")  # Preset value for the linestyle (solid line)
        ttk.Label(mainframe,text="Line Style").grid(column=0,row=7)
        combo_linestyle=ttk.Combobox(mainframe,textvariable=self.linestyle,state="readonly",width=5)
        combo_linestyle["values"]=["None","-","--","-.",":"]  # Those are all the posible options for the linestyle
        combo_linestyle.grid(column=1,row=7)

        # Change the color, this is a test version for a quick need
        ttk.Label(mainframe,text="Color").grid(column=2,row=7)
        self.color=StringVar()
        ttk.Entry(textvar=self.color,width=5).grid(column=3,row=7)

        # Change the axis labels
        ttk.Label(mainframe,text="X axis Label").grid(column=0,row=8,columnspan=2)
        ttk.Label(mainframe,text="Y axis Label").grid(column=2,row=8,columnspan=2)
        self.x_label=StringVar()  # Variable storing the label for the x axis
        self.y_label=StringVar()  # Variable storing the label for the y axis
        ttk.Entry(mainframe,textvar=self.x_label).grid(column=0,row=9,columnspan=2)
        ttk.Entry(mainframe,textvar=self.y_label).grid(column=2,row=9,columnspan=2)
        ttk.Button(mainframe,text="Update Labels",command=self.update_labels).grid(column=4,row=8)  # This button aplies the labels in the axis
        # Limits for the x and y axes
        # Label with information
        ttk.Label(text="Minimun Value of the axis").grid(column=0,row=10,columnspan=2)
        ttk.Label(text="Minimun Value of the axis").grid(column=2,row=10,columnspan=2)
        self.xmin=DoubleVar()  # Variable with the value of the minimun x
        self.xmax=DoubleVar()  # Variable with the value of the maximun x
        self.ymin=DoubleVar()  # Variable with the value of the minimun y
        self.ymax=DoubleVar()  # Variable with the value of the maximun y
        ttk.Entry(mainframe,textvar=self.xmin).grid(column=0,row=11,columnspan=2)  # Entry for the minimal value
        ttk.Entry(mainframe,textvar=self.xmax).grid(column=2,row=11,columnspan=2)  # Entry for the maximun value
        ttk.Button(mainframe,text="x axis",command=self.change_x_axe).grid(column=4,row=11)  # Update x axe
        ttk.Entry(mainframe,textvar=self.ymin).grid(column=0,row=12,columnspan=2)  # The same for the y axis
        ttk.Entry(mainframe,textvar=self.ymax).grid(column=2,row=12,columnspan=2)
        ttk.Button(mainframe,text="y axis",command=self.change_y_axe).grid(column=4,row=12)

        # Make the plot
        ttk.Button(mainframe,text="Plot",command=self.make_plot).grid(column=0,row=13)
        # Save the figure
        ttk.Button(mainframe,text="Save",command=self.save_plot).grid(column=1,row=13)
        # Options of subplots
        # Reset one of the subplots
        ttk.Button(mainframe,text="Reset Subplot",command=self.reset_subplot).grid(column=4,row=1)
        #  Export a configuration file of the plots, this will allow to redo the figure
        ttk.Button(mainframe,text="Export",command=self.export_logfile).grid(column=2,row=13)
        #  Import a configuration file of the plots, this will allow to redo a previous exported figure using the logfile
        ttk.Button(mainframe,text="import",command=self.read_logfile).grid(column=3,row=13)
        #  Join the x axis
        ttk.Button(mainframe,text="join x axes",command=self.fix_x_ticks).grid(column=5,row=12)
        ttk.Button(mainframe,text="join y axes",command=self.fix_y_ticks).grid(column=6,row=12)
        ttk.Button(mainframe,text="join x and y axes",command=self.fix_x_and_y_ticks).grid(column=7,row=12)

    def init_program(self,*args):
        # This functions set all the presets for the program to work
        self.create_subplots()  # First we create the subplots required by self.total_rows/columns
        self.current_column.set(1)  # We set the focus on the
        self.current_row.set(1)  # (1,1) subplot (remember that we start to count in 1 ,not 0.
        self.change_subplot()  # Aply the changes in the focus of the subplot

    def create_subplots(self,*args):
        self.sharex=True
        self.sharey=False
        # This function will create the subplot axes, and the needed variables
        self.fig=Figure()  # This will replace the figure in the main routine (its necesary to change the size of the figure without closing
        # the program
        self.canvas=FigureCanvasTkAgg(self.fig,master=self.mainframe)  # We have to redefine the canvas
        self.canvas.show()
        self.canvas.get_tk_widget().grid(column=5,row=0,rowspan=12,columnspan=4)
        self.axes=[[]]  # This variable will store the axes of each subplots. Th axe os a subplot is what allow to make the plot in that subplot
        # self.fig.subplots_adjust(hspace=0.001)
        self.file_index=[[]]  # This will save wich data files were plotted in each subplot, its necessary in order to write the logfile
        self.label_index=[[]]  # This variable store the labels for making the logfile
        self.marker_index=[[]]  # This one store the marker for each data set
        self.linestyle_index=[[]]  # This one stores line style
        self.color_index=[[]]  # This one stores the color
        self.xcolumn_index=[[]]  # This one store the column of the file used as x data
        self.xlabel_index=[[]]  # This will save the labels used for the x axis
        self.ycolumn_index=[[]]  # This one store the column of the file used as y data
        self.ylabel_index=[[]]  # This will save the labels used for the y axis
        self.xlimit_index=[[]]
        self.ylimit_index=[[]]

        # Now we will append an axe inside self.axes for each subplot, it will be stored so it is accesible with [row][column]
        # We need to append also a list for each subplot in the other variables
        for j in range(self.total_columns.get()):  # First we make a lopp throw the first row
            self.axes[0].append(self.fig.add_subplot(self.total_rows.get(),self.total_columns.get(),j+1))  # Append an axe subplot in each column
            self.file_index[0].append([])  # Append a list for each column
            self.label_index[0].append([])
            self.marker_index[0].append([])
            self.linestyle_index[0].append([])
            self.color_index[0].append([])
            self.xcolumn_index[0].append([])
            self.ycolumn_index[0].append([])
            self.xlabel_index[0].append("")  # Append a blank string for each column
            self.ylabel_index[0].append("")  # Append a blank string for each column
            self.xlimit_index[0].append(["",""])
            self.ylimit_index[0].append(["",""])
        if (self.total_rows.get()-1)!=0:  # If we have more than one row we repeat the previous steps for each row
            for i in range(1,self.total_rows.get()):
                self.axes.append([])  # We need to append a list for each row here
                self.file_index.append([])  # also in this one
                self.label_index.append([])  # And this one
                self.marker_index.append([])  # and this one
                self.linestyle_index.append([])  # and this one
                self.xcolumn_index.append([])
                self.ycolumn_index.append([])
                self.color_index.append([])
                self.xlabel_index.append([])
                self.ylabel_index.append([])
                self.xlimit_index.append([])
                self.ylimit_index.append([])

                for j in range(self.total_columns.get()):  # The next steps are the same than in the previous loop
                    self.axes[i].append(self.fig.add_subplot(self.total_rows.get(),self.total_columns.get(),1+j+self.total_columns.get()*i))
                    self.file_index[i].append([])
                    self.label_index[i].append([])
                    self.marker_index[i].append([])
                    self.linestyle_index[i].append([])
                    self.color_index[i].append([])
                    self.xlabel_index[i].append("")
                    self.xcolumn_index[i].append([])
                    self.ycolumn_index[i].append([])
                    self.ylabel_index[i].append("")
                    self.xlimit_index[i].append(["",""])
                    self.ylimit_index[i].append(["",""])

        self.fig.tight_layout()  # We let maplotlib to optimize the space around the subplots
        # for i in range(self.total_rows.get()-1):
        #     self.axes[0][0].xaxis.set_visible(False)
        self.canvas.draw()  # We draw the changes, now the main figure is divided in the subplots
        self.box_current_column["values"]=range(1,self.total_columns.get()+1)  # Update the possible values in the combobox
        self.box_current_row["values"]=range(1,self.total_rows.get()+1)  # of the columns and rows to make the change posiible
        self.figure_created=True  # We save that we have created a figure with subplots

    def stick_x_axis(self,*args):
        # This function will create the subplot axes, and the needed variables
        self.fig=Figure()  # This will replace the figure in the main routine (its necesary to change the size of the figure without closing
        # the program
        self.canvas=FigureCanvasTkAgg(self.fig,master=self.mainframe)  # We have to redefine the canvas
        self.canvas.show()
        self.canvas.get_tk_widget().grid(column=5,row=0,rowspan=12)
        self.axes=[[]]  # This variable will store the axes of each subplots. Th axe os a subplot is what allow to make the plot in that subplot

        self.file_index=[[]]  # This will save wich data files were plotted in each subplot, its necessary in order to write the logfile
        self.label_index=[[]]  # This variable store the labels for making the logfile
        self.marker_index=[[]]  # This one store the marker for each data set
        self.linestyle_index=[[]]  # This one stores line style
        self.color_index=[[]]
        self.xcolumn_index=[[]]
        self.ycolumn_index=[[]]
        self.xlabel_index=[[]]  # This will save the labels used for the x axis
        self.ylabel_index=[[]]  # This will save the labels used for the y axis
        self.xlimit_index=[[]]
        self.ylimit_index=[[]]

        # Now we will append an axe inside self.axes for each subplot, it will be stored so it is accesible with [row][column]
        # We need to append also a list for each subplot in the other variables
        for j in range(self.total_columns.get()):  # First we make a lopp throw the first row
            self.axes[0].append(self.fig.add_subplot(self.total_rows.get(),self.total_columns.get(),j+1))  # Append an axe subplot in each column
            self.file_index[0].append([])  # Append a list for each column
            self.label_index[0].append([])
            self.marker_index[0].append([])
            self.linestyle_index[0].append([])
            self.color_index[0].append([])
            self.xcolumn_index[0].append([])
            self.ycolumn_index[0].append([])
            self.xlabel_index[0].append("")  # Append a blank string for each column
            self.ylabel_index[0].append("")  # Append a blank string for each column
            self.xlimit_index[0].append(["",""])
            self.ylimit_index[0].append(["",""])

        if (self.total_rows.get()-1)!=0:  # If we have more than one row we repeat the previous steps for each row
            for i in range(1,self.total_rows.get()):
                self.axes.append([])  # We need to append a list for each row here
                self.file_index.append([])  # also in this one
                self.label_index.append([])  # And this one
                self.marker_index.append([])  # and this one
                self.linestyle_index.append([])  # and this one
                self.color_index.append([])
                self.xcolumn_index.append([])
                self.ycolumn_index.append([])
                self.xlabel_index.append([])
                self.ylabel_index.append([])
                self.xlimit_index.append([])
                self.ylimit_index.append([])
                for j in range(self.total_columns.get()):  # The next steps are the same than in the previous loop
                    self.axes[i].append(self.fig.add_subplot(self.total_rows.get(),self.total_columns.get(),1+j+self.total_columns.get()*i))
                    self.file_index[i].append([])
                    self.label_index[i].append([])
                    self.marker_index[i].append([])
                    self.linestyle_index[i].append([])
                    self.color_index[i].append([])
                    self.xcolumn_index[i].append([])
                    self.ycolumn_index[i].append([])
                    self.xlabel_index[i].append("")
                    self.ylabel_index[i].append("")
                    self.xlimit_index[i].append(["",""])
                    self.ylimit_index[i].append(["",""])

        self.fig.tight_layout()  # We let maplotlib to optimize the space around the subplots
        self.canvas.draw()  # We draw the changes, now the main figure is divided in the subplots
        self.box_current_column["values"]=range(1,self.total_columns.get()+1)  # Update the possible values in the combobox
        self.box_current_row["values"]=range(1,self.total_rows.get()+1)  # of the columns and rows to make the change posiible
        self.figure_created=True  # We save that we have created a figure with subplots

    def fix_x_ticks(self,*args):
        # First set for the maximun of each column and set all the graphs to it
        for j in range(self.total_columns.get()):
            max=self.axes[0][j].xaxis.get_majorticklocs()[-1]
            min=self.axes[0][j].xaxis.get_majorticklocs()[0]
            for i in range(1,self.total_rows.get()):
                if (self.axes[i][j].xaxis.get_majorticklocs()[-1]>max):
                    max=self.axes[i][j].xaxis.get_majorticklocs()[-1]
                if self.axes[i][j].xaxis.get_majorticklocs()[0]<min:
                    min=self.axes[i][j].xaxis.get_majorticklocs()[0]
            for u in range(self.total_rows.get()):
                self.axes[i][j].set_xlim([min,max])
        for i in range(self.total_rows.get()-1):
            for j in range(self.total_columns.get()):
                # self.axes[i][j].xaxis.set_visible(False)
                self.axes[i][j].set_xticklabels('')
                self.axes[i+1][j].get_shared_x_axes().join(self.axes[0][j],self.axes[i+1][j])
        for j in range(self.total_columns.get()):
            label=(self.axes[0][j].yaxis.get_majorticklocs())
            self.axes[0][j].set_yticklabels(label)

        for i in range(1,self.total_rows.get()):
            for j in range(self.total_columns.get()):
                label=(self.axes[i][j].yaxis.get_majorticklocs())
                label=label[0:-1]
                self.axes[i][j].set_yticklabels(label)
        self.fig.tight_layout()
        self.fig.subplots_adjust(hspace=0.001)
        self.canvas.draw()

    def fix_y_ticks(self,*args):
        # First search for the maximun and minimun of y in each row:
        for i in range(self.total_rows.get()):
            max=self.axes[i][0].yaxis.get_majorticklocs()[-1]
            min=self.axes[i][0].yaxis.get_majorticklocs()[0]
            for j in range(1,self.total_columns.get()):
                if (self.axes[i][j].yaxis.get_majorticklocs()[-1]>max):
                    max=self.axes[i][j].yaxis.get_majorticklocs()[-1]
                if self.axes[i][j].yaxis.get_majorticklocs()[0]<min:
                    min=self.axes[i][j].yaxis.get_majorticklocs()[0]
            for j in range(self.total_columns.get()):
                self.axes[i][j].set_ylim([min,max])
        for i in range(self.total_rows.get()):
            for j in range(self.total_columns.get()-1):
                # self.axes[i][j].xaxis.set_visible(False)
                self.axes[i][j+1].get_shared_y_axes().join(self.axes[i][0],self.axes[i][j+1])
                self.axes[i][j+1].set_yticklabels('')
                self.axes[i][j+1].get_shared_y_axes().join(self.axes[i][0],self.axes[i][j+1])
        for i in range(self.total_rows.get()):
            label=(self.axes[i][-1].xaxis.get_majorticklocs())
            self.axes[i][0].set_xticklabels(label)

        for i in range(self.total_rows.get()):
            for j in range(self.total_columns.get()-1):
                label=(self.axes[i][j].xaxis.get_majorticklocs())
                label=label[0:-1]
                self.axes[i][j].set_xticklabels(label)
        self.fig.tight_layout()
        self.fig.subplots_adjust(wspace=0.001)
        self.canvas.draw()

    def fix_x_and_y_ticks(self,*args):
        # First set for the maximun of each column and set all the graphs to it
        for j in range(self.total_columns.get()):
            max=self.axes[0][j].xaxis.get_majorticklocs()[-1]
            min=self.axes[0][j].xaxis.get_majorticklocs()[0]
            for i in range(1,self.total_rows.get()):
                if (self.axes[i][j].xaxis.get_majorticklocs()[-1]>max):
                    max=self.axes[i][j].xaxis.get_majorticklocs()[-1]
                if self.axes[i][j].xaxis.get_majorticklocs()[0]<min:
                    min=self.axes[i][j].xaxis.get_majorticklocs()[0]
            for u in range(self.total_rows.get()):
                self.axes[i][j].set_xlim([min,max])
        for i in range(self.total_rows.get()-1):
            for j in range(self.total_columns.get()):
                # self.axes[i][j].xaxis.set_visible(False)
                self.axes[i][j].set_xticklabels('')
                self.axes[i+1][j].get_shared_x_axes().join(self.axes[0][j],self.axes[i+1][j])

        for j in range(self.total_columns.get()):
            label=(self.axes[0][j].yaxis.get_majorticklocs())
            self.axes[0][j].set_yticklabels(label)

        for i in range(1,self.total_rows.get()):
            for j in range(self.total_columns.get()):
                label=(self.axes[i][j].yaxis.get_majorticklocs())
                label=label[0:-1]
                self.axes[i][j].set_yticklabels(label)

        # First search for the maximun and minimun of y in each row:
        for i in range(self.total_rows.get()):
            max=self.axes[i][0].yaxis.get_majorticklocs()[-1]
            min=self.axes[i][0].yaxis.get_majorticklocs()[0]
            for j in range(1,self.total_columns.get()):
                if (self.axes[i][j].yaxis.get_majorticklocs()[-1]>max):
                    max=self.axes[i][j].yaxis.get_majorticklocs()[-1]
                if self.axes[i][j].xaxis.get_majorticklocs()[0]<min:
                    min=self.axes[i][j].yaxis.get_majorticklocs()[0]
            for j in range(self.total_columns.get()):
                self.axes[i][j].set_ylim([min,max])
        for i in range(self.total_rows.get()):
            for j in range(self.total_columns.get()-1):
                # self.axes[i][j].xaxis.set_visible(False)
                self.axes[i][j+1].get_shared_y_axes().join(self.axes[i][0],self.axes[i][j+1])
                self.axes[i][j+1].set_yticklabels('')

        for i in range(self.total_rows.get()):
            label=(self.axes[i][-1].xaxis.get_majorticklocs())
            self.axes[i][0].set_xticklabels(label)

        for i in range(self.total_rows.get()):
            for j in range(self.total_columns.get()-1):
                label=(self.axes[i][j].xaxis.get_majorticklocs())
                label=label[0:-1]
                self.axes[i][j].set_xticklabels(label)

        for i in range(self.total_rows.get()-1):
            for j in range(self.total_columns.get()):
                self.axes[i][j].set_xticklabels('')

        self.fig.tight_layout()
        self.fig.subplots_adjust(hspace=0.001)
        self.fig.subplots_adjust(wspace=0.001)
        self.canvas.draw()

    def change_subplot(self,*args):  # This function will set the focus on the proper subplot
        self.current_axe=self.axes[self.current_row.get()-1][self.current_column.get()-1]  # We marke as the current axe the one we have chosen
        self.label.set("")  # Reset the values of the labels
        self.x_label.set(self.xlabel_index[self.current_row.get()-1][self.current_column.get()-1])  # This set the correct x label
        self.y_label.set(self.ylabel_index[self.current_row.get()-1][self.current_column.get()-1])  # This set the correct ylabel
        self.column_x.set(0)  # Give the variable an initial value
        self.column_y.set(1)  # Give the variable an initial value

    def save_plot(self,*args):  # This function save the figure into a file
        self.fig.savefig(tkFileDialog.asksaveasfilename(filetypes=(('Portable Document Format','*.pdf'),   # Theese are the preset file types
                                                                   ('Portable Network Graphics','*.png'),  # Its possible to insert more
                                                                   ('All files','*.*'))))

    def update_labels(self,*args):  # This will change the x and y labels of a subplot to a new value
        self.current_axe.set_xlabel(self.x_label.get())  # This set the value ox the x label to the one inserted in the entry for that purpouse
        self.current_axe.set_ylabel(self.y_label.get())  # This do the same for the y axe
        self.xlabel_index[self.current_row.get()-1][self.current_column.get()-1]=self.x_label.get()  # This two senteces save the new values
        self.ylabel_index[self.current_row.get()-1][self.current_column.get()-1]=self.y_label.get()  # of the labels for making the logfile
        self.fig.tight_layout()  # We let matplotlib to arrage the spaces between subplots
        self.canvas.draw()  # Update the canvas

    def reset_subplot(self,*args):  # This will reset the current subplot
        self.current_axe.clear()  # This erase all the information of the subplot
        self.linestyle_index[self.current_row.get()-1][self.current_column.get()-1]=[]  # Empty the values of the log variables
        self.marker_index[self.current_row.get()-1][self.current_column.get()-1]=[]
        self.label_index[self.current_row.get()-1][self.current_column.get()-1]=[]
        self.file_index[self.current_row.get()-1][self.current_column.get()-1]=[]
        self.canvas.draw()  # Update the changes on the canvas

    def set_custom_marker(self,*args):  # This function allows as to refine a custom marker
        self.marker.set(self.marker_custom.get())  # This will update the value of the marker variable to the one we have entered

    def open_file(self,*args):  # This will open a window for choosing the file where to read the data
        self.filename.set(tkFileDialog.askopenfilename())  # This stores in the variable of the data file the path to the file
        self.label.set("")  # Set the label to a blank value

    def make_plot(self,*args):  # This will make the plot in the current selected subplot
        self.file_index[self.current_row.get()-1][self.current_column.get()-1].append(self.filename.get())  # Update the log variable
        self.label_index[self.current_row.get()-1][self.current_column.get()-1].append(self.label.get())  # With the information of the
        self.marker_index[self.current_row.get()-1][self.current_column.get()-1].append(self.marker.get())  # new plot
        self.linestyle_index[self.current_row.get()-1][self.current_column.get()-1].append(self.linestyle.get())
        self.color_index[self.current_row.get()-1][self.current_column.get()-1].append(self.color.get())
        self.xcolumn_index[self.current_row.get()-1][self.current_column.get()-1].append(self.column_x.get())
        self.ycolumn_index[self.current_row.get()-1][self.current_column.get()-1].append(self.column_y.get())

        f=open(self.filename.get(),'r')  # Open the file in read mode
        x=[]  # This variable will store the data for the x axis
        y=[]  # This will store the data of the y axis
        for l in f:  # We goe throw the lines of the file
            try:  # We try to add a couple of values to the x and y, this fill only succed if both are numbers
                x.append(float(l.split()[self.column_x.get()]))  # We read the data from the column we have previously selected
                y.append(float(l.split()[self.column_y.get()]))  # For both x and y axes
            except:  # I should insert here only to except some kind of errors, this maybe will be changed in a future, I have to see
                pass  # which are the errors that I should allow (the ones of trying to convert string to float)

        if self.color.get()!='':
            # We plot the data with the color
            self.current_axe.plot(x,y,marker=self.marker.get(),linestyle=self.linestyle.get(),label=self.label.get(),color=self.color.get())
        else:
            self.current_axe.plot(x,y,marker=self.marker.get(),linestyle=self.linestyle.get(),label=self.label.get())

        self.current_axe.legend(loc="best",prop={'size':10})  # current settings, and place a legend. The legend will search for the best location
        # The location of the legend may be customizable in the future
        self.fig.tight_layout()
        self.canvas.draw()
        f.close()  # We close the file

    def export_logfile(self,*args):  # This will export what we have done to make our graphics, so it will be easy to redo or continue our work
        # from that point.
        filename=(tkFileDialog.asksaveasfilename())  # This will open a window to ask for a location to save the file
        f=open(filename,'w+')  # We open the file

        # The first line of the log will be the size of the subplots (how many rows and column)
        f.write("Total Rows "+str(self.total_rows.get())+" Total Columns "+str(self.total_columns.get())+"\n")
        # Now the loop in the rows and the columns
        for i in range(self.total_rows.get()):
            for j in range(self.total_columns.get()):
                # Insert 1 black lines to improve readablity
                f.write("\n")
                # For each row and column, we write its "coordinates" (row and column)
                f.write('row= ')
                f.write(str(i+1))
                f.write(' column= ')
                f.write(str(j+1))
                f.write('\n')
                # We write the value of the labels of the axis
                f.write("\txlabel "+self.xlabel_index[i][j]+"\n")
                f.write("\tylabel "+self.ylabel_index[i][j]+"\n")
                f.write("\txlimits "+str(self.xlimit_index[i][j][0])+" "+str(self.xlimit_index[i][j][1])+"\n")
                f.write("\tylimits "+str(self.ylimit_index[i][j][0])+" "+str(self.ylimit_index[i][j][1])+"\n")

                # Now we loop in the files we have plotted
                for k in range(len(self.file_index[i][j])):
                    # We frite file 1,2,3... before each configuration of a data ser, this number doesn't have any use at all, but maybe
                    # in the future will, and it helps to struturize the logfile
                    f.write('\n\tfile ')
                    f.write(str(k+1))
                    f.write('\n')
                    # Wtite a line with the marker, the program is case sensitive
                    f.write('\t\tMarker '+self.marker_index[i][j][k]+'\n')
                    # Another with the linestyle
                    f.write('\t\tLinestyle '+self.linestyle_index[i][j][k]+'\n')
                    f.write('\t\tColor '+self.color_index[i][j][k]+'\n')
                    # The label of the file
                    f.write('\t\tLabel '+self.label_index[i][j][k]+'\n')
                    # the columns with the data to plot
                    f.write('\t\tXcolumn '+str(self.xcolumn_index[i][j][k])+'\n')
                    f.write('\t\tYcolumn '+str(self.ycolumn_index[i][j][k])+'\n')
                    # The path to the file
                    f.write('\t\tData '+self.file_index[i][j][k]+'\n')

    def read_logfile(self,*args):  # This function will get the path to the logfile we want to import
        filename=(tkFileDialog.askopenfilename())  # This will open a window to select the path
        self.load_logfile(filename)  # We call the function that will actually load the logfile

    def load_logfile(self,filename):  # This function will load the logfile
        # This function takes more arguments than the ones of the class. This was done in order to simplify
        # The load of the logfile directly from terminal
        f=open(filename,'r')  # Open the logfile
        for l in f:  # Loop throw the lines looking for some key words that will load the configuration
            # All the words are case sensitive, so if you want to make yourself the logfile you must notice that
            l=l.strip()
            if l.startswith("Total Row"):  # This should be the first config line of the logfile
                total_rows=int(l.split()[2])  # Read the total number of rows
                total_columns=int(l.split()[5])  # And columns
                if (total_rows>self.total_rows.get() or total_columns>self.total_columns.get() or (not self.figure_created)):
                    # This will be executed if the subplots were not created or if the subplots created are not big enought.
                    # If the created subplots can fit the newones they will add to the current ones
                    self.total_rows.set(total_rows)  # We set the total number of rows and columns
                    self.total_columns.set(total_columns)  # to the value of the logfile
                    self.create_subplots()  # Execute the function that create the subplots

            elif l.startswith("row"):  # This line is the one with the information of wich subplot are we editing
                self.current_row.set(int(l.split()[1]))  # We set the variables to that value
                self.current_column.set(int(l.split()[3]))
                self.change_subplot()  # And change the subplot to that set

            elif l.startswith("xlabel"):  # This line is the one with the label o the x axis
                xlabel=""
                if len(l.split())>2:  # If the label is more than 1 word we add all the words within a separation of 1 space
                    for palabra in l.split()[1:-1]:
                        xlabel+=palabra+" "
                    xlabel+=l.split()[-1]  # And the las word whiout a separation
                elif len(l.split())==2:  # If it's only one word wen just
                    xlabel=l.split()[1]  # set the correct value
                self.x_label.set(xlabel)
                self.update_labels()  # Run the function to make the change

            elif l.startswith("ylabel"):  # The same with the y axis
                ylabel=""
                if len(l.split())>2:
                    for palabra in l.split()[1:-1]:
                        ylabel+=palabra+" "
                    ylabel+=l.split()[-1]
                elif len(l.split())==2:
                    ylabel=l.split()[1]
                self.y_label.set(ylabel)
                self.update_labels()

            elif l.startswith("xlimit"):
                if len(l.split())==3:
                    self.xmin.set(float(l.split()[1]))
                    self.xmax.set(float(l.split()[2]))
                    self.change_x_axe()

            elif l.startswith("ylimit"):  # The same with the y axis
                if len(l.split())==3:
                    self.ymin.set(float(l.split()[1]))
                    self.ymax.set(float(l.split()[2]))
                    self.change_y_axe()

            elif l.startswith("Marker"):  # This line have the marker
                self.marker.set(l.split()[1])  # Set the marker to the correct value
                # If this line is not finded the value of this variable will be the last one used or, if it wasn't used before, the
                # preset value, this aplies also to the linestyle
            elif l.startswith("Linestyle"):  # This line have the linestyle wanted
                self.linestyle.set(l.split()[1])  # set the variable to it's proper value

            elif l.startswith("Color"):
                if len(l.split())==2:
                    self.color.set(l.split()[1])
                else:
                    self.color.set("")

            elif l.startswith("Xcolumn"):
                self.column_x.set(int(l.split()[1]))

            elif l.startswith("Ycolumn"):
                self.column_y.set(int(l.split()[1]))

            elif l.startswith("Label"):  # This lane have the value of the label
                label=""
                if len(l.split())>2:  # We make the same trick than for reading the x and y labels
                    for palabra in l.split()[1:-1]:
                        label=label+palabra+" "
                    label=label+l.split()[-1]
                elif len(l.split())==2:
                    label=l.split()[1]
                self.label.set(label)  # Update the calue of the label variable

            elif l.startswith("Data"):  # This line have the path to the file with the data, it can be absolute or relative path
                # In oreder to avoid errors is better to use absolute paths (the ones the program writes with the export logfile)
                filename2=""
                if len(l.split())>2:  # We make the same trick than with the labels
                    for palabra in l.split()[1:-1]:
                        filename2=filename2+palabra+" "
                    filename2=filename2+l.split()[-1]
                elif len(l.split())==2:
                    filename2=l.split()[1]
                self.filename.set(filename2)  # Update the variable with the path to the data file
                self.make_plot()  # We make the plot

        f.close()  # Finally we close the file

    # This functions will allow to choose the range of the x and y axe
    def change_x_axe(self,*args):
        self.current_axe.set_xlim([self.xmin.get(),self.xmax.get()])  # This line set the axes to the value entered
        self.xlimit_index[self.current_row.get()-1][self.current_column.get()-1]=[self.xmin.get(),self.xmax.get()]
        self.fig.tight_layout()  # This fit all the changes
        self.canvas.draw()  # This draws the new figure

    def change_y_axe(self,*args):
        self.current_axe.set_ylim([self.ymin.get(),self.ymax.get()])
        self.ylimit_index[self.current_row.get()-1][self.current_column.get()-1]=[self.ymin.get(),self.ymax.get()]
        self.fig.tight_layout()
        self.canvas.draw()
コード例 #9
0
hnu_grid = table_key_dict["{0}_{1}".format("hnugrid", selected_ID.get())]
mgr_grid = table_key_dict["{0}_{1}".format("ramg", selected_ID.get())]
mgp_grid = table_key_dict["{0}_{1}".format("pmg", selected_ID.get())]
mgs_grid = table_key_dict["{0}_{1}".format("rsmg", selected_ID.get())]
mgrt_grid = table_key_dict["{0}_{1}".format("rtmg", selected_ID.get())]

# get log average rho and T for initial plot
avg_rho = exp(0.5 * (log(max(rho_grid)) + log(min(rho_grid))))
avg_rho = round(avg_rho, -int(floor(log10(avg_rho)) - 1))
avg_T = exp(0.5 * (log(max(T_grid)) + log(min(T_grid))))
avg_T = round(avg_T, -int(floor(log10(avg_T)) - 1))

# setup and initialize Tk values that can be tied to entry fields
# set temperature and density to log average value
target_rho = DoubleVar()
target_rho.set(avg_rho)
target_T = DoubleVar()
target_T.set(avg_T)
op_type = IntVar()
op_type.set(1)

# get interpolated opacity data for this temperature and density
# first check to see if the data is valid (non-zero)
mgr_valid = ip_reader.check_valid_data(mgr_grid)
mgp_valid = ip_reader.check_valid_data(mgp_grid)
mgs_valid = ip_reader.check_valid_data(mgs_grid)
mgrt_valid = ip_reader.check_valid_data(mgrt_grid)

name = selected_ID.get()
print("-------------------- BEGIN DATA PRINT FOR {0} ---------------------".format(name))
print("Group structure for {0} groups:".format(len(hnu_grid) - 1))
コード例 #10
0
ファイル: rotate_plugin.py プロジェクト: sk1project/sk1-tk
class RotatePanel(PluginPanel):
    name = "Rotate"
    title = _("Rotate")

    def init(self, master):
        PluginPanel.init(self, master)

        root = self.mw.root

        self.var_angle = DoubleVar(root)
        self.var_angle.set(0)

        self.var_width_number = DoubleVar(root)
        self.var_height_number = DoubleVar(root)

        self.var_width_base = DoubleVar(root)
        self.var_height_base = DoubleVar(root)

        self.cnt_x_absolute = None
        self.cnt_y_absolute = None

        var_width_unit = StringVar(root)
        var_height_unit = StringVar(root)

        unit = config.preferences.default_unit
        self.var_width = LengthVar(10, unit, self.var_width_number, var_width_unit)
        self.var_height = LengthVar(10, unit, self.var_height_number, var_height_unit)

        jump = config.preferences.default_unit_jump
        self.var_width.set(0)
        self.var_height.set(0)

        self.var_width_base.set(0)
        self.var_height_base.set(0)

        self.var_position = StringVar(root)
        self.var_position.set(ABSOLUTE)

        self.var_basepoint = StringVar(root)
        self.var_basepoint.set("C")

        # ---------------------------------------------------------
        top = TFrame(self.panel, style="FlatFrame")
        top.pack(side=TOP, fill=BOTH)
        # ---------------------------------------------------------

        angle_frame = TFrame(top, style="FlatFrame", borderwidth=3)
        angle_frame.pack(side=TOP, fill=BOTH)
        label = TLabel(angle_frame, style="FlatLabel", text=" " + _("Angle:") + " ")
        label.pack(side=LEFT, padx=5)

        self.entry_angle = TSpinbox(
            angle_frame,
            var=0,
            vartype=1,
            textvariable=self.var_angle,
            min=-360,
            max=360,
            step=5,
            width=6,
            command=self.apply_rotate,
        )
        self.entry_angle.pack(side=LEFT, anchor=E)
        label = TLabel(angle_frame, style="FlatLabel", text=_("deg"))
        label.pack(side=LEFT, padx=5)
        # ---------------------------------------------------------
        label = TLabel(top, style="FlatLabel", text=_("Center:"))
        label.pack(side=TOP, fill=BOTH, padx=5)

        # ---------------------------------------------------------
        # Horisontal
        size_frameH = TFrame(top, style="FlatFrame", borderwidth=3)
        size_frameH.pack(side=TOP, fill=BOTH)

        label = TLabel(size_frameH, style="FlatLabel", image="center_h")
        label.pack(side=LEFT, padx=5)
        self.entry_width = TSpinbox(
            size_frameH,
            var=0,
            vartype=1,
            textvariable=self.var_width_number,
            min=-50000,
            max=50000,
            step=jump,
            width=10,
            command=self.apply_rotate,
        )
        self.entry_width.pack(side=LEFT)

        self.labelwunit = TLabel(size_frameH, style="FlatLabel", text=self.var_width.unit)
        self.labelwunit.pack(side=LEFT, padx=5)
        # ---------------------------------------------------------
        # Vertical

        size_frameV = TFrame(top, style="FlatFrame", borderwidth=3)
        size_frameV.pack(side=TOP, fill=BOTH)
        label = TLabel(size_frameV, style="FlatLabel", image="center_v")
        label.pack(side=LEFT, padx=5)

        self.entry_height = TSpinbox(
            size_frameV,
            var=0,
            vartype=1,
            textvariable=self.var_height_number,
            min=-50000,
            max=50000,
            step=jump,
            width=10,
            command=self.apply_rotate,
        )
        self.entry_height.pack(side=LEFT)

        self.labelhunit = TLabel(size_frameV, style="FlatLabel", text=self.var_height.unit)
        self.labelhunit.pack(side=LEFT, padx=5)

        # ---------------------------------------------------------
        # position chek

        self.position_check = TCheckbutton(
            top,
            text=_("Absolute Center"),
            variable=self.var_position,
            onvalue=ABSOLUTE,
            offvalue=RELATIVE,
            command=self.position,
        )
        self.position_check.pack(side=TOP, anchor=W, padx=5, pady=5)

        # ---------------------------------------------------------
        # Basepoint check

        label = TLabel(top, style="FlatLabel", text=_("Basepoint:"))
        label.pack(side=TOP, fill=BOTH, padx=5)
        basepoint_frame = TLabelframe(top, labelwidget=label, style="Labelframe", borderwidth=4)
        basepoint_frame.pack(side=TOP, fill=X, padx=5, pady=2)

        self.Basepoint = BasePointSelector(basepoint_frame, anchor=self.var_basepoint, command=self.apply_basepoint)
        self.Basepoint.pack(side=LEFT, fill=BOTH, padx=5)

        label = TLabel(basepoint_frame, style="FlatLabel", image="coordinate_deg")
        label.pack(side=LEFT, fill=BOTH, padx=10)

        self.position_check.pack(side=TOP, anchor=W, padx=5, pady=5)

        # ---------------------------------------------------------
        # Button frame

        button_frame = TFrame(top, style="FlatFrame", borderwidth=5)
        button_frame.pack(side=BOTTOM, fill=BOTH)

        self.update_buttons = []
        self.button = UpdatedButton(top, text=_("Apply"), command=self.apply_rotate)
        self.button.pack(in_=button_frame, side=BOTTOM, expand=1, fill=X, pady=3)

        self.button_copy = UpdatedButton(top, text=_("Apply to Copy"), command=self.apply_to_copy)
        self.button_copy.pack(in_=button_frame, side=BOTTOM, expand=1, fill=X)

        self.init_from_doc()
        self.subscribe_receivers()

    ###############################################################################

    def subscribe_receivers(self):
        self.document.Subscribe(SELECTION, self.Update)
        self.document.Subscribe(EDITED, self.update_var)
        config.preferences.Subscribe(CHANGED, self.update_pref)

    def unsubscribe_receivers(self):
        self.document.Unsubscribe(SELECTION, self.Update)
        self.document.Unsubscribe(EDITED, self.update_var)
        config.preferences.Unsubscribe(CHANGED, self.update_pref)

    def init_from_doc(self, *arg):
        self.Update()

    def Update(self, *arg):
        if self.is_selection():
            self.entry_angle.set_state(NORMAL)
            self.entry_width.set_state(NORMAL)
            self.entry_height.set_state(NORMAL)
            self.position_check["state"] = NORMAL
            self.button["state"] = NORMAL
            self.button_copy["state"] = NORMAL
            self.TestBasepoint()
        else:
            self.entry_angle.set_state(DISABLED)
            self.entry_width.set_state(DISABLED)
            self.entry_height.set_state(DISABLED)
            self.position_check["state"] = DISABLED
            self.button["state"] = DISABLED
            self.button_copy["state"] = DISABLED

        self.update_pref()

    def apply_basepoint(self):
        self.update_var()

    def position(self):
        self.update_var()

    def RotateSelected(self, angle, cnt=None):
        text = _("Rotation")
        if self.document.selection:
            self.document.begin_transaction(text)
            try:
                try:
                    if cnt is None:
                        cnt = self.document.selection.coord_rect.center()
                    angle = angle * degrees
                    trafo = Rotation(angle, cnt)
                    self.document.TransformSelected(trafo, text)
                except:
                    self.document.abort_transaction()
            finally:
                self.document.end_transaction()

    def RotateAndCopy(self, angle, cnt=None):
        text = _("Rotation&Copy")
        if self.document.selection:
            self.document.begin_transaction(text)
            try:
                try:
                    if cnt is None:
                        cnt = self.document.selection.coord_rect.center()
                    angle = angle * degrees
                    trafo = Rotation(angle, cnt)
                    self.document.ApplyToDuplicate()
                    self.document.TransformSelected(trafo, text)
                except:
                    self.document.abort_transaction()
            finally:
                self.document.end_transaction()

    def apply_rotate(self, *arg):
        if self.button["state"] == DISABLED:
            return

        try:
            var_x = self.var_width.get()
            var_y = self.var_height.get()
            var_a = self.var_angle.get()
        except:
            return

        if var_a < 0:
            if var_a < -360:
                var_a += int(var_a / 360) * 360
            var_a += 360

        if self.var_basepoint.get() != "USER":
            self.cnt_x_absolute, self.cnt_y_absolute = self.coordinates(ABSOLUTE)
            self.var_basepoint.set("USER")

        if self.var_width_base != var_x or self.var_height_base != var_y:

            if self.var_position.get() == ABSOLUTE:
                self.cnt_x_absolute = var_x
                self.cnt_y_absolute = var_y
            else:
                x, y = self.coordinates(ABSOLUTE, "C")
                self.cnt_x_absolute = var_x + x
                self.cnt_y_absolute = var_y + y

            self.var_basepoint.set("USER")

        if arg and arg[0] == "Duplicate":
            self.RotateAndCopy(var_a, Point(self.cnt_x_absolute, self.cnt_y_absolute))
        else:
            self.RotateSelected(var_a, Point(self.cnt_x_absolute, self.cnt_y_absolute))

    def apply_to_copy(self):
        self.apply_rotate("Duplicate")

    def coordinates(self, position, anchor=None):
        br = self.document.selection.coord_rect
        hor_sel = br.right - br.left
        ver_sel = br.top - br.bottom

        if position == RELATIVE:
            left, bottom = -hor_sel / 2.0, -ver_sel / 2.0
        else:
            left, bottom = br.left, br.bottom

        cnt_x, cnt_y = self.Basepoint.get_basepoint(hor_sel, ver_sel, left, bottom, anchor)
        return cnt_x, cnt_y

    def TestBasepoint(self):
        if self.cnt_x_absolute is None:
            return
        base = ["C", "NW", "N", "NE", "W", "E", "SW", "S", "SE"]
        for b in xrange(len(base)):
            cnt_x, cnt_y = self.coordinates(ABSOLUTE, base[b])
            if round(cnt_x, 2) == round(self.cnt_x_absolute, 2) and round(cnt_y, 2) == round(self.cnt_y_absolute, 2):
                self.var_basepoint.set(base[b])
                return
        self.var_basepoint.set("USER")

    def update_pref(self, *arg):
        self.labelwunit["text"] = config.preferences.default_unit
        self.labelhunit["text"] = config.preferences.default_unit
        self.entry_width.step = config.preferences.default_unit_jump
        self.entry_height.step = config.preferences.default_unit_jump
        self.update_var()

    def update_var(self, *arg):
        if len(self.document.selection.GetInfo()):

            self.var_width.unit = config.preferences.default_unit
            self.var_height.unit = config.preferences.default_unit

            if self.var_basepoint.get() == "USER":

                if self.var_position.get() == ABSOLUTE:
                    self.var_width.set(self.cnt_x_absolute)
                    self.var_height.set(self.cnt_y_absolute)
                else:
                    x, y = self.coordinates(ABSOLUTE, "C")
                    self.var_width.set(self.cnt_x_absolute - x)
                    self.var_height.set(self.cnt_y_absolute - y)

            else:
                x, y = self.coordinates(self.var_position.get())
                self.var_width.set(x)
                self.var_height.set(y)
                self.var_width_base = self.var_width.get()
                self.var_height_base = self.var_height.get()

    def is_selection(self):
        return len(self.document.selection) > 0
コード例 #11
0
ファイル: calelevation.py プロジェクト: Rcharle1/NoCodeHere
class GeoGUI:
    def __init__(self, master):
        self.master = master
        master.title("GeoGUI")
        self.w= "W:"
        self.inputw=0
        self.e= "E:"
        self.inpute=0
        self.s= "S:"
        self.inputs=0
        self.n= "N:"
        self.inputn=0
        self.r= "Resolution:"
        self.inputr=0
        self.lele= "Lower Elevation Limits:"
        self.inputlele=-40000
        self.hele= "Higher Elevation Limits:"
        self.inputhele=40000
        self.step= "Step size:"
        self.inputstep=.1

        
        self.w_label_text = StringVar()
        self.w_label = Label(master, textvariable=self.w_label_text)     
        self.w_label_text.set(self.w)
        self.e_label_text = StringVar()
        self.e_label = Label(master, textvariable=self.e_label_text)
        self.e_label_text.set(self.e)
        self.n_label_text = StringVar()
        self.n_label = Label(master, textvariable=self.n_label_text)
        self.n_label_text.set(self.n)
        self.s_label_text = StringVar()
        self.s_label = Label(master, textvariable=self.s_label_text)        
        self.s_label_text.set(self.s)  
        self.r_label_text = DoubleVar()
        self.r_label = Label(master, textvariable=self.r_label_text)        
        self.r_label_text.set(self.r)  
        self.lele_label_text = DoubleVar()
        self.lele_label = Label(master, textvariable=self.lele_label_text)        
        self.lele_label_text.set(self.lele) 
        self.hele_label_text = DoubleVar()
        self.hele_label = Label(master, textvariable=self.hele_label_text)        
        self.hele_label_text.set(self.hele) 
        self.step_label_text = DoubleVar()
        self.step_label = Label(master, textvariable=self.step_label_text)        
        self.step_label_text.set(self.step) 
        self.label = Label(master, text="W:")
        self.label = Label(master, text="E:")
        self.label = Label(master, text="N:")
        self.label = Label(master, text="S:")
        self.label = Label(master, text="Resolution:")
        self.label = Label(master, text="Lower Elevation Limits:")
        self.label = Label(master, text="Higher Elevation Limits:")
        self.label = Label(master, text="Step size:")
        wvcmd = master.register(self.wvalidate)
        evcmd = master.register(self.evalidate)
        svcmd = master.register(self.svalidate)
        nvcmd = master.register(self.nvalidate)
        rvcmd = master.register(self.rvalidate) 
        lelevcmd = master.register(self.lelevalidate)
        helevcmd = master.register(self.helevalidate)
        stepvcmd = master.register(self.stepvalidate) # we have to wrap the command
        self.entryw = Entry(master, validate="key", validatecommand=(wvcmd, '%s'))
        self.entrye = Entry(master, validate="key", validatecommand=(evcmd, '%s'))
        self.entrys = Entry(master, validate="key", validatecommand=(svcmd, '%s'))
        self.entryn = Entry(master, validate="key", validatecommand=(nvcmd, '%s'))
        self.entryr = Entry(master, validate="key", validatecommand=(rvcmd, '%P'))
        self.entrylele = Entry(master, validate="key", validatecommand=(lelevcmd, '%P'))     
        self.entryhele = Entry(master, validate="key", validatecommand=(helevcmd, '%P'))     
        self.entrystep = Entry(master, validate="key", validatecommand=(stepvcmd, '%P'))       
     

        self.w_label.pack()
        self.entryw.pack()
        self.e_label.pack()
        self.entrye.pack()
        self.s_label.pack()
        self.entrys.pack()
        self.n_label.pack()
        self.entryn.pack()
        self.r_label.pack()
        self.entryr.pack()
        self.lele_label.pack()
        self.entrylele.pack()
        self.hele_label.pack()
        self.entryhele.pack()
        self.step_label.pack()
        self.entrystep.pack()


        self.run_button = Button(master, text="Run", command=self.run)
        self.run_button.pack()

        self.close_button = Button(master, text="Close", command=master.quit)
        self.close_button.pack()


        

        
    
    def evalidate(self, new_text):
        if not new_text: # the field is being cleared
            self.inpute = 0
            return True

        try:
    
            self.inpute = float(new_text)
            print(self.inpute)
            return True
        except ValueError:
            if  new_text[0] == "-":
                self.inpute=-1
                return True
            return False

    def wvalidate(self, new_text):
        if not new_text: # the field is being cleared
            self.inputw = 0
            return True

        try:
            self.inputw = float(new_text)
            print(self.inputw)
            return True
        except ValueError:
            if  new_text[0] == "-":
                self.inputw=-1
                return True
            return False

    def nvalidate(self, new_text):
        if not new_text: # the field is being cleared
            self.inputn = 0
            return True
        try:

            self.inputn = float(new_text)
            print(self.inputn)
            return True
        except ValueError:
            if  new_text[0] == "-":
                self.inputn=-1
                return True
            return False

    def svalidate(self, new_text):
        if not new_text: # the field is being cleared
            self.inputs = 0
            return True

        try:

            self.inputs = float(new_text)
            print(self.inputs)
            return True
        except ValueError:
            if  new_text[0] == "-":
                self.inputs=-1
                return True
            return False


    def rvalidate(self, new_text):
        if not new_text: # the field is being cleared
            self.inputr = 0
            return True

        try:
            self.inputr = int(new_text)
            return True
        except ValueError:
            return False

    def lelevalidate(self, new_text):
        if not new_text: # the field is being cleared
           
            return True

        try:
            self.inputlele = float(new_text)
            return True
        except ValueError:
            return False

    def helevalidate(self, new_text):
        if not new_text: # the field is being cleared
            
            return True

        try:
            self.inputhele = float(new_text)
            return True
        except ValueError:
            return False

    def stepvalidate(self, new_text):
        if not new_text: # the field is being cleared
            
            return True

        try:
            self.inputstep = float(new_text)
            return True
        except ValueError:
            return False
    
    


    
    def run(self):
        locations=[]
        tstamp= str(datetime.datetime.now())

        
        foutname= os.path.join(os.path.expanduser("~"),"Documents",tstamp+".ascii")
        fout = open(foutname, 'w')
        w=self.inputw
        s= self.inputs
        while s <= self.inputn:
           
            while w <= self.inpute+.1:
                locations.append((w,s))
                w+=self.inputstep
               

            getElevation(locations,self.inputr,self.inputlele,self.inputhele,fout)
            s+=self.inputstep
            w=self.inputw
            #print(self.inputs)
            locations=[]
        
        fout.close()
コード例 #12
0
class ChemicalEditor:
    """Gets the contaminant properties."""
    def __init__(self, master, system, chemical, chemicals, database,
                 editflag):
        """Constructor method.  Defines the parameters to be obtained in this window."""

        self.master = master
        self.fonttype = system.fonttype
        self.version = system.version
        self.superfont = get_superfont(self.fonttype)  #superscript font
        self.frame = Frame(master.frame)
        self.tframe = Frame(master.tframe)
        self.bframe = Frame(master.bframe)
        self.system = system
        self.database = database
        self.top = None  #flag for existence of toplevel#
        self.tkfont = tkFont.Font(font=system.fonttype)

        self.soluableflag = chemical.soluable
        self.editflag = editflag
        self.cancelflag = 0
        self.chemicals = chemicals
        self.diffunit = system.diffunit
        self.diffunits = system.diffunits

        if self.soluableflag == 1:
            self.database = database
            self.chemical = chemical

            self.name = StringVar(value='')
            self.formula = StringVar(value='')
            self.MW = DoubleVar(value=0)
            self.temp = DoubleVar(value=0)
            self.Dw = DoubleVar(value=0)
            self.Ref = StringVar(value='')
            self.Koc = DoubleVar(value=0)
            self.Kdoc = DoubleVar(value=0)
            self.Kf = DoubleVar(value=0)
            self.N = DoubleVar(value=0)

            if editflag == 1:  #Detemine whether the chemical is added or edited

                self.name.set(self.chemical.name)
                self.formula.set(self.chemical.formula)
                self.MW.set(self.chemical.MW)
                self.temp.set(self.chemical.temp)
                self.Dw.set(self.chemical.Dw)
                self.Koc.set(self.chemical.Koc)
                self.Kdoc.set(self.chemical.Kdoc)
                self.Ref.set(self.chemical.Ref)
                self.Kf.set(self.chemical.Kf)
                self.N.set(self.chemical.N)
        else:
            self.name = StringVar(value=' ')
            self.formula = StringVar(value=' ')
            self.MW = DoubleVar(value=100)
            self.temp = DoubleVar(value=0)
            self.Dw = DoubleVar(value=0)
            self.Koc = DoubleVar(value=0)
            self.Kdoc = DoubleVar(value=0)
            self.Kf = DoubleVar(value=0)
            self.N = DoubleVar(value=0)

            if editflag == 1:  #Detemine whether the chemical is added or edited
                self.name.set(chemical.name)

    def make_widgets(self):
        """Make the widgets for the window."""

        self.bgcolor = self.frame.cget('bg')
        self.instructions = Label(
            self.tframe,
            text=
            ' Please provide the following chemical properties:                    '
        )

        self.blankcolumn = Label(self.frame, text=' ', width=2)
        self.namecolumn = Label(self.frame, text=' ', width=20)
        self.MWcolumn = Label(self.frame, text=' ', width=12)
        self.tempcolumn = Label(self.frame, text=' ', width=10)
        self.Dwcolumn = Label(self.frame, text=' ', width=18)
        self.Koccolumn = Label(self.frame, text=' ', width=18)
        self.Kdoccolumn = Label(self.frame, text=' ', width=18)
        self.Refcolumn = Label(self.frame, text=' ', width=20)
        self.Rightcolumn = Label(self.frame, text=' ', width=2)

        self.namelabel = Label(self.frame, text='Chemical name')
        self.MWlabel = Label(self.frame, text='Molecular\n weight')
        self.templabel = Label(self.frame, text='Temperature')
        self.Dwlabel = Label(self.frame,
                             text='Molecular diffusivity\n in water')
        self.Koclabel = Label(self.frame,
                              text='Organic carbon\n partition coefficient')
        self.Kdoclabel = Label(
            self.frame,
            text='Dissolved organic carbon\n partition coefficient')
        self.Reflabel = Label(self.frame, text='Reference')

        self.tempunits = Label(self.frame, text=unichr(176) + 'C')
        self.Dwunits = Label(self.frame, text=self.diffunit)
        self.Kocunits = Label(self.frame, text='log(L/kg)')
        self.Kdocunits = Label(self.frame, text='log(L/kg)')

        self.importbutton = Button(self.frame,
                                   text='From Database',
                                   width=20,
                                   command=self.importchemical)
        self.okbutton = Button(self.frame,
                               text='OK',
                               width=20,
                               command=self.OK)
        self.cancelbutton = Button(self.frame,
                                   text='Cancel',
                                   width=20,
                                   command=self.Cancel)
        self.blank1 = Label(self.frame, text=' ')
        self.blank2 = Label(self.frame, text=' ')

        #show the widgets on the grid
        self.instructions.grid(row=0,
                               column=0,
                               columnspan=6,
                               padx=8,
                               sticky='W')

        self.blankcolumn.grid(row=1, column=0, sticky='WE', padx=1, pady=1)
        self.namecolumn.grid(row=1, column=1, sticky='WE', padx=1, pady=1)
        self.MWcolumn.grid(row=1, column=2, sticky='WE', padx=1, pady=1)
        self.tempcolumn.grid(row=1, column=3, sticky='WE', padx=1, pady=1)
        self.Dwcolumn.grid(row=1, column=4, sticky='WE', padx=1, pady=1)
        self.Koccolumn.grid(row=1, column=5, sticky='WE', padx=1, pady=1)
        self.Kdoccolumn.grid(row=1, column=6, sticky='WE', padx=1, pady=1)
        self.Refcolumn.grid(row=1, column=7, sticky='WE', padx=1, pady=1)
        self.Rightcolumn.grid(row=1, column=8, sticky='WE', padx=1, pady=1)

        self.namelabel.grid(row=2, column=1, sticky='WE', padx=1, pady=1)
        self.MWlabel.grid(row=2, column=2, sticky='WE', padx=1, pady=1)
        self.templabel.grid(row=2, column=3, sticky='WE', padx=1, pady=1)
        self.Dwlabel.grid(row=2, column=4, sticky='WE', padx=1, pady=1)
        self.Koclabel.grid(row=2, column=5, sticky='WE', padx=1, pady=1)
        self.Kdoclabel.grid(row=2, column=6, sticky='WE', padx=1, pady=1)
        self.Reflabel.grid(row=2, column=7, sticky='WE', padx=1, pady=1)

        self.tempunits.grid(row=3, column=3, sticky='WE', padx=1, pady=1)
        self.Dwunits.grid(row=3, column=4, sticky='WE', padx=1, pady=1)
        self.Kocunits.grid(row=3, column=5, sticky='WE', padx=1, pady=1)
        self.Kdocunits.grid(row=3, column=6, sticky='WE', padx=1, pady=1)

        if self.soluableflag == 1:
            if self.editflag == 0:
                self.namewidget = Entry(self.frame,
                                        width=18,
                                        justify='center',
                                        textvariable=self.name)
                self.tempwidget = Entry(self.frame,
                                        width=10,
                                        justify='center',
                                        textvariable=self.temp)
            else:
                self.namewidget = Label(self.frame,
                                        width=18,
                                        justify='center',
                                        textvariable=self.name)
                self.tempwidget = Label(self.frame,
                                        width=10,
                                        justify='center',
                                        textvariable=self.temp)

            self.Dwwidget = Entry(self.frame,
                                  width=16,
                                  justify='center',
                                  textvariable=self.Dw)
            self.MWwidget = Entry(self.frame,
                                  width=10,
                                  justify='center',
                                  textvariable=self.MW)
            self.Kocwidget = Entry(self.frame,
                                   width=16,
                                   justify='center',
                                   textvariable=self.Koc)
            self.Kdocwidget = Entry(self.frame,
                                    width=16,
                                    justify='center',
                                    textvariable=self.Kdoc)
            self.Refwidget = Entry(self.frame,
                                   width=18,
                                   justify='center',
                                   textvariable=self.Ref)

            self.namewidget.grid(row=4, column=1, padx=2, pady=1)
            self.MWwidget.grid(row=4, column=2, padx=2, pady=1)
            self.tempwidget.grid(row=4, column=3, padx=2, pady=1)
            self.Dwwidget.grid(row=4, column=4, padx=2, pady=1)
            self.Kocwidget.grid(row=4, column=5, padx=2, pady=1)
            self.Kdocwidget.grid(row=4, column=6, padx=2, pady=1)
            self.Refwidget.grid(row=4, column=7, padx=2, pady=1)

        else:
            self.namewidget = Entry(self.frame,
                                    width=18,
                                    justify='center',
                                    textvariable=self.name)
            self.Dwwidget = Label(self.frame,
                                  width=16,
                                  justify='center',
                                  text='Not applicable')
            self.Kocwidget = Label(self.frame,
                                   width=16,
                                   justify='center',
                                   text='Not applicable')
            self.Kdocwidget = Label(self.frame,
                                    width=16,
                                    justify='center',
                                    text='Not applicable')

            self.namewidget.grid(row=4, column=1, padx=2, pady=1)
            self.Dwwidget.grid(row=4, column=3, padx=2, pady=1)
            self.Kocwidget.grid(row=4, column=4, padx=2, pady=1)
            self.Kdocwidget.grid(row=4, column=5, padx=2, pady=1)

        self.blank1.grid(row=5)
        if self.editflag == 0: self.importbutton.grid(row=6, columnspan=11)
        self.okbutton.grid(row=7, columnspan=11)
        self.cancelbutton.grid(row=8, columnspan=11)
        self.blank2.grid(row=9)
        self.okbutton.bind('<Return>', self.OK)
        self.focusbutton = self.okbutton

        if int((self.tkfont.measure(self.Ref.get()) + 10) * 1.1424219345 /
               8) + 1 > 18:
            self.Refwidget.config(
                width=int((self.tkfont.measure(self.Ref.get()) + 10) *
                          1.1424219345 / 8) + 3)
        if int((self.tkfont.measure(self.name.get()) + 10) * 1.1424219345 /
               8) + 1 > 18:
            self.namewidget.config(
                width=int((self.tkfont.measure(self.name.get()) + 10) *
                          1.1424219345 / 8) + 3)

        self.master.geometry()
        self.master.center()

    def importchemical(self):

        if self.top is None:

            self.top = CapSimWindow(master=self.master, buttons=2)
            self.top.make_window(
                ChemicalImporter(self.top, self.system, self.database))
            self.top.tk.mainloop()

            if self.top.window.cancelflag == 0:
                self.updatechemical(self.top.window)

            if self.top is not None:
                self.top.destroy()
                self.top = None

        elif self.top is not None:
            tkmb.showerror(
                title=self.system.version,
                message=
                'Please close the existing parameter input window first.')
            self.top.tk.focus()

    def updatechemical(self, window):

        self.name.set(window.name.get())
        self.formula.set(window.formula.get())
        self.MW.set(window.MW.get())
        self.temp.set(window.temp.get())
        self.Koc.set(window.Koc.get())
        self.Kdoc.set(window.Kdoc.get())
        self.Ref.set(window.Ref.get())
        self.Kf.set(window.Kf.get())
        self.N.set(window.N.get())

        if self.diffunit == self.diffunits[0]: self.Dw.set(window.Dw.get())
        elif self.diffunit == self.diffunits[1]:
            Dw = window.Dw.get() * 86400 * 365
            if Dw > 1: self.Dw.set(round(Dw, 2))
            else:
                i = 2
                while Dw / 100 < 0.1**i:
                    i = i + 1
                self.Dw.set(round(Dw, i))

        self.frame.update()

        if int((self.tkfont.measure(self.Ref.get()) + 10) * 1.1424219345 /
               8) + 1 > 20:
            self.Refwidget.config(
                width=int((self.tkfont.measure(self.Ref.get()) + 10) *
                          1.1424219345 / 8) + 3)
        if int((self.tkfont.measure(self.name.get()) + 10) * 1.1424219345 /
               8) + 1 > 20:
            self.namewidget.config(
                width=int((self.tkfont.measure(self.name.get()) + 10) *
                          1.1424219345 / 8) + 3)
        self.master.geometry()
        self.master.center()

    def OK(self, event=None):
        """Finish and move on.  Checks that the number chemicals are less than the total number of chemicals in database."""

        if self.editflag == 0:
            check = [(chemical.name == self.name.get())
                     for chemical in self.chemicals[0:-1]]
        else:
            check = [0]

        if self.master.window.top is not None: self.master.open_toplevel()
        elif sum(check) >= 1: self.chemicals_error()
        elif self.name.get() == '' or self.name.get().count(' ') == len(
                self.name.get()):
            self.name_error()
        elif self.Dw.get() == 0:
            self.Dw_error()
        else:
            self.master.tk.quit()

    def chemicals_error(self):

        tkmb.showerror(title=self.version,
                       message='This chemical has already been selected!')
        self.focusbutton = self.okbutton
        self.master.tk.lift()

    def name_error(self):

        tkmb.showerror(title=self.version,
                       message='Please input the name for the chemical!')
        self.focusbutton = self.okbutton
        self.master.tk.lift()

    def Dw_error(self):

        tkmb.showerror(
            title=self.version,
            message=
            'The diffusivity of the chemical can not be zero, please correct!')
        self.focusbutton = self.okbutton
        self.master.tk.lift()

    def Cancel(self):

        try:
            self.name.set(self.chemical.name)
            self.MW.set(self.chemical.MW.get())
            self.formula.set(self.chemical.formula.get())
            self.temp.set(self.chemical.temp.get())
            self.Dw.set(self.chemical.Dw.get())
            self.Koc.set(self.chemical.Koc.get())
            self.Kdoc.set(self.chemical.Kdoc.get())
            self.Ref.set(self.chemical.Ref.get())
            self.Kf.set(self.chemical.Kf.get())
            self.N.set(self.chemical.N.get())
        except:
            self.cancelflag = 1

        if self.master.window.top is not None: self.master.open_toplevel()
        else: self.master.tk.quit()
コード例 #13
0
class Controller(Observer):
    def __init__(self,parent,view,lissajou,subjectSig,subjects):

        self.cursorFrame = Frame(parent)
        self.selectionFrame = Frame(self.cursorFrame)
        self.view = view
        self.lissajou = lissajou
        self.subjects = subjects
        self.subjectSig=subjectSig
        self.amp=IntVar()
        self.scale_amp=Scale(self.cursorFrame,variable=self.amp,
                          label="Amplitude",
                          orient="horizontal",length=250,from_=0,to=10,
                          sliderlength=50,tickinterval=1,showvalue=0,
                          command=self.update)
        self.freq=IntVar()
        self.scale_freq=Scale(self.cursorFrame,variable=self.freq,
                          label="Frequence",
                          orient="horizontal",length=250,from_=0,to=10,
                          sliderlength=50,tickinterval=0,showvalue=0,
                          command=self.update)
        self.offset=DoubleVar()
        self.scale_offset=Scale(self.cursorFrame,variable=self.offset,
                          label="Offset",
                          orient="horizontal",length=250,from_=-10.0,to=10.0,
                          sliderlength=50,tickinterval=5,showvalue=0,
                          command=self.update)

        self.phase=IntVar()
        self.scale_phase=Scale(self.cursorFrame,variable=self.phase,
                          label="Phase",
                          orient="horizontal",length=250,from_=-90,to=90,
                          sliderlength=10,tickinterval=45,showvalue=0,
                          command=self.update)


        self.voltVar = DoubleVar()
        self.voltVar.set(1)
        self.button1 = Radiobutton(self.selectionFrame, text="1V", variable=self.voltVar,
                                    value=1.0*5.0,command =lambda:self.update(None))
        self.button1.select()

        self.button2 = Radiobutton(self.selectionFrame, text="2V", variable=self.voltVar,
                                    value=2.0*5.0, command =lambda:self.update(None))

        self.button5 = Radiobutton(self.selectionFrame, text="5V", variable=self.voltVar,
                                    value=5.0*5.0, command =lambda:self.update(None))

        self.isOffsetVar= IntVar()
        self.isOffset = Checkbutton(self.selectionFrame,text = "Offset",variable = self.isOffsetVar,
                                    command =lambda:self.update(None))

    def update(self,event):
        self.update_amplitude(event)
        self.update_offset(event)
        self.update_frequency(event)
        self.update_phase(event)
        self.view.update()
        if self.lissajou!=None:
            self.lissajou.update()


    def update_amplitude(self,event):
        print("update_amplitude(self,event)",self.amp.get())
        self.subjectSig.set_magnitude(self.amp.get()/self.voltVar.get())
        self.subjects.generate_XYCurve()
    def update_frequency(self,event):
        print("update_frequency(self,event)",self.freq.get())
        self.subjectSig.set_frequency(self.freq.get())
        self.subjects.generate_XYCurve()
    def update_phase(self,event):
        print("update_phase(self,event)",self.phase.get())
        self.subjectSig.set_phase(self.phase.get())
        self.subjects.generate_XYCurve()
    def update_offset(self,event):
        if self.isOffsetVar.get():
            print("update_offset(self,event)",self.isOffsetVar.get())
            self.subjectSig.set_offset(self.offset.get()/self.voltVar.get())
            self.subjects.generate_XYCurve()
        else:
            self.subjectSig.set_offset(0.0)
            self.subjects.generate_XYCurve()

    def setLissajou(self,lissajou):
        self.lissajou = lissajou


    def packing(self) :
        self.selectionFrame.pack(side='top')
        self.button1.pack(side='left')
        self.button2.pack(side='left')
        self.button5.pack(side='left')
        self.isOffset.pack(side='left')
        self.cursorFrame.pack(side='left',expand=1, fill='both')
        self.scale_amp.pack()
        self.scale_freq.pack()
        self.scale_offset.pack()
        self.scale_phase.pack()
コード例 #14
0
ファイル: data_run_gui.py プロジェクト: project8/Pypeline
class take_data:
    '''
    '''

    def __init__(self, pype, toplevel=False, filename=False, num_sequences=10,
                 run_tag=''):
        '''
        '''
        self.pype = pype
        self.toplevel = toplevel

        self.keep_runningVar = BooleanVar(value=True)
        self.extend_runVar = BooleanVar(value=False)
        self.run_typeVar = StringVar(value="/tmp/")
        self.run_tagVar = StringVar(value=run_tag)
        self.num_sequencesVar = IntVar(value=num_sequences)
        self.sequence_spacingVar = DoubleVar(value=0)
        self.len_sequenceVar = DoubleVar()
        self.stateVar = StringVar(value='done')
        self.conf_filename = StringVar(value='')
        self.params = {}
        self.runthread = multiprocessing.Process()

        self._GetParamFuncs()
        if toplevel:
            self._BuildGui()

    def _BuildGui(self):
        '''
            Setup all of the buttons and user entries
        '''
        # Data Location
        row = 0
        Label(self.toplevel, text='-'*20+'Data Location'+'-'*20).grid(row=row,
                                                        columnspan=3, column=0)
        row += 1
        Label(self.toplevel, text='/data').grid(row=row, column=0, sticky='E')
        run_type_options = ["/tmp/", "/commissioning/", "/runs/"]
        OptionMenu(self.toplevel, self.run_typeVar, *run_type_options).grid(
                                             row=row, column=1, sticky='EW')
        Entry(self.toplevel, textvariable=self.run_tagVar).grid(row=row,
                                                                column=2)
        Checkbutton(self.toplevel, text="Extend if Exists",
                    variable=self.extend_runVar).grid(row=row, column=3)
        row += 1
        Label(self.toplevel, text="(raid location)").grid(row=row, column=0, sticky='E')
        Label(self.toplevel, text="(run type)").grid(row=row, column=1)
        Label(self.toplevel, text="(run tag)").grid(row=row, column=2)
        row += 1

        # Acquisition Cycle Details
        Label(self.toplevel, text='-'*20+'Acquisition Cycles'+'-'*20).grid(row=row,
                                                        columnspan=3, column=0)
        row += 1
        Label(self.toplevel, text='Number of Sequences').grid(row=row,
                                                              column=0)
        Entry(self.toplevel, textvariable=self.num_sequencesVar).grid(row=row,
                                                                      column=1)
        row += 1
        Label(self.toplevel, text='Delay Between').grid(row=row, column=0)
        Entry(self.toplevel, textvariable=self.sequence_spacingVar).grid(row=row, column=1)
        Label(self.toplevel, text='[s]').grid(row=row, column=2, sticky='W')
        row += 1
        builtins_list = ['default_run', 'noise_analysis_run']
        self.conf_filename.set(builtins_list[0])
        Button(self.toplevel, text='Load Builtin Run Def',
               command=self._GetParamFuncs).grid(row=row, column=0)
        OptionMenu(self.toplevel, self.conf_filename, *builtins_list).grid(
            row=row, column=1)
        self.conf_filename.set(builtins_list[0])
        row += 1
        Label(self.toplevel, text='Load Custom Run Def').grid(row=row,
                                                              column=0)
        Button(self.toplevel, text="find file", command=self._ParamFuncFile
               ).grid(row=row, column=1)
        row += 1

        # Mantis Settings
        Label(self.toplevel, text='-'*20+'Mantis Settings'+'-'*20).grid(row=row,
                                                        columnspan=3, column=0)
        row += 1
        Label(self.toplevel, text='(Empty fields use default values)').grid(row=row,
                                                        columnspan=3, column=0)
        row += 1
        Label(self.toplevel, text='Digitization Time').grid(row=row, column=0)
        Entry(self.toplevel, textvariable=self.len_sequenceVar).grid(row=row,
                                                                     column=1)
        Label(self.toplevel, text='[ms]').grid(row=row, column=2, sticky='W')
        row += 1

        Button(self.toplevel, text="Start Run", command=self.DoRun
               ).grid(row=row, column=0)
        Button(self.toplevel, text="ABORT", command=self._Abort, bg='red'
               ).grid(row=row, column=1)
        Label(self.toplevel, textvariable=self.stateVar).grid(row=row, column=2)

    def _ParamFuncFile(self):
        '''
        '''
        self.conf_filename.set(askopenfilename())
        if self.conf_filename.get():
            self._GetParamFuncs()

    def _Abort(self):
        '''
        '''
        self.keep_runningVar.set(False)
        if self.runthread.is_alive():
            print('terminating child process')
            self.runthread.terminate()
        else:
            print('no current thread')
        self.stateVar.set('aborted')

    def _IsRunning(self):
        '''
        '''
        print(self.runthread.is_alive())

    def _GetParamFuncs(self):
        '''
        '''
        fname = self.conf_filename.get()
        if not fname or fname == 'default_run':
            if not 'run_params' in sys.modules:
                from . import default_run as run_params
            else:
                reload(run_params)
        elif fname == 'noise_analysis_run':
            from . import noise_analysis_run as run_params
        else:
            imp.load_source('run_params', fname)
            import run_params
        self.DefaultParams = run_params.DefaultParams
        self.SequenceParams = run_params.SequenceParams
        self.FilenamePrefix = run_params.FilenamePrefix
        self.Mantis_kwargs = run_params.Mantis_kwargs()
        if 'duration' in self.Mantis_kwargs:
            self.len_sequenceVar.set(self.Mantis_kwargs['duration'])

    def DoRun(self):
        '''
            Execute the run
        '''
        self.keep_runningVar.set(True)
        self.stateVar.set('normal')
        if self.runthread.is_alive():
            print('there is already live process, abort first or let it finish')
        else:
            self.runthread = multiprocessing.Process(target=self.__DoRun)
            self.runthread.start()

    def __DoRun(self):
        '''
            the run called by DoRun in a subprocess
        '''
        self.params['run_tag'] = self.run_tagVar.get()
        self.params['num_sequences'] = self.num_sequencesVar.get()
        print('setting defaults')
        self._SetParams(self.DefaultParams())
        for sequence_num in range(self.params['num_sequences']):
            print('--> starting sequence {0}/{1}'.format(sequence_num, self.params['num_sequences'] - 1))
            if not self.keep_runningVar.get():
                print('Aborting!')
                break
            self._DoSequence(sequence_num)
            print('--> sequence {0}/{1} complete.'.format(sequence_num, self.params['num_sequences'] - 1))
            sleep(self.sequence_spacingVar.get())
        print('-> run (tag: {0}) complete.'.format(self.params['run_tag']))
        self.stateVar.set('run complete')

    def _SetParams(self, params_list):
        '''
        '''
        for channel_name, value in params_list:
            setattempt = self.pype.Set(channel_name, value)
            setattempt.Wait()
            if setattempt.Waiting():
                raise NoResponseError('setting ' + str(channel_name))
            print(channel_name, '->', value)

    def _DoSequence(self, sequence_number):
        '''
            Do one sequence within the run
        '''
        mantis_kwargs = self.Mantis_kwargs.copy()
        run_doc = self.pype.NewDump(uuid4().hex, self.params['run_tag'],
                                    new_run=((not sequence_number) and
                                    not self.extend_runVar.get()))
        self._SetParams(self.SequenceParams(sequence_number))
        for channel in self.pype.ListWithProperty('dump'):
            run_doc[channel] = self.pype.Get(channel)
            run_doc[channel].Update()
            run_doc[channel].Wait()
        run_doc._UpdateTo()
        outfilename = '/data/{:s}_{:05d}_{:05d}.egg'.format(
            self.FilenamePrefix(sequence_number),
            run_doc['run_number'],
            run_doc['sequence_number'])
        print('outputting '+outfilename)
        run_descrip = ast.literal_eval(mantis_kwargs['description'])
        for (chan, val) in self.SequenceParams(sequence_number):
            run_descrip[chan] = val
        run_descrip['run_tag'] = self.params['run_tag']
        run_doc['sequence_tag'] = dumps(run_descrip)
        mantis_kwargs.update({'output': outfilename,
                              'description': dumps(run_descrip),
                              'duration': self.len_sequenceVar.get()})
        run = self.pype.RunMantis(**mantis_kwargs)
        print('mantis run starting')
        run.Wait()
        run_doc['mantis'] = run
        run_doc._UpdateTo()
コード例 #15
0
ファイル: fourbar_gui1.py プロジェクト: 2014c2g1/2014cadp
class FourBarGUI(object):
    """
    GUI to model a 4-bar mechanism.
    """
    def __init__(self, wdw, r, c):
        """
        Determines layout of the canvas,
        number of rows and colums is r and c.
        """
        wdw.title('a 4-bar mechanism')
        self.fbr = FourBar()
        self.rows = r
        self.cols = c
        self.ox = c/3
        self.oy = 3*r/4
        # print "A =" , (self.ox, self.oy)
        self.togo = False
        # the canvas and start, stop, and clear buttons
        self.c = Canvas(wdw, width=self.cols, height=self.rows, bg='green')
        self.c.grid(row=1, column=2, columnspan=2)
        self.startbut = Button(wdw, text='start', command = self.start)
        self.startbut.grid(row=3, column=2, sticky=W+E)
        self.stopbut = Button(wdw, text='stop', command = self.stop)
        self.stopbut.grid(row=3, column=3, sticky=W+E)
        self.clearbut = Button(wdw, text='clear', command = self.clear)
        self.clearbut.grid(row=3, column=4, columnspan=3, sticky=W+E)
        # the length of the crank
        self.crank_lbl = Label(wdw, text='crank', justify=LEFT)
        self.crank_lbl.grid(row=0, column=0)
        self.crank_bar = IntVar()
        self.L = Scale(wdw, orient='vertical', from_=0, to=self.rows/2, \
            tickinterval=20, resolution=1, length=self.rows, \
            variable=self.crank_bar, command=self.draw_mechanism)
        self.L.set(self.fbr.crank)
        self.L.grid(row=1, column=0)
        # the angle that drives the crank
        self.angle_lbl = Label(wdw, text='angle', justify=LEFT)
        self.angle_lbl.grid(row=0, column=1)
        self.angle = DoubleVar()
        self.t = Scale(wdw, orient='vertical', from_=0, to=6.30, \
            tickinterval=0.30, resolution=0.01, length=self.rows, \
            variable=self.angle, command=self.draw_mechanism)
        self.t.grid(row=1, column=1)
        self.angle.set(self.fbr.angle)
        # the bar at the right
        self.right_bar_lbl = Label(wdw, text='right bar', justify=LEFT)
        self.right_bar_lbl.grid(row=0, column=4)
        self.right_bar = IntVar()
        self.r = Scale(wdw, orient='vertical', from_=0, to=self.rows/2, \
            tickinterval=20, resolution=1, length=self.rows, \
            variable=self.right_bar, command=self.draw_mechanism)
        self.r.grid(row=1, column=4)
        self.right_bar.set(self.fbr.right)
        # the top bar attached to the crank
        self.top_bar_lbl = Label(wdw, text='top bar', justify=LEFT)
        self.top_bar_lbl.grid(row=0, column=5)
        self.r_top_bar = IntVar()
        self.R = Scale(wdw, orient='vertical', from_=0, to=self.rows/2, \
            tickinterval=20, resolution=1, length=self.rows, \
            variable=self.r_top_bar, command=self.draw_mechanism)
        self.R.grid(row=1, column=5)
        self.r_top_bar.set(self.fbr.top)
        # the scale for the coupler bar
        self.coupler_bar_lbl = Label(wdw, text='coupler', justify=LEFT)
        self.coupler_bar_lbl.grid(row=0, column=6)
        self.coupler_bar = IntVar()
        self.cpl = Scale(wdw, orient='vertical', from_=0, to=self.rows/2, \
            tickinterval=20, resolution=1, length=self.rows, \
            variable=self.coupler_bar, command=self.draw_mechanism)
        self.cpl.grid(row=1, column=6)
        self.coupler_bar.set(self.fbr.coupler)
        # the horizontal bottom bar
        self.flat_lbl = Label(wdw, text='right joint', justify=RIGHT)
        self.flat_lbl.grid(row=2, column=1)
        self.flat = IntVar()
        self.f = Scale(wdw, orient='horizontal', from_=0, to=self.rows/2, \
            tickinterval=50, resolution=1, length=self.cols, \
            variable=self.flat, command=self.draw_mechanism)
        self.f.grid(row=2, column=2, columnspan=2)
        self.flat.set(self.fbr.flat)
        # coordinates of the coupler point appear on top
        self.ex = Entry(wdw) # for x value
        self.ex.grid(row=0, column=2)
        self.ex.insert(INSERT, "x = ")
        self.ey = Entry(wdw) # for y value
        self.ey.grid(row=0, column=3)
        self.ey.insert(INSERT,"y = ")
        # check button for drawing of coupler curve
        self.curve = IntVar()
        self.cb = Checkbutton(wdw, text='coupler', \
            variable=self.curve, onvalue=1, offvalue=0)
        self.curve.set(1)
        self.cb.grid(row=3, column=0)
        # draw the mechanism on canvas
        self.draw_mechanism(0)

    def update_values(self):
        """
        Takes all values of the scales and updates
        the data attributes of self.fbr.
        """
        self.fbr.flat = self.flat.get()
        self.fbr.crank = self.crank_bar.get()
        self.fbr.top = self.r_top_bar.get()
        self.fbr.right = self.right_bar.get()
        self.fbr.coupler = self.coupler_bar.get()
        self.fbr.angle = self.angle.get()
        #self.fbr.print_joints()

    def draw_coupler_point(self, p):
        """
        Draws coupler point with coordinates in p
        if the curve checkbox is on.
        Note that the previous values for the coordinates
        of the coupler point are stored in the entry fields.
        """
        if self.curve.get() == 1:
            px = self.ox + p[0]
            py = self.oy - p[1]
            eqx = self.ex.get()
            Lx = eqx.split('=')
            if Lx[1] == ' ':
                qx = 0.0
            else:
                qx = float(Lx[1])
            eqy = self.ey.get()
            Ly = eqy.split('=')
            if Ly[1] == ' ':
                qy = 0.0
            else:
                qy = float(Ly[1])
            if (qx != 0.0) and (qy != 0.0):
                qx = self.ox + qx
                qy = self.oy - qy
                self.c.create_line(qx, qy, px, py, width=1)

    def fill_entries(self, p):
        """
        Fills the entry fields with the coordinates
        of the coupler point in p.
        """
        sx = 'x = %f' % p[0]
        sy = 'y = %f' % p[1]
        self.ex.delete(0, END)
        self.ex.insert(INSERT, sx)
        self.ey.delete(0, END)
        self.ey.insert(INSERT, sy)

    def draw_link(self, p, q, s):
        """
        Draws the link from point with coordinates in p
        to the point with coordinates in q, using s as tag.
        """
        self.c.delete(s)
        px = self.ox + p[0]
        py = self.oy - p[1]
        qx = self.ox + q[0]
        qy = self.oy - q[1]
        self.c.create_line(px, py, qx, qy, width=2, tags=s)

    def draw_mechanism(self, v):
        """
        Fills the canvas with the current model
        of the planar 4-bar mechanism.
        Because this command is called by the sliders,
        the argument v is needed but not used.
        """
        self.update_values()
        L = self.fbr.joints()
        for i in range(0, len(L)):
            p = L[i]
            px = self.ox + p[0]
            py = self.oy - p[1]
            sj = 'joint%d' % i
            self.c.delete(sj)
            self.c.create_oval(px-6, py-6, px+6, py+6, width=1, \
                outline='black', fill='red', tags=sj)
        self.draw_link(L[0], L[2], 'link0')
        self.draw_link(L[1], L[3], 'link1')
        self.draw_link(L[2], L[3], 'link2')
        self.draw_link(L[2], L[4], 'link3')
        self.draw_coupler_point(L[4])
        self.fill_entries(L[4])

    def start(self):
        """
        Starts the animation, adding 0.01 to angle.
        """
        self.togo = True
        while self.togo:
            theta = self.angle.get()
            theta = theta + 0.01
            if theta > 6.28:
                theta = 0
            self.angle.set(theta)
            self.draw_mechanism(0)
            self.c.update()

    def stop(self):
        """
        Stops the animation.
        """
        self.togo = False

    def clear(self):
        """
        Clears the canvas.
        """
        self.c.delete(ALL)
コード例 #16
0
ファイル: main.py プロジェクト: shish/context
class _App:
    #########################################################################
    # GUI setup
    #########################################################################
    def __menu(self, master):
        menubar = Menu(master)

        def file_menu():
            menu = Menu(menubar, tearoff=0)
            menu.add_command(label="Open ctxt / cbin", command=self.open_file)
            # menu.add_command(label="Append ctxt", command=self.append_file)
            menu.add_separator()
            menu.add_command(label="Exit", command=self.save_settings_and_quit)
            return menu
        menubar.add_cascade(label="File", menu=file_menu())

        def view_menu():
            menu = Menu(menubar, tearoff=0)
            menu.add_checkbutton(label="Auto-render", variable=self.render_auto)
            # menu.add_command(label="Filter threads", command=None)
            return menu
        menubar.add_cascade(label="View", menu=view_menu())

        def analyse_menu():
            # def timechart():
            #    _TimeChart(master, self.output.get("0.0", END))
            menu = Menu(menubar, tearoff=0)
            # menu.add_command(label="Time Chart", command=timechart)
            return menu
        # menubar.add_cascade(label="Analyse", menu=analyse_menu())

        def help_menu():
            def show_about():
                t = Toplevel(master)
                t.title("About")
                t.transient(master)
                t.resizable(False, False)
                Label(t, image=self.img_logo).grid(column=0, row=0, sticky=(E, W))
                Label(t, text="Context %s" % VERSION, anchor=CENTER).grid(column=0, row=1, sticky=(E, W))
                Label(t, text="(c) 2011-2014 Shish", anchor=CENTER).grid(column=0, row=2, sticky=(E, W))
                Button(t, text="Close", command=t.destroy).grid(column=0, row=3, sticky=(E,))
                win_center(t)

            def show_docs():
                t = Toplevel(master)
                t.title("Context Documentation")
                t.transient(master)
                scroll = Scrollbar(t, orient=VERTICAL)
                tx = Text(
                    t,
                    wrap=WORD,
                    yscrollcommand=scroll.set,
                )
                scroll['command'] = tx.yview
                scroll.pack(side=RIGHT, fill=Y, expand=1)
                tx.pack(fill=BOTH, expand=1)
                tx.insert("0.0", b64decode(data.README).replace("\r", ""))
                tx.configure(state="disabled")
                tx.focus_set()
                win_center(t)

            def show_license():
                t = Toplevel(master)
                t.title("Context Licenses")
                t.transient(master)
                scroll = Scrollbar(t, orient=VERTICAL)
                tx = Text(
                    t,
                    wrap=WORD,
                    yscrollcommand=scroll.set,
                )
                scroll['command'] = tx.yview
                scroll.pack(side=RIGHT, fill=Y, expand=1)
                tx.pack(fill=BOTH, expand=1)
                tx.insert("0.0", b64decode(data.LICENSE).replace("\r", ""))
                tx.configure(state="disabled")
                tx.focus_set()
                win_center(t)

            menu = Menu(menubar, tearoff=0)
            menu.add_command(label="About", command=show_about)
            menu.add_command(label="Documentation", command=show_docs)
            menu.add_command(label="License", command=show_license)
            return menu
        menubar.add_cascade(label="Help", menu=help_menu())

        return menubar

    def __control_box(self, master):
        f = None

        def _la(t):
            Label(f, text=t).pack(side="left")

        def _sp(fr, t, i, v, w=10):
            Spinbox(f, from_=fr, to=t, increment=i, textvariable=v, width=w).pack(side="left")

        f = Frame(master)
        _la("  Start ")
        _sp(0, int(time.time()), 10, self.render_start, 15)
        _la("  Seconds ")
        _sp(MIN_SEC, MAX_SEC, 1, self.render_len, 3)
        _la("  Pixels per second ")
        _sp(MIN_PPS, MAX_PPS, 100, self.scale, 5)

        _la("  Cutoff (ms) ")
        _sp(0, 1000, 1, self.render_cutoff, 3)
        _la("  Coalesce (ms) ")
        _sp(0, 1000, 1, self.coalesce_threshold, 3)
        Button(f, text="Render", command=self.update).pack(side=LEFT, fill=Y)  # padding=0

        f.pack()
        return f

    def __bookmarks(self, master):
        panel = Frame(master)
        panel.grid_rowconfigure(0, weight=1)

        bookmarks = Frame(panel)
        bookmarks.grid_columnconfigure(0, weight=1)
        bookmarks.grid_rowconfigure(0, weight=1)

        li = Listbox(bookmarks, width=40)
        li.grid(column=0, row=0, sticky=(N, E, S, W))
        self.bookmarks_list = li

        sb = Scrollbar(bookmarks, orient=VERTICAL, command=li.yview)
        sb.grid(column=1, row=0, sticky=(N, S))

        li.config(yscrollcommand=sb.set)
        def _lbox_selected(*args):
            selected_idx = int(li.curselection()[0])
            self.render_start.set(self.bookmarks_values[selected_idx])
            self.canvas.xview_moveto(0)
            if not self.render_auto.get():
                self.update()
        li.bind('<Double-Button-1>', _lbox_selected)
        bookmarks.grid(column=0, row=0, sticky=(N, E, S, W))

        buttons = Frame(panel)
        Button(buttons, image=self.img_start, command=self.start_event).pack(side="left")
        Button(buttons, image=self.img_prev, command=self.prev_event).pack(side="left")
        Button(buttons, image=self.img_end, command=self.end_event).pack(side="right")
        Button(buttons, image=self.img_next, command=self.next_event).pack(side="right")
        buttons.grid(column=0, row=1, sticky=(E, W))

        return panel

    def __canvas(self, master):
        f = Frame(master)
        f.grid_columnconfigure(0, weight=1)
        f.grid_rowconfigure(0, weight=1)

        h = Scrollbar(f, orient=HORIZONTAL)
        v = Scrollbar(f, orient=VERTICAL)
        canvas = Canvas(
            f,
            background="white",
            xscrollcommand=h.set,
            yscrollcommand=v.set,
        )
        h['command'] = canvas.xview
        v['command'] = canvas.yview

        canvas.bind("<4>", lambda e: self.scale_view(e, 1.0 * 1.1))
        canvas.bind("<5>", lambda e: self.scale_view(e, 1.0 / 1.1))

        # in windows, mouse wheel events always go to the root window o_O
        self.master.bind("<MouseWheel>", lambda e: self.scale_view(
            e, ((1.0 * 1.1) if e.delta > 0 else (1.0 / 1.1))
        ))

        # Drag based movement
        # def _sm(e):
        #    self.st = self.render_start.get()
        #    self.sx = e.x
        #    self.sy = e.y
        # def _cm(e):
        #    self.render_start.set(self.st + float(self.sx - e.x)/self.scale.get())
        #    self.render()
        # self.canvas.bind("<1>", _sm)
        # self.canvas.bind("<B1-Motion>", _cm)

        canvas.grid(column=0, row=0, sticky=(N, W, E, S))
        v.grid(column=1, row=0, sticky=(N, S))
        h.grid(column=0, row=1, sticky=(W, E))

        self.canvas = canvas

        return f

    def __scrubber(self, master):
        sc = Canvas(
            master,
            width=800, height=SCRUBBER_HEIGHT,
            background="white",
        )

        def sc_goto(e):
            width_fraction = float(e.x) / sc.winfo_width()
            ev_s = self.get_earliest_bookmark_after(0)
            ev_e = self.get_latest_bookmark_before(sys.maxint)
            ev_l = ev_e - ev_s
            self.render_start.set(ev_s + ev_l * width_fraction - float(self.render_len.get()) / 2)
            if not self.render_auto.get():
                self.update()
            self.canvas.xview_moveto(0.5)
        sc.bind("<1>", sc_goto)

        def resize(event):
            if self.c:
                self.render_scrubber_activity()
                self.render_scrubber_arrow()
            # sc.coords(line, 0, 0, event.width, event.height)
        sc.bind("<Configure>", resize)

        return sc

    def __init__(self, master, database_file):
        self.master = master
        self.bookmarks_values = []
        self.bookmarks_list = None
        self.canvas = None
        self.scrubber = None  # render is called before init finished?

        self.char_w = -1
        self.soft_scale = 1.0
        self.window_ready = False
        self.data = []
        self.sc_activity = None
        self.original_texts = {}
        self.tooltips = {}
        self.event_idx_offset = 0
        self._last_log_dir = os.path.expanduser("~/")
        self.c = None  # database connection

        try:
            os.makedirs(os.path.expanduser(os.path.join("~", ".config")))
        except OSError:
            pass
        self.config_file = os.path.expanduser(os.path.join("~", ".config", "context.cfg"))

        self.threads = []
        self.render_start = DoubleVar(master, 0)
        self.render_len = IntVar(master, 10)
        self.render_cutoff = IntVar(master, 1)
        self.coalesce_threshold = IntVar(master, 1)
        self.render_auto = IntVar(master, 1)
        self.scale = IntVar(master, 1000)

        self.load_settings()
        master.protocol("WM_DELETE_WINDOW", self.save_settings_and_quit)

        self.render_start.trace_variable("w", lambda *x: conditional(self.render_auto, self.update))
        self.render_len.trace_variable("w", lambda *x: conditional(self.render_auto, self.update))
        self.render_cutoff.trace_variable("w", lambda *x: conditional(self.render_auto, self.render))
        self.coalesce_threshold.trace_variable("w", lambda *x: conditional(self.render_auto, self.update))
        self.scale.trace_variable("w", lambda *x: conditional(self.render_auto, self.render))

        self.img_start = PhotoImage(data=data.start)
        self.img_prev = PhotoImage(data=data.prev)
        self.img_next = PhotoImage(data=data.next)
        self.img_end = PhotoImage(data=data.end)
        self.img_logo = PhotoImage(data=data.context_name)

        menu = self.__menu(master)
        controls_panel = self.__control_box(master)
        bookmarks_panel = self.__bookmarks(master)
        canvas_panel = self.__canvas(master)
        scrubber = self.__scrubber(master)
        status = Label(master, text="")
        if have_ttk:
            grip = Sizegrip(master)
        else:
            grip = Label(master, text="")

        master.grid_columnconfigure(1, weight=1)
        master.grid_rowconfigure(1, weight=1)

        master.config(menu=menu)
        controls_panel.grid(column=0, row=0, sticky=(W, E), columnspan=2)
        bookmarks_panel.grid(column=0, row=1, sticky=(N, W, E, S))
        canvas_panel.grid(column=1, row=1, sticky=(N, E, S, W))
        scrubber.grid(column=0, row=2, sticky=(W, E), columnspan=2)
        status.grid(column=0, row=3, sticky=(W, E), columnspan=2)
        grip.grid(column=1, row=3, sticky=(S, E))

        self.scrubber = scrubber
        self.status = status
        self.master.update()

        self.window_ready = True

        if database_file:
            self.load_file(database_file)

    def set_status(self, text):
        if text:
            print(text)
        self.status.config(text=text)
        self.master.update()

    #########################################################################
    # Open file
    #########################################################################

    def open_file(self):
        filename = askopenfilename(
            filetypes=[
                ("All Supported Types", "*.ctxt *.cbin"),
                ("Context Text", "*.ctxt"),
                ("Context Binary", "*.cbin")
            ],
            initialdir=self._last_log_dir
        )
        if filename:
            try:
                self.load_file(filename)
            except Exception as e:
                self.set_status("Error loading file: %s" % str(e))

    def load_file(self, given_file):
        if not os.path.exists(given_file):
            showerror("Error", "Context dump file '%s' does not exist" % given_file)
            return

        self._last_log_dir = os.path.dirname(given_file)

        path, _ext = os.path.splitext(given_file)

        log_file = path + ".ctxt"
        database_file = path + ".cbin"

        # if the user picked a log file, compile it (unless an
        # up-to-date version already exists)
        if given_file == log_file:
            needs_recompile = False

            if not os.path.exists(database_file):
                needs_recompile = True
                self.set_status("Compiled log not found, compiling")
            elif os.stat(log_file).st_mtime > os.stat(database_file).st_mtime:
                needs_recompile = True
                self.set_status("Compiled log is out of date, recompiling")
            elif not version_check(database_file):
                needs_recompile = True
                self.set_status("Compiled log is from an old version of context, recompiling")

            if needs_recompile:
                compiler = subprocess.Popen(["context-compiler", log_file], stdout=subprocess.PIPE)
                while True:
                    line = compiler.stdout.readline()
                    if line:
                        self.set_status(line.strip())
                    else:
                        break

        self.c = sqlite3.connect(database_file)

        self.data = []  # don't load the bulk of the data yet
        self.load_bookmarks(self.c)
        self.load_summary(self.c)
        self.load_threads(self.c)

        self.master.title(NAME + ": " + database_file)

        # render grid + scrubber
        self.render()

        self.event_idx_offset = self.get_earliest_bookmark_after(0)
        self.render_start.set(self.event_idx_offset)

    def load_bookmarks(self, conn):
        self.bookmarks_values = []
        self.bookmarks_list.delete(0, END)
        for ts, tx, et in conn.execute("""
            SELECT start_time, start_text, end_text
            FROM events
            WHERE start_type = 'BMARK'
            ORDER BY start_time
        """):
            tss = datetime.datetime.fromtimestamp(ts).strftime("%Y/%m/%d %H:%M:%S")  # .%f
            self.bookmarks_values.append(ts)
            self.bookmarks_list.insert(END, "%s: %s" % (tss, tx or et))

    def load_threads(self, conn):
        # fast because the data is split off into a tiny table
        self.threads = [
            "-".join([str(c) for c in r])
            for r
            in conn.execute("SELECT node, process, thread FROM threads ORDER BY id")
        ]

    def load_summary(self, conn):
        self.sc_activity = [row[0] for row in conn.execute("SELECT events FROM summary ORDER BY id")]


    #########################################################################
    # Settings
    #########################################################################

    def load_settings(self):
        try:
            cp = ConfigParser.SafeConfigParser()
            cp.readfp(file(self.config_file))
            if cp.has_section("gui"):
                if cp.has_option("gui", "render_len"):
                    self.render_len.set(cp.getint("gui", "render_len"))
                if cp.has_option("gui", "scale"):
                    self.scale.set(cp.getint("gui", "scale"))
                if cp.has_option("gui", "render_cutoff"):
                    self.render_cutoff.set(cp.getint("gui", "render_cutoff"))
                if cp.has_option("gui", "coalesce_threshold"):
                    self.coalesce_threshold.set(cp.getint("gui", "coalesce_threshold"))
                if cp.has_option("gui", "render_auto"):
                    self.render_auto.set(cp.getint("gui", "render_auto"))
                if cp.has_option("gui", "last_log_dir"):
                    self._last_log_dir = cp.get("gui", "last_log_dir")
        except Exception as e:
            print("Error loading settings from %s:\n  %s" % (self.config_file, e))

    def save_settings(self):
        try:
            cp = ConfigParser.SafeConfigParser()
            cp.add_section("gui")
            cp.set("gui", "render_len", str(self.render_len.get()))
            cp.set("gui", "scale", str(self.scale.get()))
            cp.set("gui", "render_cutoff", str(self.render_cutoff.get()))
            cp.set("gui", "coalesce_threshold", str(self.coalesce_threshold.get()))
            cp.set("gui", "render_auto", str(self.render_auto.get()))
            cp.set("gui", "last_log_dir", self._last_log_dir)
            cp.write(file(self.config_file, "w"))
        except Exception as e:
            print("Error writing settings to %s:\n  %s" % (self.config_file, e))

    def save_settings_and_quit(self):
        self.save_settings()
        self.master.destroy()
        self.master.quit()

    #########################################################################
    # Navigation
    #########################################################################

    def get_earliest_bookmark_after(self, start_hint=0):
        return list(self.c.execute(
            "SELECT min(start_time) FROM events WHERE start_time > ? AND start_type = 'BMARK'",
            [start_hint, ]
        ))[0][0]

    def get_latest_bookmark_before(self, end_hint=0):
        return list(self.c.execute(
            "SELECT max(start_time) FROM events WHERE start_time < ? AND start_type = 'BMARK'",
            [end_hint, ]
        ))[0][0]

    def end_event(self):
        next_ts = self.get_latest_bookmark_before(sys.maxint)
        if next_ts:
            self.render_start.set(next_ts)
        self.canvas.xview_moveto(0)

    def next_event(self):
        next_ts = self.get_earliest_bookmark_after(self.render_start.get())
        if next_ts:
            self.render_start.set(next_ts)
        self.canvas.xview_moveto(0)

    def prev_event(self):
        prev_ts = self.get_latest_bookmark_before(self.render_start.get())
        if prev_ts:
            self.render_start.set(prev_ts)
        self.canvas.xview_moveto(0)

    def start_event(self):
        next_ts = self.get_earliest_bookmark_after(0)
        if next_ts:
            self.render_start.set(next_ts)
        self.canvas.xview_moveto(0)

    #########################################################################
    # Rendering
    #########################################################################

    def scale_view(self, e=None, n=1.0):
        # get the old pos
        if e:
            _xv = self.canvas.xview()
            left_edge = _xv[0]
            width = _xv[1] - _xv[0]
            width_fraction = float(e.x) / self.canvas.winfo_width()
            x_pos = left_edge + width * width_fraction
        # scale
        if n != 1:
            self.soft_scale *= n
            self.canvas.scale("event", 0, 0, n, 1)
            self.canvas.scale("lock", 0, 0, n, 1)
            for t in self.canvas.find_withtag("time_label"):
                val = self.canvas.itemcget(t, 'text')[2:]
                self.canvas.itemconfigure(t, text=" +%.4f" % (float(val) / n))
            for t in self.canvas.find_withtag("event_tip"):
                self.canvas.itemconfigure(t, width=float(self.canvas.itemcget(t, 'width')) * n)  # this seems slow? sure something similar was faster...
            for t in self.canvas.find_withtag("event_label"):
                self.canvas.itemconfigure(t, width=float(self.canvas.itemcget(t, 'width')) * n)  # this seems slow? sure something similar was faster...
                w = int(self.canvas.itemcget(t, 'width'))
                tx = self.truncate_text(" " + self.original_texts[t], w)
                self.canvas.itemconfigure(t, text=tx)  # this seems slow? sure something similar was faster...
            self.canvas.delete("grid")
            self.render_base()
            self.canvas.configure(scrollregion=shrink(self.canvas.bbox("grid"), 2))
        # scroll the canvas so that the mouse still points to the same place
        if e:
            _xv = self.canvas.xview()
            new_width = _xv[1] - _xv[0]
            self.canvas.xview_moveto(x_pos - new_width * width_fraction)

    def truncate_text(self, text, w):
        return text.split("\n")[0][:w / self.char_w]

    def update(self):
        self.update_events()
        self.render()

    def update_events(self):
        """
        Data settings changed, get new data and re-render
        """
        if not self.window_ready:
            # update() is called a couple of times during init()
            return

        try:
            s = self.render_start.get()
            e = self.render_start.get() + self.render_len.get()
            threshold = float(self.coalesce_threshold.get()) / 1000.0
        except ValueError:
            return

        try:
            self.n = 0
            self.data = []  # free memory
            # thread_level_starts = [[], ] * len(self.threads)  # this bug is subtle and hilarious
            thread_level_ends = [[] for _ in self.threads]

            def progress(*args):
                try:
                    self.n += 1
                    self.set_status("Loading... (%dk opcodes)" % (self.n * 10))
                    return 0
                except Exception as e:
                    return 1  # non-zero = cancel query
            self.c.set_progress_handler(progress, 10000)

            try:
                for row in self.c.execute(
                    """
                        SELECT *
                        FROM events
                        WHERE id IN (SELECT id FROM events_index WHERE end_time > ? AND start_time < ?)
                        AND (end_time - start_time) * 1000 >= ?
                        ORDER BY start_time ASC, end_time DESC
                    """,
                    (s - self.event_idx_offset, e - self.event_idx_offset, self.render_cutoff.get())
                ):
                    event = Event(row)
                    thread_idx = event.thread_id

                    if event.start_type == "START":
                        prev_event_at_level = None
                        while thread_level_ends[thread_idx] and thread_level_ends[thread_idx][-1].end_time <= event.start_time:
                            prev_event_at_level = thread_level_ends[thread_idx].pop()
                        event.depth = len(thread_level_ends[thread_idx])

                        if (
                            threshold and
                            prev_event_at_level and
                            prev_event_at_level.can_merge(event, threshold)
                        ):
                            prev_event_at_level.merge(event)
                            thread_level_ends[thread_idx].append(prev_event_at_level)
                        else:
                            thread_level_ends[thread_idx].append(event)
                            self.data.append(event)
                    else:
                        self.data.append(event)
            except sqlite3.OperationalError:
                pass

            self.c.set_progress_handler(None, 0)
        finally:
            self.set_status("")

    def render(self):
        """
        Render settings changed, re-render with existing data
        """
        if not self.window_ready:
            return
        if not MIN_PPS < self.scale.get() < MAX_PPS:
            return
        self.soft_scale = 1.0
        self.render_clear()
        self.render_scrubber_activity()
        self.render_scrubber_arrow()
        self.render_base()
        self.render_data()

    def render_clear(self):
        """
        clear the canvas and any cached variables
        """
        self.canvas.delete(ALL)
        self.original_texts = {}
        self.tooltips = {}
        self.canvas.configure(scrollregion=(
            0, 0,
            self.render_len.get() * self.scale.get(),
            len(self.threads) * (MAX_DEPTH * BLOCK_HEIGHT) + HEADER_HEIGHT
        ))
        if self.char_w == -1:
            t = self.canvas.create_text(0, 0, font="TkFixedFont", text="_", anchor=NW)
            bb = self.canvas.bbox(t)
            # [2]-[0]=10, but trying by hand, 8px looks better on win7
            # 7px looks right on linux, not sure what [2]-[0] is there,
            # hopefully 9px, so "-2" always helps?
            self.char_w = bb[2] - bb[0] - 2
            self.canvas.delete(t)

    def render_scrubber_activity(self, length=None):
        sc = self.scrubber

        if not sc:
            return

        if self.sc_activity is not None:
            sc.delete("activity")
            activity_peak = max(self.sc_activity)
            if activity_peak == 0:
                return
            for n in range(0, length or len(self.sc_activity)):
                col = gen_colour(self.sc_activity[n], activity_peak)
                sc.create_rectangle(
                    int(float(n) / len(self.sc_activity) * sc.winfo_width()), 1,
                    int(float(n + 1) / len(self.sc_activity) * sc.winfo_width()), SCRUBBER_HEIGHT,
                    fill=col, outline=col, tags="activity",
                )

    def render_scrubber_arrow(self):
        sc = self.scrubber

        if not self.window_ready:
            return

        if not sc:
            return

        # events start / end / length
        ev_s = self.get_earliest_bookmark_after(0)
        ev_e = self.get_latest_bookmark_before(sys.maxint)
        ev_l = ev_e - ev_s

        if ev_l == 0:  # only one bookmark
            return

        # view start / end / length
        vi_s = self.render_start.get()
        vi_e = self.render_start.get() + self.render_len.get()
        # vi_l = vi_e - vi_s

        # scrubber width
        sc_w = sc.winfo_width()

        sc.delete("arrow")

        # arrow
        start_abs = vi_s
        start_rel = start_abs - ev_s
        start_fraction = start_rel / ev_l
        start_scaled = start_fraction * sc_w
        start = start_scaled

        end_abs = vi_e
        end_rel = end_abs - ev_s
        end_fraction = end_rel / ev_l
        end_scaled = end_fraction * sc_w
        end = end_scaled

        # left edge
        sc.create_line(
            start, 1,
            start, SCRUBBER_HEIGHT,
            fill="#000", tags="arrow",
        )
        sc.create_line(
            start, SCRUBBER_HEIGHT/2,
            start + 5, 15,
            fill="#000", tags="arrow",
        )
        sc.create_line(
            start, SCRUBBER_HEIGHT/2,
            start + 5, 5,
            fill="#000", tags="arrow",
        )

        # right edge
        sc.create_line(
            end, 1,
            end, SCRUBBER_HEIGHT,
            fill="#000", tags="arrow",
        )
        sc.create_line(
            end, SCRUBBER_HEIGHT/2,
            end - 5, 15,
            fill="#000", tags="arrow",
        )
        sc.create_line(
            end, SCRUBBER_HEIGHT/2,
            end - 5, 5,
            fill="#000", tags="arrow",
        )

        # join
        sc.create_line(
            start, SCRUBBER_HEIGHT/2,
            end, SCRUBBER_HEIGHT/2,
            fill="#000", tags="arrow",
        )

    def render_base(self):
        """
        Render grid lines and markers
        """
        _rl = self.render_len.get()
        _sc = self.scale.get() * self.soft_scale

        rs_px = int(_rl * _sc)
        rl_px = int(_rl * _sc)

        for n in range(rs_px, rs_px + rl_px, 100):
            label = " +%.4f" % (float(n) / _sc - _rl)
            self.canvas.create_line(n - rs_px, 0, n - rs_px, HEADER_HEIGHT + len(self.threads) * MAX_DEPTH * BLOCK_HEIGHT, fill="#CCC", tags="grid")
            self.canvas.create_text(n - rs_px, 5, text=label, anchor=NW, tags="time_label grid")

        for n in range(0, len(self.threads)):
            self.canvas.create_line(0, HEADER_HEIGHT + MAX_DEPTH * BLOCK_HEIGHT * n, rl_px, HEADER_HEIGHT + MAX_DEPTH * BLOCK_HEIGHT * n, tags="grid")
            self.canvas.create_text(0, HEADER_HEIGHT + MAX_DEPTH * BLOCK_HEIGHT * (n + 1) - 5, text=" " + self.threads[n], anchor=SW, tags="grid")

        self.canvas.tag_lower("grid")

    def render_data(self):
        """
        add the event rectangles
        """
        if not self.window_ready:
            # update() is called a couple of times during init()
            return

        _rs = self.render_start.get()
        _rc = self.render_cutoff.get()
        _sc = self.scale.get()

        event_count = len(self.data) - 1
        shown = 0
        for n, event in enumerate(self.data):
            if n % 1000 == 0 or n == event_count:
                self.set_status("Rendered %d events (%d%%)" % (n, float(n) * 100 / event_count))
                self.master.update()
            thread_idx = event.thread_id

            if event.start_type == "START":
                if (event.end_time - event.start_time) * 1000 < _rc:
                    continue
                if event.depth >= MAX_DEPTH:
                    continue
                shown += 1
                if shown == 500 and VERSION.endswith("-demo"):
                    showerror("Demo Limit", "The evaluation build is limited to showing 500 events at a time, so rendering has stopped")
                    break
                self.show_event(
                    event, _rs, _sc,
                    thread_idx,
                )

            elif event.start_type == "BMARK":
                # note that when loading data, we currently filter for
                # "start_type=START" for a massive indexed speed boost
                # so there are no bookmarks. We may want to load bookmarks
                # into a separate array?
                pass  # render bookmark

            elif event.start_type == "LOCKW" or event.start_type == "LOCKA":
                self.show_lock(
                    event, _rs, _sc,
                    thread_idx,
                )

        self.set_status("")

    def show_event(self, event, offset_time, scale_factor, thread):
        function = event.start_location
        ok = event.end_type == "ENDOK"

        start_px = int((event.start_time - offset_time) * scale_factor)
        length_px = int(event.length * scale_factor)

        tip = "%dms @%dms: %s\n%s" % (
            (event.end_time - event.start_time) * 1000,
            (event.start_time - offset_time) * 1000,
            function, event.text
        )

        fill = "#CFC" if ok else "#FCC"
        outl = "#484" if ok else "#844"
        r = self.canvas.create_rectangle(
            start_px, HEADER_HEIGHT + thread * MAX_DEPTH * BLOCK_HEIGHT + event.depth * BLOCK_HEIGHT,
            start_px + length_px, HEADER_HEIGHT + thread * MAX_DEPTH * BLOCK_HEIGHT + event.depth * BLOCK_HEIGHT + BLOCK_HEIGHT,
            fill=fill, outline=outl, tags="event",
        )
        t = self.canvas.create_text(
            start_px, HEADER_HEIGHT + thread * MAX_DEPTH * BLOCK_HEIGHT + event.depth * BLOCK_HEIGHT + 3,
            text=self.truncate_text(" " + event.text, length_px), tags="event event_label", anchor=NW, width=length_px,
            font="TkFixedFont",
            state="disabled",
        )
        self.canvas.tag_raise(r)
        self.canvas.tag_raise(t)

        self.canvas.tag_bind(r, "<1>", lambda e: self._focus(r))

        self.original_texts[t] = event.text
        self.tooltips[r] = tip

        self.canvas.tag_bind(r, "<Enter>", lambda e: self._ttip_show(r))
        self.canvas.tag_bind(r, "<Leave>", lambda e: self._ttip_hide())

    def show_lock(self, event, offset_time, scale_factor, thread):
        start_px = int((event.start_time - offset_time) * scale_factor)
        length_px = int(event.length * scale_factor)

        fill = "#FDD" if event.start_type == "LOCKW" else "#DDF"
        r = self.canvas.create_rectangle(
            start_px, HEADER_HEIGHT + thread * MAX_DEPTH * BLOCK_HEIGHT,
            start_px + length_px, HEADER_HEIGHT + (thread + 1) * MAX_DEPTH * BLOCK_HEIGHT,
            fill=fill, outline=fill, tags="lock",
        )
        t = self.canvas.create_text(
            start_px + length_px, HEADER_HEIGHT + (thread + 1) * MAX_DEPTH * BLOCK_HEIGHT,
            text=self.truncate_text(event.text, length_px),
            tags="lock lock_label", anchor=SE, width=length_px,
            font="TkFixedFont",
            state="disabled",
            fill="#888",
        )
        self.canvas.tag_lower(t)
        self.canvas.tag_lower(r)

    def _focus(self, r):
        # scale the canvas so that the (selected item width + padding == screen width)
        view_w = self.canvas.winfo_width()
        rect_w = max(self.canvas.bbox(r)[2] - self.canvas.bbox(r)[0] + HEADER_HEIGHT, 10)
        self.scale_view(n=float(view_w) / rect_w)

        # move the view so that the selected (item x1 = left edge of screen + padding)
        canvas_w = self.canvas.bbox("grid")[2]
        rect_x = self.canvas.bbox(r)[0] - 5
        self.canvas.xview_moveto(float(rect_x) / canvas_w)

    def _ttip_show(self, r):
        tip = self.tooltips[r]

        x0, y0, x1, y1 = self.canvas.bbox(r)

        if x0 < 0:
            x1 = x1 - x0
            x0 = x0 - x0

        t2 = self.canvas.create_text(
            x0 + 4, y0 + BLOCK_HEIGHT + 4,
            text=tip.strip(), width=400, tags="tooltip", anchor=NW,
            justify="left", state="disabled",
        )

        x0, y0, x1, y1 = self.canvas.bbox(t2)

        r2 = self.canvas.create_rectangle(
            x0 - 2, y0 - 1, x1 + 2, y1 + 2,
            state="disabled", fill="#FFA", outline="#AA8", tags="tooltip"
        )

        self.canvas.tag_raise(t2)

    def _ttip_hide(self):
        self.canvas.delete("tooltip")
コード例 #17
0
ファイル: rcomplete.py プロジェクト: JLuebben/R_complete
def gui():
    from Tkinter import Tk, Label, Entry,Button, Scale, Checkbutton,W,HORIZONTAL, Frame, StringVar, IntVar, DoubleVar, Radiobutton, BooleanVar, E

    global root
    root = Tk()
    root.wm_title("Compute R_complete")
    line = 0

    global insFile, hklFile, nHKL, nParams, nHKLLabel, fracFree, status, nParamsLabel, nCPU, rCompleteLabel, cycles, lsType, cleanup,nFree, nRunsLabel, mergeCheck, compileMap
    insFile = StringVar()
    hklFile = StringVar()
    nHKL = IntVar()
    nParams = IntVar()
    nFree = IntVar()
    fracFree = DoubleVar()
    fracFree.set(5.0)
    nCPU = IntVar()
    nCPU.set(maxCPU)
    cycles = IntVar()
    cycles.set(10)
    lsType = IntVar()
    lsType.set(1)
    cleanup = BooleanVar()
    cleanup.set(True)
    mergeCheck = BooleanVar()
    mergeCheck.set(True)
    compileMap = BooleanVar()
    compileMap.set(True)

    Label(root, text='Instruction File:').grid(row=line, column=0, sticky=E)
    Entry(root, textvariable=insFile).grid(row=line, column=1)
    Button(root, text='Browse', command=browseINS).grid(row=line, column=2)

    line += 1

    Label(root, text='Reflection File:').grid(row=line, column=0, sticky=E)
    Entry(root, textvariable=hklFile).grid(row=line, column=1)
    Button(root, text='Browse', command=browseHKL).grid(row=line, column=2)

    line += 1
    Checkbutton(root, var=mergeCheck, text='Merge Reflections').grid(row=line, column=1, sticky=W)
    line += 1
    Button(root, text='Load', command=load).grid(row=line, columnspan=3)
    line += 1

    Frame(root, height=20).grid(row=line)

    line += 1

    Label(root, text='# of reflections:').grid(row=line, sticky=E)
    nHKLLabel = Label(root, text='???')
    nHKLLabel.grid(row=line, column=1, sticky=W)


    line += 1

    Label(root, text='# of atoms:').grid(row=line, sticky=E)
    nParamsLabel = Label(root, text='???')
    nParamsLabel.grid(row=line, column=1, sticky=W)

    line += 1

    Frame(root, height=20).grid(row=line)

    line += 1

    Label(root, text='Select Parameters').grid(row=line, column=1)
    line += 1

    Frame(root, height=20).grid(row=line)
    line += 1

    Label(root, text='# of free reflections:').grid(row=line, sticky=E)
    nFreeEntry = Entry(root, width=5, textvariable=nFree)
    nFreeEntry.grid(row=line, column=1, sticky=W)
    nFreeEntry.bind('<Return>', setScale)
    nRunsLabel = Label(root, text='# runs')
    nRunsLabel.grid(row=line, column=2)

    line += 1

    Label(root, text='% of free reflections:').grid(row=line, column=0, sticky=E)
    w = Scale(root, from_=0.1, to=10.0, resolution=0.1, orient=HORIZONTAL, length=200, var=fracFree, command=percentScale)
    w.grid(row=line, column=1, columnspan=2, sticky=W)


    line += 1


    Label(root, text='stable <-------------------------------> fast').grid(row=line, column=1, columnspan=2, sticky=W)

    line += 1
    Frame(root, height=10).grid(row=line)


    line += 1

    Label(root, text='Refinement cycles:').grid(row=line, column=0, sticky=E)
    ls = Scale(root, from_=0, to=50, resolution=1, orient=HORIZONTAL, length=200, var=cycles)
    ls.grid(row=line, column=1, columnspan=2, sticky=W)

    line += 1


    Label(root, text='fast <--------------------> less model bias').grid(row=line, column=1, columnspan=2, sticky=W)

    line += 1
    Frame(root, height=10).grid(row=line)


    line += 1
    Label(root, text='# of CPUs:').grid(row=line, column=0, sticky=E)
    ww = Scale(root, from_=1, to=maxCPU, orient=HORIZONTAL, length=200, var=nCPU)
    ww.grid(row=line, column=1, columnspan=2, sticky=W)

    line += 1

    Label(root, text='Refinement Type:').grid(row=line, column=0, sticky=E)
    Radiobutton(root, text='CGLS', var=lsType, value=1).grid(row=line, column=1, sticky=W)
    Radiobutton(root, text='L.S.', var=lsType, value=2).grid(row=line, column=2, sticky=W)

    line += 1
    Frame(root, height=10).grid(row=line)
    line += 1

    Label(root, text='Compile map:').grid(row=line, column=0, sticky=E)
    Checkbutton(root, var=compileMap).grid(row=line, column=1, sticky=W)

    line += 1
    Label(root, text='Cleanup:').grid(row=line, column=0, sticky=E)
    Checkbutton(root, var=cleanup).grid(row=line, column=1, sticky=W)

    line += 1

    Button(root, text='RUN', command=run, width=25).grid(row=line, columnspan=3)

    line += 1
    Frame(root, height=20).grid(row=line)
    line += 1

    Label(root, text='R_complete:').grid(row=line, column=0, sticky=E)
    rCompleteLabel = Label(root, text='???')
    rCompleteLabel.grid(row=line, column=1, sticky=W)

    line += 1

    Frame(root, height=20).grid(row=line)

    line += 1

    Label(root, text='Status:').grid(row=line, column=0, sticky=E)
    status = Label(root, text='Idle... Please load files.')
    status.grid(row=line, column=1, columnspan=2, sticky=W)
    global IDLE
    IDLE = True

    root.mainloop()
コード例 #18
0
ファイル: colordlg.py プロジェクト: shumik/skencil-c
    def set(self, value):
	DoubleVar.set(self, round(value, self.precision))
コード例 #19
0
ファイル: filldlg.py プロジェクト: shumik/skencil-c
class GradientPatternFrame(PatternFrame):

    def __init__(self, master=None, **kw):
	apply(PatternFrame.__init__, (self, master), kw)

	gradient = CreateSimpleGradient(StandardColors.white,
					StandardColors.black)
	frame = Frame(self)
	frame.pack(side = TOP, fill = X)
	self.var_gradient_type = IntVar(self)
	for value, bitmap in [(0, 'linear'), (1, 'conical'), (2, 'radial')]:
	    bitmap = getattr(pixmaps, 'gradient_' + bitmap)
	    button = make_button(frame, bitmap = bitmap, value = value,
				 variable = self.var_gradient_type,
				 command = self.choose_type)
	    button.pack(side = LEFT, fill = X, expand = 1)

	frame = Frame(self)
	frame.pack(side = TOP, expand = 1, fill = X)
	self.colors = [None, None]
	self.colors[0] = ColorButton(frame, height = 1,
				     color = gradient.StartColor(),
				     command = self.set_color, args = 0)
	self.colors[0].pack(side = LEFT, fill = X, expand = 1)
	self.colors[1] =  ColorButton(frame, height = 1,
				      color = gradient.EndColor(),
				      command = self.set_color, args = 1)
	self.colors[1].pack(side = LEFT, fill = X, expand = 1)

	self.var_border = DoubleVar(self)
	self.var_border.set(0.0)
	frame = Frame(self)
	frame.pack(side = TOP, fill = X, expand = 1)
	label = Label(frame, text = _("Border"))
	label.pack(side = LEFT, expand = 1, anchor = E)
	entry = MyEntry(frame, textvariable = self.var_border, width = 4,
			justify = RIGHT, command = self.set_border)
	entry.pack(side = LEFT, expand = 1, fill = X)
	scroll = MiniScroller(frame, variable = self.var_border,
			      min = 0.0, max = 100.0, step = 1.0,
			      command = self.set_border)
	scroll.pack(side = LEFT, fill = Y)

	button = UpdatedButton(self, text = _("Edit Gradient"),
			       command = self.edit_gradient)
	button.pack(side = TOP, fill = X)

	pattern = LinearGradient(gradient)
	self.SetPattern(pattern)


    def set_color(self, idx):
	self.gradient = self.gradient.Duplicate()
	self.gradient.SetStartColor(self.__get_color(0))
	self.gradient.SetEndColor(self.__get_color(1))
	self.pattern = self.pattern.Duplicate()
	self.pattern.SetGradient(self.gradient)
	self.issue(CHANGED)

    def __get_color(self, idx):
	return self.colors[idx].Color()

    def choose_type(self):
	type = gradient_types[self.var_gradient_type.get()]
	self.pattern = type(duplicate = self.pattern)
	self.issue(CHANGED)

    def set_border(self, *rest):
	border = self.var_border.get() / 100.0
	if hasattr(self.pattern, 'SetBorder'):
	    self.pattern = self.pattern.Duplicate()
	    self.pattern.SetBorder(border)
	    self.issue(CHANGED)

    def SetPattern(self, pattern):
	PatternFrame.SetPattern(self, pattern)
	self.gradient = gradient = pattern.Gradient().Duplicate()
	self.var_gradient_type.set(gradient_types.index(pattern.__class__))
	self.colors[0].SetColor(gradient.StartColor())
	self.colors[1].SetColor(gradient.EndColor())
	if hasattr(pattern, 'Border'):
	    self.var_border.set(pattern.Border() * 100.0)

    def Center(self):
	if self.pattern.__class__ == LinearGradient:
	    return (0.5, 0.5)
	return tuple(self.pattern.Center())

    def SetCenter(self, center):
	if self.pattern.__class__ == LinearGradient:
	    return
	p = apply(Point, center)
	self.pattern = self.pattern.Duplicate()
	self.pattern.SetCenter(p)
	self.issue(CHANGED)

    def SetDirection(self, dir):
	if self.pattern.__class__ == RadialGradient:
	    return
	dir = apply(Point, dir)
	self.pattern = self.pattern.Duplicate()
	self.pattern.SetDirection(dir)
	self.issue(CHANGED)

    def EditModes(self):
	if self.pattern.__class__ == LinearGradient:
	    return (0, 1)
	elif self.pattern.__class__ == RadialGradient:
	    return (1, 0)
	else:
	    return (1, 1)

    def edit_gradient(self):
	gradient = gradientedit.EditGradient(self, self.gradient)
	if gradient is not None:
	    pattern = self.pattern.Duplicate()
	    pattern.SetGradient(gradient)
	    self.SetPattern(pattern)
	    self.issue(CHANGED)
コード例 #20
0
ファイル: glee.py プロジェクト: lponnala/glee-py-gui
filename_button = Button(root, text=names[0], command = lambda : filename_value.set(askopenfilename()))
filename_entry = Entry(root, textvariable=filename_value, justify='left')
nA_label = Label(root, text=names[1])
nA_value = IntVar(); nA_value.set(nA_default)
nA_entry = Entry(root, textvariable=nA_value)
nB_label = Label(root, text=names[2])
nB_value = IntVar(); nB_value.set(nB_default)
nB_entry = Entry(root, textvariable=nB_value)
nobinning_label = Label(root, text='DO NOT USE BINNING')
nobinning_value = IntVar(); nobinning_value.set(1)
nobinning_check = Checkbutton(root, variable=nobinning_value, onvalue=1, offvalue=0, command=check_binning)
p_label = Label(root, text=names[3])
p_value = IntVar(); p_value.set(p_default)
p_entry = Entry(root, textvariable=p_value)
fbL_label = Label(root, text=names[4])
fbL_value = DoubleVar(); fbL_value.set(fbL_default)
fbL_entry = Entry(root, textvariable=fbL_value)
fbH_label = Label(root, text=names[5])
fbH_value = DoubleVar(); fbH_value.set(fbH_default)
fbH_entry = Entry(root, textvariable=fbH_value)
binchoice_label = Label(root, text=names[6])
binchoice_frame = Frame(root)
binchoice_value = StringVar()
equal = Radiobutton(binchoice_frame,text='equal',variable=binchoice_value,value='equal',command=check_binchoice)
equal.select()
adaptive = Radiobutton(binchoice_frame,text='adaptive',variable=binchoice_value,value='adaptive',command=check_binchoice)
equal.grid(row=0,column=0)
adaptive.grid(row=0,column=1)
binchoice_frame.grid()
fitType_label = Label(root, text=names[7])
fitType_frame = Frame(root)
コード例 #21
0
ファイル: notebook.py プロジェクト: arnew/dxf2gcode
class ExportParasClass:
    def __init__(self, frame=None, config=None, postpro=None):
        self.frame = frame

        self.initialise_textvariables()
        self.generate_entryfields(config)
        
        #self.change_entry_state(NORMAL)

    def initialise_textvariables(self):
        self.tool_dia = DoubleVar()
        self.tool_dia.set(0.0)
        
        self.start_rad = DoubleVar()
        self.start_rad.set(0.0)        
       
        self.axis3_retract = DoubleVar()
        self.axis3_retract.set(0.0)
        
        self.axis3_safe_margin = DoubleVar()
        self.axis3_safe_margin.set(0.0)

        self.axis3_slice_depth = DoubleVar()
        self.axis3_slice_depth.set(0.0)        

        self.axis3_mill_depth = DoubleVar()
        self.axis3_mill_depth.set(0.0)        
        
        self.F_G1_Depth = DoubleVar()
        self.F_G1_Depth.set(0.0)

        self.F_G1_Plane = DoubleVar()
        self.F_G1_Plane.set(0.0)

    def generate_entryfields(self, config):
        self.entries = []
       
        f1 = Frame(self.frame, relief=GROOVE, bd=2)
        f1.grid(row=1, column=0, padx=2, pady=2, sticky=N + W + E)
        f2 = Frame(self.frame, relief=GROOVE, bd=2)
        f2.grid(row=2, column=0, padx=2, pady=2, sticky=N + W + E)
        f3 = Frame(self.frame, relief=GROOVE, bd=2)
        f3.grid(row=3, column=0, padx=2, pady=2, sticky=N + W + E)
    
        f1.columnconfigure(0, weight=1)
        f2.columnconfigure(0, weight=1)
        f3.columnconfigure(0, weight=1)        
   
        Label(f1, text=_("Tool diameter [mm]:"))\
                .grid(row=0, column=0, sticky=N + W, padx=4)
        self.entries.append(Entry(f1, width=7, textvariable=self.tool_dia, state=DISABLED))
        self.entries[-1].grid(row=0, column=1, sticky=N + E)


        Label(f1, text=_("Start radius (for tool comp.) [mm]:"))\
                .grid(row=1, column=0, sticky=N + W, padx=4)
        self.entries.append(Entry(f1, width=7, textvariable=self.start_rad, state=DISABLED))
        self.entries[-1].grid(row=1, column=1, sticky=N + E) 

        Label(f2, text=(_("%s safety margin [mm]:") % config.ax3_letter))\
                .grid(row=1, column=0, sticky=N + W, padx=4)
        self.entries.append(Entry(f2, width=7, textvariable=self.axis3_safe_margin, state=DISABLED))
        self.entries[-1].grid(row=1, column=1, sticky=N + E)

        Label(f2, text=(_("%s infeed depth [mm]:") % config.ax3_letter))\
                .grid(row=2, column=0, sticky=N + W, padx=4)
        self.entries.append(Entry(f2, width=7, textvariable=self.axis3_slice_depth, state=DISABLED))
        self.entries[-1].grid(row=2, column=1, sticky=N + E)

        Label(f2, text=(_("%s mill depth [mm]:") % config.ax3_letter))\
                .grid(row=3, column=0, sticky=N + W, padx=4)
        self.entries.append(Entry(f2, width=7, textvariable=self.axis3_mill_depth, state=DISABLED))
        self.entries[-1].grid(row=3, column=1, sticky=N + E)

        Label(f3, text=(_("G1 feed %s-direction [mm/min]:") % config.ax3_letter))\
                .grid(row=1, column=0, sticky=N + W, padx=4)
        self.entries.append(Entry(f3, width=7, textvariable=self.F_G1_Depth, state=DISABLED))
        self.entries[-1].grid(row=1, column=1, sticky=N + E)

        Label(f3, text=(_("G1 feed %s%s-direction [mm/min]:") % (config.ax1_letter, config.ax2_letter)))\
                .grid(row=2, column=0, sticky=N + W, padx=4)
        self.entries.append(Entry(f3, width=7, textvariable=self.F_G1_Plane, state=DISABLED))
        self.entries[-1].grid(row=2, column=1, sticky=N + E)

    def change_entry_state(self, state=DISABLED):
        for entry in self.entries:
            entry.config(state=state)
            
    def ShowParas(self, LayerContent):
        print 'Kommt noch'
コード例 #22
0
ファイル: cockpit.py プロジェクト: dpm76/eaglebone
class Cockpit(ttkFrame):
    '''
    Remote device GUI 
    '''
    
    #TODO: 20160415 DPM - Set these values from configuration file
    #--- config
    THROTTLE_BY_USER = True
    THROTTLE_RESOLUTION = 0.1
    
    # Joystick enabled or not, if any
    JOYSTICK_ENABLED = True 

    DEFAULT_DRONE_IP = "192.168.1.130"
    DEFAULT_DRONE_PORT = 2121
    #--- end config
    

    KEY_ANG_SPEED = "ang-speed"
    KEY_ANGLES = "angles"
    KEY_ACCEL = "accel"
    
    PID_KEYS = ["P", "I", "D"]

    DIR_NONE = 0
    DIR_VERTICAL = 1
    DIR_HORIZONTAL = 2
    
    def __init__(self, parent, isDummy = False, droneIp = DEFAULT_DRONE_IP, dronePort = DEFAULT_DRONE_PORT):
        '''
        Constructor
        '''
        ttkFrame.__init__(self, parent)
        
        self._target = [0.0] * 4        
        
        self._selectedPidConstats = "--"
        self._pidConstants = {
                              Cockpit.KEY_ANG_SPEED:{
                                           "X":{
                                                "P": 0.0,
                                                "I": 0.0,
                                                "D": 0.0
                                                },
                                           "Y":{
                                                "P": 0.0,
                                                "I": 0.0,
                                                "D": 0.0
                                                },
                                           "Z":{
                                                "P": 0.0,
                                                "I": 0.0,
                                                "D": 0.0
                                                }
                                           },
                               Cockpit.KEY_ANGLES: {
                                          "X":{
                                                "P": 0.0,
                                                "I": 0.0,
                                                "D": 0.0
                                                },
                                           "Y":{
                                                "P": 0.0,
                                                "I": 0.0,
                                                "D": 0.0
                                                }
                                          },
                               Cockpit.KEY_ACCEL:{
                                           "X":{
                                                "P": 0.0,
                                                "I": 0.0,
                                                "D": 0.0
                                                },
                                           "Y":{
                                                "P": 0.0,
                                                "I": 0.0,
                                                "D": 0.0
                                                },
                                            "Z":{
                                                "P": 0.0,
                                                "I": 0.0,
                                                "D": 0.0
                                                 }
                                          }
                              }
        
        self.parent = parent

        self.initUI()

        self._controlKeysLocked = False

        if not isDummy:
            self._link = INetLink(droneIp, dronePort)
        else:
            self._link = ConsoleLink()
            
        self._link.open()

        self._updateInfoThread = Thread(target=self._updateInfo)
        self._updateInfoThreadRunning = False
        self._readingState = False

        self._start()
        
            
    def initUI(self):
        
        self.parent.title("Drone control")
        self.style = Style()
        self.style.theme_use("default")
        
        self.pack(fill=BOTH, expand=1)
        
        self.parent.bind_all("<Key>", self._keyDown)
        self.parent.bind_all("<KeyRelease>", self._keyUp)

        if system() == "Linux":
            self.parent.bind_all("<Button-4>", self._onMouseWheelUp)
            self.parent.bind_all("<Button-5>", self._onMouseWheelDown)

        else:
            #case of Windows
            self.parent.bind_all("<MouseWheel>", self._onMouseWheel)
        
        #Commands        
        commandsFrame = tkFrame(self)
        commandsFrame.grid(column=0, row=0, sticky="WE")
        
        self._started = IntVar()
        self._startedCB = Checkbutton(commandsFrame, text="On", variable=self._started, command=self._startedCBChanged)
        self._startedCB.pack(side=LEFT, padx=4)
        
#         self._integralsCB = Checkbutton(commandsFrame, text="Int.", variable=self._integralsEnabled, \
#                                         command=self._integralsCBChanged, state=DISABLED)
#         self._integralsCB.pack(side=LEFT, padx=4)
        
        self._quitButton = Button(commandsFrame, text="Quit", command=self.exit)
        self._quitButton.pack(side=LEFT, padx=2, pady=2)
        
#         self._angleLbl = Label(commandsFrame, text="Angle")
#         self._angleLbl.pack(side=LEFT, padx=4)
#         
#         self._angleEntry = Entry(commandsFrame, state=DISABLED)
#         self._angleEntry.pack(side=LEFT)
        
        #Info
        infoFrame = tkFrame(self)
        infoFrame.grid(column=1, row=1, sticky="NE", padx=4)
        
        #Throttle
        Label(infoFrame, text="Throttle").grid(column=0, row=0, sticky="WE")        
        self._throttleTexts = [StringVar(),StringVar(),StringVar(),StringVar()]
        Entry(infoFrame, textvariable=self._throttleTexts[3], state=DISABLED, width=5).grid(column=0, row=1)                
        Entry(infoFrame, textvariable=self._throttleTexts[0], state=DISABLED, width=5).grid(column=1, row=1)
        Entry(infoFrame, textvariable=self._throttleTexts[2], state=DISABLED, width=5).grid(column=0, row=2)
        Entry(infoFrame, textvariable=self._throttleTexts[1], state=DISABLED, width=5).grid(column=1, row=2)
        
        #Angles
        Label(infoFrame, text="Angles").grid(column=0, row=3, sticky="WE")        
        self._angleTexts = [StringVar(),StringVar(),StringVar()]
        for index in range(3):
            Entry(infoFrame, textvariable=self._angleTexts[index], state=DISABLED, width=5).grid(column=index, row=4)
               
        #Accels
        Label(infoFrame, text="Accels").grid(column=0, row=5, sticky="WE")
        self._accelTexts = [StringVar(),StringVar(),StringVar()]
        for index in range(3):
            Entry(infoFrame, textvariable=self._accelTexts[index], state=DISABLED, width=5).grid(column=index, row=6)
        
        #Speeds
        Label(infoFrame, text="Speeds").grid(column=0, row=7, sticky="WE")
        self._speedTexts = [StringVar(),StringVar(),StringVar()]
        for index in range(3):
            Entry(infoFrame, textvariable=self._speedTexts[index], state=DISABLED, width=5).grid(column=index, row=8)
        
        #Height
        Label(infoFrame, text="Height").grid(column=0, row=9, sticky="E")
        self._heightText = StringVar()
        Entry(infoFrame, textvariable=self._heightText, state=DISABLED, width=5).grid(column=1, row=9)
        
        #Loop rate
        Label(infoFrame, text="Loop @").grid(column=0, row=10, sticky="E")
        self._loopRateText = StringVar()
        Entry(infoFrame, textvariable=self._loopRateText, state=DISABLED, width=5).grid(column=1, row=10)
        Label(infoFrame, text="Hz").grid(column=2, row=10, sticky="W")
        
        #control
        
        controlFrame = tkFrame(self)
        controlFrame.grid(column=0, row=1, sticky="W")
        
        self._throttle = DoubleVar()
        
        if Cockpit.THROTTLE_BY_USER:

            self._thrustScale = Scale(controlFrame, orient=VERTICAL, from_=100.0, to=0.0, \
                                tickinterval=0, variable=self._throttle, resolution=Cockpit.THROTTLE_RESOLUTION, \
                                length=200, showvalue=1, \
                                state=DISABLED,
                                command=self._onThrustScaleChanged)

        else:
        
            self._thrustScale = Scale(controlFrame, orient=VERTICAL, from_=100.0, to=-100.0, \
                                tickinterval=0, variable=self._throttle, \
                                length=200, showvalue=1, \
                                state=DISABLED,
                                command=self._onThrustScaleChanged)

        self._thrustScale.bind("<Double-Button-1>", self._onThrustScaleDoubleButton1, "+")
        self._thrustScale.grid(column=0)
        
        self._shiftCanvas = Canvas(controlFrame, bg="white", height=400, width=400, \
                             relief=SUNKEN)
        self._shiftCanvas.bind("<Button-1>", self._onMouseButton1)
        #self._shiftCanvas.bind("<ButtonRelease-1>", self._onMouseButtonRelease1)
        self._shiftCanvas.bind("<B1-Motion>", self._onMouseButton1Motion)
        self._shiftCanvas.bind("<Double-Button-1>", self._onMouseDoubleButton1)

        self._shiftCanvas.bind("<Button-3>", self._onMouseButton3)
        #self._shiftCanvas.bind("<ButtonRelease-3>", self._onMouseButtonRelease3)
        self._shiftCanvas.bind("<B3-Motion>", self._onMouseButton3Motion)

        self._shiftCanvas.grid(row=0,column=1, padx=2, pady=2)
        self._shiftCanvas.create_oval(1, 1, 400, 400, outline="#ff0000")
        self._shiftCanvas.create_line(200, 2, 200, 400, fill="#ff0000")
        self._shiftCanvas.create_line(2, 200, 400, 200, fill="#ff0000")
        self._shiftMarker = self._shiftCanvas.create_oval(196, 196, 204, 204, outline="#0000ff", fill="#0000ff")
        
        self._yaw = DoubleVar()
        self._yawScale = Scale(controlFrame, orient=HORIZONTAL, from_=-100.0, to=100.0, \
                            tickinterval=0, variable=self._yaw, \
                            length=200, showvalue=1, \
                            command=self._onYawScaleChanged)
        self._yawScale.bind("<Double-Button-1>", self._onYawScaleDoubleButton1, "+")
        self._yawScale.grid(row=1, column=1)
        
        self._controlKeyActive = False
        

        #PID calibration

        pidCalibrationFrame = tkFrame(self)
        pidCalibrationFrame.grid(column=0, row=2, sticky="WE");

        self._pidSelected = StringVar()
        self._pidSelected.set("--")
        self._pidListBox = OptionMenu(pidCalibrationFrame, self._pidSelected, "--", \
                                      Cockpit.KEY_ANG_SPEED, Cockpit.KEY_ANGLES, Cockpit.KEY_ACCEL, \
                                       command=self._onPidListBoxChanged)
        self._pidListBox.pack(side=LEFT, padx=2)
        self._pidListBox.config(width=10)
        
        self._axisSelected = StringVar()
        self._axisSelected.set("--")
        self._axisListBox = OptionMenu(pidCalibrationFrame, self._axisSelected, "--", "X", "Y", "Z", \
                                       command=self._onAxisListBoxChanged)
        self._axisListBox.pack(side=LEFT, padx=2)
        self._axisListBox.config(state=DISABLED)

        Label(pidCalibrationFrame, text="P").pack(side=LEFT, padx=(14, 2))

        self._pidPString = StringVar()
        self._pidPString.set("0.00")
        self._pidPSpinbox = Spinbox(pidCalibrationFrame, width=5, from_=0.0, to=10000.0, increment=0.01, state=DISABLED, \
                                         textvariable=self._pidPString, command=self._onPidSpinboxChanged)
        self._pidPSpinbox.pack(side=LEFT, padx=2)

        Label(pidCalibrationFrame, text="I").pack(side=LEFT, padx=(14, 2))

        self._pidIString = StringVar()
        self._pidIString.set("0.00")
        self._pidISpinbox = Spinbox(pidCalibrationFrame, width=5, from_=0.0, to=10000.0, increment=0.01, state=DISABLED, \
                                         textvariable=self._pidIString, command=self._onPidSpinboxChanged)
        self._pidISpinbox.pack(side=LEFT, padx=2)
        
        Label(pidCalibrationFrame, text="D").pack(side=LEFT, padx=(14, 2))
        
        self._pidDString = StringVar()
        self._pidDString.set("0.00")
        self._pidDSpinbox = Spinbox(pidCalibrationFrame, width=5, from_=0.0, to=10000.0, increment=0.01, state=DISABLED, \
                                         textvariable=self._pidDString, command=self._onPidSpinboxChanged)
        self._pidDSpinbox.pack(side=LEFT, padx=2)
        
        #debug
        debugFrame = tkFrame(self)
        debugFrame.grid(column=0, row=3, sticky="WE")
        
        self._debugMsg = Message(debugFrame, anchor="nw", justify=LEFT, relief=SUNKEN, width=300)
        self._debugMsg.pack(fill=BOTH, expand=1)



    def _start(self):

        self._readDroneConfig()
        
        if Cockpit.JOYSTICK_ENABLED:
            self._joystickManager = JoystickManager.getInstance()
            self._joystickManager.start()
            
            joysticks = self._joystickManager.getJoysticks()
            if len(joysticks) != 0:
                self._joystick = joysticks[0]
                self._joystick.onAxisChanged += self._onJoystickAxisChanged
                self._joystick.onButtonPressed += self._onJoystickButtonPressed
            else:
                self._joystick = None     
        
        
    def _onJoystickAxisChanged(self, sender, index):
        
        if self._started.get() and sender == self._joystick:
            
            axisValue = self._joystick.getAxisValue(index) 
            
            if index == 0:
                
                self._yaw.set(axisValue)
                self._updateTarget()
            
            elif index == 1 and not Cockpit.THROTTLE_BY_USER:
            
                thrust = -axisValue
                self._throttle.set(thrust)            
                self._updateTarget()

            elif index == 2 and Cockpit.THROTTLE_BY_USER:            
            
                rowThrottle = (axisValue + 100.0)/2.0 
                if rowThrottle < 10.0:
                    throttle = rowThrottle * 6.0
                elif rowThrottle < 90.0:
                    throttle = 60.0 + ((rowThrottle - 10.0) / 8.0)
                else:
                    throttle = 70.0 + (rowThrottle - 90.0) * 3.0
                self._throttle.set(throttle)
                self._sendThrottle()
                
            elif index == 3:
                
                x = 196 + axisValue * 2                
                lastCoords = self._shiftCanvas.coords(self._shiftMarker)
                coords = (x, lastCoords[1])                 
                self._plotShiftCanvasMarker(coords)
                
            elif index == 4:
                
                y = 196 + axisValue * 2 
                lastCoords = self._shiftCanvas.coords(self._shiftMarker)
                coords = (lastCoords[0], y)                 
                self._plotShiftCanvasMarker(coords)


    def _onJoystickButtonPressed(self, sender, index):
        
        if sender == self._joystick and index == 7:
            
            if self._started.get() == 0:
                self._startedCB.select()
            else:
                self._startedCB.deselect()
                
            # Tkinter's widgets seem not to be calling the event-handler
            # when they are changed programmatically. Therefore, the 
            # even-handler is called explicitly here.
            self._startedCBChanged()
        
    
    def exit(self):
        
        self._link.send({"key": "close", "data": None})
        
        self._stopUpdateInfoThread()
        
        self._link.close()

        if Cockpit.JOYSTICK_ENABLED:
            self._joystickManager.stop()
        
        self.quit()


    def _updateTarget(self):
        
        markerCoords = self._shiftCanvas.coords(self._shiftMarker)
        coords = ((markerCoords[0] + markerCoords[2]) / 2, (markerCoords[1] + markerCoords[3]) / 2)
        
        self._target[0] = float(coords[1] - 200) / 2.0 # X-axis angle / X-axis acceleration
        self._target[1] = float(coords[0] - 200) / 2.0 # Y-axis angle / Y-axis acceleration
        #Remote control uses clockwise angle, but the drone's referece system uses counter-clockwise angle
        self._target[2] = -self._yaw.get() # Z-axis angular speed
        
        # Z-axis acceleration (thrust). Only when the motor throttle is not controlled by user directly
        if Cockpit.THROTTLE_BY_USER:
            self._target[3] = 0.0
        else:        
            self._target[3] = self._throttle.get()
        
        self._sendTarget() 
    
        
    def _keyDown(self, event):

        if event.keysym == "Escape":            
            self._throttle.set(0)
            self._started.set(0)
            self._thrustScale.config(state=DISABLED)
            self._stopUpdateInfoThread()
            self._sendIsStarted()
            
        elif event.keysym.startswith("Control"):            
            self._controlKeyActive = True
            
        elif not self._controlKeysLocked and self._controlKeyActive:
            
            if event.keysym == "Up":
                self._thrustScaleUp()
                
            elif event.keysym == "Down":
                self._thrustScaleDown()
                
            elif event.keysym == "Left":
                self._yawLeft()
                
            elif event.keysym == "Right":
                self._yawRight()
                
            elif event.keysym == "space":
                self._yawReset()
                if not Cockpit.THROTTLE_BY_USER:
                    self._thrustReset()
                
        elif not self._controlKeysLocked and not self._controlKeyActive:
            
            if event.keysym == "Up":
                self._moveShiftCanvasMarker((0,-5))
                
            elif event.keysym == "Down":
                self._moveShiftCanvasMarker((0,5))
                
            elif event.keysym == "Left":
                self._moveShiftCanvasMarker((-5,0))
                
            elif event.keysym == "Right":
                self._moveShiftCanvasMarker((5,0))
                
            elif event.keysym == "space":
                self._resetShiftCanvasMarker()                
    
    
    def _keyUp(self, eventArgs):
        
        if eventArgs.keysym.startswith("Control"):
            self._controlKeyActive = False
            
    
    def _onMouseButton1(self, eventArgs):

        self._lastMouseCoords = (eventArgs.x, eventArgs.y)

        
    def _onMouseButtonRelease1(self, eventArgs):

        self._shiftCanvas.coords(self._shiftMarker, 196, 196, 204, 204)

    
    def _limitCoordsToSize(self, coords, size, width):
        
        maxSize = size-(width/2.0)
        minSize = -(width/2.0)
        
        if coords[0] > maxSize:
            x = maxSize
        
        elif coords[0] < minSize:
            x = minSize
            
        else:
            x = coords[0]
            
            
        if coords[1] > maxSize:
            y = maxSize
            
        elif coords[1] < minSize:
            y = minSize
            
        else:
            y = coords[1]
            
        
        return (x,y)
    
    
    def _plotShiftCanvasMarker(self, coords):
        
        coords = self._limitCoordsToSize(coords, 400, 8)
        self._shiftCanvas.coords(self._shiftMarker, coords[0], coords[1], coords[0] + 8, coords[1] + 8)
        self._updateTarget()

    
    def _moveShiftCanvasMarker(self, shift):

        lastCoords = self._shiftCanvas.coords(self._shiftMarker)
        newCoords = (lastCoords[0] + shift[0], lastCoords[1] + shift[1])        
        self._plotShiftCanvasMarker(newCoords)
    
    
    def _resetShiftCanvasMarker(self):
    
        self._shiftCanvas.coords(self._shiftMarker, 196, 196, 204, 204)
        self._updateTarget()
        
    
    def _onMouseButton1Motion(self, eventArgs):

        deltaCoords = (eventArgs.x - self._lastMouseCoords[0], eventArgs.y - self._lastMouseCoords[1])
        self._moveShiftCanvasMarker(deltaCoords)
        self._lastMouseCoords = (eventArgs.x, eventArgs.y)
  
      
    def _onMouseDoubleButton1(self, eventArgs):
        
        self._resetShiftCanvasMarker()        
            

    def _onMouseButton3(self, eventArgs):

        self._lastMouseCoords = (eventArgs.x, eventArgs.y)
        self._mouseDirection = Cockpit.DIR_NONE

        
    def _onMouseButtonRelease3(self, eventArgs):

        self._shiftCanvas.coords(self._shiftMarker, 196, 196, 204, 204)

        
    def _onMouseButton3Motion(self, eventArgs):

        deltaCoords = (eventArgs.x - self._lastMouseCoords[0], eventArgs.y - self._lastMouseCoords[1])

        if self._mouseDirection == Cockpit.DIR_NONE:
            if abs(deltaCoords[0]) > abs(deltaCoords[1]):
                self._mouseDirection = Cockpit.DIR_HORIZONTAL
            else:
                self._mouseDirection = Cockpit.DIR_VERTICAL

        if self._mouseDirection == Cockpit.DIR_HORIZONTAL:
            deltaCoords = (deltaCoords[0], 0)
        else:
            deltaCoords = (0, deltaCoords[1])

        self._moveShiftCanvasMarker(deltaCoords)
        self._lastMouseCoords = (eventArgs.x, eventArgs.y)
        
    
    def _thrustScaleUp(self):

        #TODO: 20160526 DPM: El valor de incremento de aceleración (1.0) puede ser muy alto
        if self._started.get(): 
            newValue = self._thrustScale.get() \
                + (Cockpit.THROTTLE_RESOLUTION if Cockpit.THROTTLE_BY_USER else 1.0)
            self._thrustScale.set(newValue)
            
            self._updateTarget()
    
    
    def _thrustScaleDown(self):
        
        #TODO: 20160526 DPM: El valor de decremento de aceleración (1.0) puede ser muy alto
        if self._started.get():
            newValue = self._thrustScale.get() \
                - (Cockpit.THROTTLE_RESOLUTION if Cockpit.THROTTLE_BY_USER else 1.0)
            self._thrustScale.set(newValue)
            
            self._updateTarget()
            
    
    def _thrustReset(self):
        
        if self._started.get():
            self._thrustScale.set(0.0)
            
            self._updateTarget()
            
            
    def _onThrustScaleDoubleButton1(self, eventArgs):
        
        self._thrustReset()
        
        return "break"
        
    
    def _yawRight(self):
        
        newValue = self._yaw.get() + 1
        self._yaw.set(newValue)
        self._updateTarget()
            

    def _yawLeft(self):
        
        newValue = self._yaw.get() - 1
        self._yaw.set(newValue)
        self._updateTarget()
        
        
    def _yawReset(self):
        
        self._yaw.set(0)
        self._updateTarget()
        
        
    def _onMouseWheelUp(self, eventArgs):
        
        if not self._controlKeyActive:
            self._thrustScaleUp()
            
        else:
            self._yawRight()
            

    def _onMouseWheelDown(self, eventArgs):

        if not self._controlKeyActive:
            self._thrustScaleDown()
            
        else:
            self._yawLeft()
    

    def _onMouseWheel(self, eventArgs):

        factor = eventArgs.delta/(1200.0 if Cockpit.THROTTLE_BY_USER and not self._controlKeyActive else 120.0)

        if not self._controlKeyActive:
        
            if self._started.get():
                newValue = self._thrustScale.get() + factor 
                self._thrustScale.set(newValue)
                
                self._updateTarget()
        else:
            newValue = self._yaw.get() + factor
            self._yaw.set(newValue)
            self._updateTarget()

    
    def _onYawScaleChanged(self, eventArgs):
        
        self._updateTarget()
    
    
    def _onYawScaleDoubleButton1(self, eventArgs):
        
        self._yawReset()
        
        return "break"
        
    
    def _startedCBChanged(self):
        
        if not self._started.get():
            self._throttle.set(0)
            self._thrustScale.config(state=DISABLED)            
            #self._integralsCB.config(state=DISABLED)
            self._stopUpdateInfoThread()
        else:
            self._thrustScale.config(state="normal")            
            #self._integralsCB.config(state="normal")
            self._startUpdateInfoThread()
            
        self._sendIsStarted()
     
    
#     def _integralsCBChanged(self):
#     
#         self._link.send({"key": "integrals", "data":self._integralsEnabled.get() != 0})
#             
    
     
    def _onThrustScaleChanged(self, eventArgs):
        
        if Cockpit.THROTTLE_BY_USER:
            
            self._sendThrottle()
        
        else:
        
            self._updateTarget()


    def _sendThrottle(self):
        
        self._link.send({"key": "throttle", "data": self._throttle.get()})
    

    def _sendTarget(self):
        
        self._link.send({"key": "target", "data": self._target})
        
        
    def _sendIsStarted(self):
        
        isStarted = self._started.get() != 0        
        self._link.send({"key": "is-started", "data": isStarted})
        

    def _sendPidCalibrationData(self):

        if self._pidSelected.get() != "--" and self._axisSelected.get() != "--":

            pidData = {
                "pid": self._pidSelected.get(),
                "axis": self._axisSelected.get(), 
                "p": float(self._pidPSpinbox.get()),
                "i": float(self._pidISpinbox.get()),
                "d": float(self._pidDSpinbox.get())}
        
            self._link.send({"key": "pid-calibration", "data": pidData})


    def _updatePidCalibrationData(self):

        pid = self._pidSelected.get()
        axis = self._axisSelected.get()

        if pid != "--" and axis != "--":
             
            self._pidConstants[pid][axis]["P"] = float(self._pidPSpinbox.get())
            self._pidConstants[pid][axis]["I"] = float(self._pidISpinbox.get())
            self._pidConstants[pid][axis]["D"] = float(self._pidDSpinbox.get())
            

    def _readDroneConfig(self):

        self._link.send({"key": "read-drone-config", "data": None}, self._onDroneConfigRead)


    def _readDroneState(self):
        
        if not self._readingState:
            self._readingState = True
            self._link.send({"key": "read-drone-state", "data": None}, self._onDroneStateRead)


    def _readPidConfigItem(self, message, cockpitKey, axises, configKeys):
        
        for i in range(len(axises)):
            for j in range(len(Cockpit.PID_KEYS)):
                self._pidConstants[cockpitKey][axises[i]][Cockpit.PID_KEYS[j]] = message[configKeys[j]][i]
                

    def _onDroneConfigRead(self, message):

        #TODO Show current configuration within the GUI (at least relevant settings)
        if message:
            
            #Angle-speeds
            self._readPidConfigItem(message, Cockpit.KEY_ANG_SPEED, ["X", "Y", "Z"], \
                                    [Configuration.PID_ANGLES_SPEED_KP, \
                                     Configuration.PID_ANGLES_SPEED_KI, \
                                     Configuration.PID_ANGLES_SPEED_KD])
            
            #Angles
            self._readPidConfigItem(message, Cockpit.KEY_ANGLES, ["X", "Y"], \
                                    [Configuration.PID_ANGLES_KP, \
                                     Configuration.PID_ANGLES_KI, \
                                     Configuration.PID_ANGLES_KD])
                        
            #Accels
            self._readPidConfigItem(message, Cockpit.KEY_ACCEL, ["X", "Y", "Z"], \
                                    [Configuration.PID_ACCEL_KP, \
                                     Configuration.PID_ACCEL_KI, \
                                     Configuration.PID_ACCEL_KD])
        

    def _onDroneStateRead(self, state):
        
        if state:
            
            for index in range(4):
                self._throttleTexts[index].set("{0:.3f}".format(state["_throttles"][index]))
                
            for index in range(3):
                self._accelTexts[index].set("{0:.3f}".format(state["_accels"][index]))
                self._angleTexts[index].set("{0:.3f}".format(state["_angles"][index]))
                
            currentPeriod = state["_currentPeriod"]
            if currentPeriod > 0.0:
                
                freq = 1.0/currentPeriod                
                self._loopRateText.set("{0:.3f}".format(freq))
                
            else:
                self._loopRateText.set("--")
                
        else:
            self._stopUpdateInfoThread()
            
        self._readingState = False
   

    def _onPidSpinboxChanged(self):

        self._updatePidCalibrationData()
        self._sendPidCalibrationData()

    
    def _onPidListBoxChanged(self, pid):
        
        self._axisSelected.set("--")
        
        self._pidPString.set("--")
        self._pidIString.set("--")
        self._pidDString.set("--")

        self._pidPSpinbox.config(state=DISABLED)
        self._pidISpinbox.config(state=DISABLED)
        self._pidDSpinbox.config(state=DISABLED)

        self._selectedPidConstats = pid

        if pid == "--":
            self._axisListBox.config(state=DISABLED)
            self._controlKeysLocked = False
                       
        else:
            self._axisListBox.config(state="normal")
            self._controlKeysLocked = True


    def _onAxisListBoxChanged(self, axis):
        
        if axis == "--" or (self._selectedPidConstats == Cockpit.KEY_ANGLES and axis == "Z"):
            
            self._pidPString.set("--")
            self._pidIString.set("--")
            self._pidDString.set("--")
            
            self._pidPSpinbox.config(state=DISABLED)
            self._pidISpinbox.config(state=DISABLED)
            self._pidDSpinbox.config(state=DISABLED)
            
            self._controlKeysLocked = axis != "--"
            
        else:
            
            self._pidPString.set("{:.2f}".format(self._pidConstants[self._selectedPidConstats][axis]["P"]))
            self._pidIString.set("{:.2f}".format(self._pidConstants[self._selectedPidConstats][axis]["I"]))
            self._pidDString.set("{:.2f}".format(self._pidConstants[self._selectedPidConstats][axis]["D"]))
            
            self._pidPSpinbox.config(state="normal")
            self._pidISpinbox.config(state="normal")
            self._pidDSpinbox.config(state="normal")
            
            self._controlKeysLocked = True

            
    def _updateInfo(self):
        
        while self._updateInfoThreadRunning:
            
            self._readDroneState()
            
            time.sleep(1.0)
            

    def _startUpdateInfoThread(self):
        
        self._updateInfoThreadRunning = True
        if not self._updateInfoThread.isAlive():                
            self._updateInfoThread.start()
        
            
    def _stopUpdateInfoThread(self):
        
        self._updateInfoThreadRunning = False
        if self._updateInfoThread.isAlive():
            self._updateInfoThread.join()
コード例 #23
0
ファイル: art.py プロジェクト: donkirkby/donkirkby
class Application(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.DEFAULT_SECTION = 'DEFAULT'
        
        self.grid()
        self.createWidgets()

    def createWidgets(self):
        self.__config = ConfigParser.ConfigParser()
        self.__config.set(self.DEFAULT_SECTION, 'input', '~/input.jpg')
        self.__config.set(self.DEFAULT_SECTION, 'inverted', '0')
        self.__config.set(self.DEFAULT_SECTION, 'output', '~/output.svg')
        self.__config.set(self.DEFAULT_SECTION, 'preamble', 'G17 G21 G90 G64 P0.1 S4500 M3 M8')
        self.__config.set(self.DEFAULT_SECTION, 'size', '256')
        self.__config.set(self.DEFAULT_SECTION, 'scale', '1')
        self.__config.set(self.DEFAULT_SECTION, 'width', '152.4')
        self.__config.set(self.DEFAULT_SECTION, 'height', '101.6')
        self.__config.set(self.DEFAULT_SECTION, 'columns', '3')
        self.__config.set(self.DEFAULT_SECTION, 'rows', '2')
        self.__config.set(self.DEFAULT_SECTION, 'margin', '10.0')
        self.__config.set(self.DEFAULT_SECTION, 'thickness', '6.35')
        self.__config.set(self.DEFAULT_SECTION, 'tool_diameter', '3.175')
        self.__config.read(os.path.expanduser('~/.py-art.cfg'))
        
        self.EntryFrame = Frame(self,bd=5)
        self.EntryFrame.grid(row=0, column=1)
        
        i = 0
        Label(self.EntryFrame, text='Engrave an image\n').grid(row=i, column=0, columnspan=2)

        i += 1
        Label(self.EntryFrame, text='Input file').grid(row=i, column=0, sticky=E)

        self.InputVar = StringVar()
        self.InputVar.set(self.__config.get(self.DEFAULT_SECTION, 'input'))
        Entry(self.EntryFrame, textvariable=self.InputVar ,width=50).grid(row=i, column=1)

        i += 1
        Label(self.EntryFrame, text='Size').grid(row=i, column=0, sticky=E)

        self.SizeVar = IntVar()
        self.SizeVar.set(self.__config.get(self.DEFAULT_SECTION, 'size'))
        Entry(self.EntryFrame, textvariable=self.SizeVar ,width=50).grid(row=i, column=1)

        i += 1
        Label(self.EntryFrame, text='Scale').grid(row=i, column=0, sticky=E)

        self.ScaleVar = IntVar()
        self.ScaleVar.set(self.__config.get(self.DEFAULT_SECTION, 'scale'))
        Entry(self.EntryFrame, textvariable=self.ScaleVar ,width=50).grid(row=i, column=1)

        i += 1
        Label(self.EntryFrame, text='Rows').grid(row=i, column=0, sticky=E)

        self.RowsVar = IntVar()
        self.RowsVar.set(self.__config.get(self.DEFAULT_SECTION, 'rows'))
        Entry(self.EntryFrame, textvariable=self.RowsVar ,width=50).grid(row=i, column=1)

        i += 1
        Label(self.EntryFrame, text='Columns').grid(row=i, column=0, sticky=E)

        self.ColumnsVar = IntVar()
        self.ColumnsVar.set(self.__config.get(self.DEFAULT_SECTION, 'columns'))
        Entry(self.EntryFrame, textvariable=self.ColumnsVar ,width=50).grid(row=i, column=1)

        i += 1
        Label(self.EntryFrame, text='Image effects').grid(row=i, column=0, sticky=E)
        self.InvertedVar = IntVar()
        self.InvertedVar.set(self.__config.getint(self.DEFAULT_SECTION, 'inverted'))
        Checkbutton(self.EntryFrame, text='Inverted', variable=self.InvertedVar).grid(row=i, column=1,sticky=W)

        if IN_AXIS:
            i += 1
            Label(self.EntryFrame, text='Preamble').grid(row=i, column=0, sticky=E)

            self.PreambleVar = StringVar()
            self.PreambleVar.set(self.__config.get(self.DEFAULT_SECTION, 'preamble'))
            Entry(self.EntryFrame, textvariable=self.PreambleVar ,width=50).grid(row=i, column=1)

            i += 1
            Label(self.EntryFrame, text='Width').grid(row=i, column=0, sticky=E)

            self.WidthVar = DoubleVar()
            self.WidthVar.set(self.__config.get(self.DEFAULT_SECTION, 'width'))
            Entry(self.EntryFrame, textvariable=self.WidthVar ,width=50).grid(row=i, column=1)

            i += 1
            Label(self.EntryFrame, text='Height').grid(row=i, column=0, sticky=E)

            self.HeightVar = DoubleVar()
            self.HeightVar.set(self.__config.get(self.DEFAULT_SECTION, 'height'))
            Entry(self.EntryFrame, textvariable=self.HeightVar ,width=50).grid(row=i, column=1)

            i += 1
            Label(self.EntryFrame, text='Margin').grid(row=i, column=0, sticky=E)

            self.MarginVar = DoubleVar()
            self.MarginVar.set(self.__config.get(self.DEFAULT_SECTION, 'margin'))
            Entry(self.EntryFrame, textvariable=self.MarginVar ,width=50).grid(row=i, column=1)

            i += 1
            Label(self.EntryFrame, text='Tool diameter').grid(row=i, column=0, sticky=E)

            self.ToolDiameterVar = DoubleVar()
            self.ToolDiameterVar.set(self.__config.get(self.DEFAULT_SECTION, 'tool_diameter'))
            Entry(self.EntryFrame, textvariable=self.ToolDiameterVar ,width=50).grid(row=i, column=1)

            i += 1
            Label(self.EntryFrame, text='Thickness').grid(row=i, column=0, sticky=E)

            self.ThicknessVar = DoubleVar()
            self.ThicknessVar.set(self.__config.get(self.DEFAULT_SECTION, 'thickness'))
            Entry(self.EntryFrame, textvariable=self.ThicknessVar ,width=50).grid(row=i, column=1)

            i += 1
            Button(self.EntryFrame, text='Trace boundary in AXIS and Quit',command=self.WriteBoundaryTraceToAxis).grid(row=i, column=1, sticky=E)
            
            i += 1
            Button(self.EntryFrame, text='Engrave in AXIS and Quit',command=self.WriteEngravingToAxis).grid(row=i, column=1, sticky=E)

            i += 1
            Button(self.EntryFrame, text='Cut boundary in AXIS and Quit',command=self.WriteBoundaryCutToAxis).grid(row=i, column=1, sticky=E)
        else:
            i += 1
            Label(self.EntryFrame, text='Output file').grid(row=i, column=0, sticky=E)
    
            self.OutputVar = StringVar()
            self.OutputVar.set(self.__config.get(self.DEFAULT_SECTION, 'output'))
            Entry(self.EntryFrame, textvariable=self.OutputVar ,width=50).grid(row=i, column=1)
    
            i += 1
            Button(self.EntryFrame, text='Write to SVG and Quit', command=self.WriteToSvg).grid(row=i, column=1, sticky=E)
        
        
    def WriteConfig(self):
        with open(os.path.expanduser('~/.py-art.cfg'), 'wb') as configfile:
            self.__config.set(self.DEFAULT_SECTION, 'input', self.InputVar.get())
            self.__config.set(self.DEFAULT_SECTION, 'inverted', self.InvertedVar.get())
            self.__config.set(self.DEFAULT_SECTION, 'size', self.SizeVar.get())
            self.__config.set(self.DEFAULT_SECTION, 'scale', self.ScaleVar.get())
            self.__config.set(self.DEFAULT_SECTION, 'rows', self.RowsVar.get())
            self.__config.set(self.DEFAULT_SECTION, 'columns', self.ColumnsVar.get())
            if IN_AXIS:
                self.__config.set(self.DEFAULT_SECTION, 'height', self.ToolDiameterVar.get())
                self.__config.set(self.DEFAULT_SECTION, 'margin', self.ToolDiameterVar.get())
                self.__config.set(self.DEFAULT_SECTION, 'preamble', self.PreambleVar.get())
                self.__config.set(self.DEFAULT_SECTION, 'tool_diameter', self.ToolDiameterVar.get())
                self.__config.set(self.DEFAULT_SECTION, 'thickness', self.ThicknessVar.get())
                self.__config.set(self.DEFAULT_SECTION, 'width', self.ToolDiameterVar.get())
            else:
                self.__config.set(self.DEFAULT_SECTION, 'output', self.OutputVar.get())
                
            self.__config.write(configfile)
        

    def DisplayAndQuit(self, display, size):
        image = Image.open(os.path.expanduser(self.InputVar.get()))
        rows = self.RowsVar.get()
        columns = self.ColumnsVar.get()
        curve = Curve(display, image)
        curve.scale = (self.ScaleVar.get(), self.ScaleVar.get())
        curve.is_inverted = self.InvertedVar.get()
        curve.calculate_levels(5, 0, 0, columns * size, rows * size)
        self.display_nxm_with_border(rows, columns, size, display, curve)
        display.close()
        self.WriteConfig()
        self.quit()

    def WriteToSvg(self):
        size = int(self.SizeVar.get())
        display = SvgDisplay(
            os.path.expanduser(self.OutputVar.get()), 
            self.ColumnsVar.get()*size + 22, 
            self.RowsVar.get()*size + 22)
#        display = TurtleDisplay()
        display.offset = (11,11)
        
        self.DisplayAndQuit(display, size)
        

    def WriteBoundaryMoves(self, display, cut_depth):
        width = self.WidthVar.get()
        height = self.HeightVar.get()
        tool_radius = self.ToolDiameterVar.get() / 2
        corner_radius = 1.0
        display.add_point((-tool_radius, -corner_radius))
        print "g1 y%f z%f" % (-height + corner_radius, -cut_depth)
        print "g3 x%f y%f i%f" % (
            corner_radius, 
            -height - tool_radius, 
            tool_radius + corner_radius)
        print "g1 x%f" % (width - corner_radius)
        print "g3 x%f y%f j%f" % (
            width + tool_radius, 
            -height + corner_radius, 
            tool_radius + corner_radius)
        print "g1 y%f" % (-corner_radius)
        print "g3 x%f y%f i%f" % (
            width - corner_radius, 
            tool_radius, 
            -tool_radius - corner_radius)
        print "g1 x%f" % (corner_radius)
        print "g3 x%f y%f j%f" % (
            -tool_radius, 
            -corner_radius, 
            -tool_radius - corner_radius)

    def WriteBoundaryTraceToAxis(self):
        display = self.CreateGCodeDisplay()
        self.WriteBoundaryMoves(display, cut_depth=0)
        display.close_curve()
        display.close()

        self.WriteConfig()
        self.quit()
        
    def WriteBoundaryCutToAxis(self):
        display = self.CreateGCodeDisplay()
        tool_radius = self.ToolDiameterVar.get() / 2
        thickness = self.ThicknessVar.get()
        zstart = 0.05
        zstep = -tool_radius / 2
        zstop = -thickness - 0.5
        display.depth = -zstart
        
        for cut_depth in range(zstart, zstop, zstep):
            self.WriteBoundaryMoves(display, cut_depth)
        
        display.close_curve()
        display.close()

        self.WriteConfig()
        self.quit()

    def CreateGCodeDisplay(self):
        display = GCodeDisplay()
        display.preamble = self.PreambleVar.get()
        display.feed_rate = 1200
        return display

    def WriteEngravingToAxis(self):
        display = self.CreateGCodeDisplay()
        width = self.WidthVar.get()
        height = self.HeightVar.get()
        margin = self.MarginVar.get()
        size = self.SizeVar.get()
        xscale = (width - 2*margin) / (3*size)
        yscale = (height - 2*margin) / (2*size)
        scale = min(xscale, yscale)
        xoff = width/2 + 1.5*size*scale
        yoff = -height/2 + size*scale
        display.scale = (-scale, -scale)
        display.offset = (xoff, yoff)
        
        self.DisplayAndQuit(display, size)

    def display_rectangle(self, size, display, curve):
        display.add_point((-1, -1))
        display.add_point((-1, size))
        display.add_point((size * 2, size))
        display.add_point((size * 2, -1))
        display.close_curve()
        
        curve.draw_cell((size - 1, 0), (size - 1, size - 1), -1)
        display.add_point((size, size - 1))
        curve.draw_cell((size, size - 1), (size, 0), -1)
        display.add_point((size - 1, 0))
        display.close_curve()
    
    def display_square(self, size, display, curve):
        display.add_point((-1, -1))
        display.add_point((-1, size))
        display.add_point((size, size))
        display.add_point((size, -1))
        display.close_curve()
        
        curve.draw_cell((size/2 - 1, 0), (size/2 - 1, size/2 - 1), -1)
        display.add_point((size/2 - 1, size/2))
        curve.draw_cell((size/2 - 1, size/2), (size/2 - 1, size-1), -1)
        display.add_point((size/2, size - 1))
        curve.draw_cell((size/2, size - 1), (size/2, size/2), -1)
        display.add_point((size/2, size/2 - 1))
        curve.draw_cell((size/2, size/2 - 1), (size/2, 0), -1)
        display.add_point((size/2 - 1, 0))
        display.close_curve()
    
    def display_nxm_with_border(self, n, m, size, display, curve):
        """ Draw the curve using n rows and m columns.
        
        n - the number of rows must be even.
        m - the number of columns.
        size - the size of each row and column.
        display - the place to draw the curve.
        curve - the curve to draw.
        """
        display.add_point((-1, n/2*size))
        display.add_point((-1, n*size))
        display.add_point((m*size, n*size))
        display.add_point((m*size, -1))
        display.add_point((-1, -1))
        display.add_point((-1, n/2*size - 1))
        
        self.display_nxm(n, m, size, display, curve)

    def display_nxm(self, n, m, size, display, curve):
        for i in range(n/2 - 1):
            display.add_point((0, (n/2 - i)*size - 1))
            curve.draw_cell((0, (n/2 - i)*size - 1), (0, (n/2 - i - 1)*size), -1)

        for i in range(m):            
            display.add_point((i*size, size - 1))
            curve.draw_cell((i*size, size - 1), ((i+1)*size - 1, size - 1), 1)

        for i in range(n/2 - 1):
            for j in range(m-2):
                display.add_point(((m-j)*size - 1, (2*i+1)*size))
                curve.draw_cell(
                    ((m-j)*size - 1, (2*i+1)*size), 
                    ((m-j-1)*size, (2*i+1)*size), 
                    1)
            for j in range(2):
                display.add_point((2*size - 1, (2*i+j+1)*size))
                curve.draw_cell(
                    (2*size - 1, (2*i+j+1)*size), 
                    (2*size - 1, (2*i+j+2)*size - 1), 
                    -1)
            for j in range(m-2):
                display.add_point(((2+j)*size, (2*i+3)*size))
                curve.draw_cell(
                    ((2+j)*size, (2*i+3)*size - 1), 
                    ((3+j)*size-1, (2*i+3)*size - 1), 
                    1)

        for i in range(m):            
            display.add_point(((m-i)*size-1, (n-1)*size))
            curve.draw_cell(
                ((m-i)*size-1, (n-1)*size), 
                ((m-i-1)*size, (n-1)*size), 
                1)

        for i in range(n/2 - 1):
            display.add_point((0, (n - i - 1)*size - 1))
            curve.draw_cell((0, (n - i - 1)*size - 1), (0, (n - i - 2)*size), -1)

        display.close_curve()
コード例 #24
0
class Scope:
    """
    Scope is class used to make a scope on grid layout with Tkinterface.

    """
    def __init__(self,
                 root,
                 data,
                 row0,
                 col0,
                 rowspan=10,
                 colspan=6,
                 showpower=True,
                 horizontaljoin=False,
                 buttoncallback=None):
        """
        Create a scope interface.

        :param root: is the main window
        :param data: is the data to display (update if enabled)
        :param row0: is the beginning row as whitch the scope should be placed on the grid layout
        :param col0: is the beginning column as whitch the scope should be placed on the grid layout
        :param rowspan: is how many rows should be used to draw the whole scope
        :param colspan: is how many columns should be used to draw the whole scope
        :param showpower: is a boolean field used to show the power (not required in wave editor) 
        :param horizontaljoin: if true the points are joined through horizontal segments then vertica 
        (for waveditor) if it is false they are just joined as useful in the scope view
        :param buttoncallback: it is the function to provide if the user can manage the graphs 
        changing point (for wave editor)
        :returns: a new instance of scope

        """
        self.root = root

        row = row0

        rowspan -= 4  #those lines are used for inputs

        self.scopetube = Scopetube(root, data, horizontaljoin, self.newratios,
                                   buttoncallback)
        self.scopetube.grid(row=row,
                            column=col0,
                            columnspan=colspan,
                            rowspan=rowspan,
                            sticky=E + W)

        row += rowspan
        rowspan = 1
        colspan = 1
        insertlabelrow(root, row, col0,
                       (("Y [V/div]: ", VCOL), None, ("Y [A/div]: ", CCOL)), E)
        self.dvarvdiv = DoubleVar()
        self.dvarvdiv.set(1.)
        self.dvarcdiv = DoubleVar()
        self.dvarcdiv.set(1.)
        entries = insertentryrow(root, row, col0,
                                 (None, self.dvarvdiv, None, self.dvarcdiv),
                                 'right', W)
        self.dvarpdiv = DoubleVar()
        self.dvarpdiv.set(1.)
        if showpower:
            insertlabelrow(root, row, col0,
                           (None, None, None, None, ("Y [W/div]: ", PCOL)), E)
            entries += insertentryrow(
                root, row, col0, (None, None, None, None, None, self.dvarpdiv),
                'right', W)

        row += rowspan
        insertlabelrow(root, row, col0,
                       (("Y0 [V]: ", VCOL), None, ("Y0 [A]: ", CCOL)), E)
        self.dvarv0 = DoubleVar()
        self.dvarv0.set(0.)
        self.dvarc0 = DoubleVar()
        self.dvarc0.set(0.)
        entries += insertentryrow(root, row, col0,
                                  (None, self.dvarv0, None, self.dvarc0),
                                  'right', W)
        self.dvarp0 = DoubleVar()
        self.dvarp0.set(0.)
        if showpower:
            insertlabelrow(root, row, col0,
                           (None, None, None, None, ("Y0 [W]: ", PCOL)), E)
            entries += insertentryrow(
                root, row, col0, (None, None, None, None, None, self.dvarp0),
                'right', W)

        row += rowspan
        rowspan = 1
        colspan = 2
        col = col0
        self.ivarvena = IntVar()
        self.ivarvena.set(1)
        Checkbutton(root,
                    variable=self.ivarvena,
                    text='Voltage show',
                    foreground=VCOL,
                    command=self.entbndcmdbutscpupdt).grid(row=row,
                                                           column=col,
                                                           columnspan=colspan,
                                                           sticky=E + W)
        col += colspan
        self.ivarcena = IntVar()
        self.ivarcena.set(1)
        Checkbutton(root,
                    variable=self.ivarcena,
                    text='Current show',
                    foreground=CCOL,
                    command=self.entbndcmdbutscpupdt).grid(row=row,
                                                           column=col,
                                                           columnspan=colspan,
                                                           sticky=E + W)
        self.ivarpena = IntVar()
        self.ivarpena.set(0)
        if showpower:
            col += colspan
            self.ivarpena.set(1)
            Checkbutton(root,
                        variable=self.ivarpena,
                        text='Power show',
                        foreground=PCOL,
                        command=self.entbndcmdbutscpupdt).grid(
                            row=row,
                            column=col,
                            columnspan=colspan,
                            sticky=E + W)

        row += rowspan
        col = col0
        insertlabelrow(root, row, col0, ("X [s/div]: ", None, "X0 [s]: "), E)
        self.dvartdiv = DoubleVar()
        self.dvartdiv.set(60.)
        self.dvart0 = DoubleVar()
        self.dvart0.set(0.)
        entries += insertentryrow(root, row, col0,
                                  (None, self.dvartdiv, None, self.dvart0),
                                  'right', W)

        for e in entries:
            e.bind('<FocusOut>', self.entbndcmdbutscpupdt)
            e.bind('<Return>', self.entbndcmdbutscpupdt)

        self.varlist = (self.ivarvena, self.ivarcena, self.ivarpena,
                        self.dvarvdiv, self.dvarcdiv, self.dvarpdiv,
                        self.dvarv0, self.dvarc0, self.dvarp0, self.dvartdiv,
                        self.dvart0)

        self.update()

    def entbndcmdbutscpupdt(self, *event):
        """
        Updates scope view ratios when something changes on any of the setting.
        
        The change is detected by RETURN pressure or field change

        :param event: may the event that made the change (not used), added to be used as command/event call back

        """
        self.scopetube.setratios([e.get() for e in self.varlist])
        self.scopetube.redraw()

    def newratios(self, ratios):
        """
        Updates scope ratios.
        
        Used by scopetube to update the ratios base on the mouse actions.

        :param ratios: is the list of ratios to update: v, c, p enables; v, c, p initial value; v, c, p divisions; t initial value and divisions.

        """
        for var, val in zip(self.varlist, ratios):
            var.set(val)

    def update(self):
        """
        Updates scope tube view based on the dimensions of the screen.
        
        """
        self.scopetube.update()
        self.entbndcmdbutscpupdt(None)

    def resetpoints(self):
        """
        Delete all the waveform points .
        
        """
        self.scopetube.resetpoints()

    def addpoint(self, p):
        """
        Add a new point to the waveform.
        
        """
        self.scopetube.addpoint(p)

    def redraw(self):
        """
        Redraw the scopetube.
        
        """
        self.scopetube.redraw()

    def sampletime(self):
        """
        Gets the scopetube suggested sample time. 
        
        That is the sample time required to have a sample for every pixel.
        
        """
        return self.scopetube.sampletime()

    def load(self, fname):
        """
        Load a waveform saved before (uses CSV files)
        
        :param fname: the file name from which retrieve the waveform
        """

        self.scopetube.load(fname)

    def save(self, fname):
        """
        Save the waveform currently in memory (uses CSV files)
        
        :param fname: the file name in which save the waveform
        """
        self.scopetube.save(fname)
コード例 #25
0
def on_entermom(event):
    """shows an explanation if cursor placed over question mark"""
    infomom.place(relx=0.545, rely=0.675, anchor=NW)


def on_leavemom(event):
    """hides explanation if cursor not placed over question mark"""
    infomom.place_forget()


qmom.bind("<Enter>", on_entermom)
qmom.bind("<Leave>", on_leavemom)  #bind functions

#Percentage of data to analize:
percentg_val = DoubleVar()
percentg_val.set(0)  #Initialize a value

#Define and show slider
PercentgEntry = Scale(frame1,
                      label="Percentage of data to analize:",
                      bg="LightCyan2",
                      from_=0,
                      to=100,
                      orient=HORIZONTAL,
                      length=300,
                      resolution=0.5,
                      variable=percentg_val)
PercentgEntry.grid(row=17, column=0, columnspan=2, sticky=W)

#Button to open root browser
latestThread = None  #analysis thread
コード例 #26
0
ファイル: art.py プロジェクト: guiltedom/donkirkby
class Application(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.DEFAULT_SECTION = 'DEFAULT'

        self.grid()
        self.createWidgets()

    def createWidgets(self):
        self.__config = ConfigParser.ConfigParser()
        self.__config.set(self.DEFAULT_SECTION, 'input', '~/input.jpg')
        self.__config.set(self.DEFAULT_SECTION, 'inverted', '0')
        self.__config.set(self.DEFAULT_SECTION, 'output', '~/output.svg')
        self.__config.set(self.DEFAULT_SECTION, 'preamble',
                          'G17 G21 G90 G64 P0.1 S4500 M3 M8')
        self.__config.set(self.DEFAULT_SECTION, 'size', '256')
        self.__config.set(self.DEFAULT_SECTION, 'scale', '1')
        self.__config.set(self.DEFAULT_SECTION, 'width', '152.4')
        self.__config.set(self.DEFAULT_SECTION, 'height', '101.6')
        self.__config.set(self.DEFAULT_SECTION, 'columns', '3')
        self.__config.set(self.DEFAULT_SECTION, 'rows', '2')
        self.__config.set(self.DEFAULT_SECTION, 'margin', '10.0')
        self.__config.set(self.DEFAULT_SECTION, 'thickness', '6.35')
        self.__config.set(self.DEFAULT_SECTION, 'tool_diameter', '3.175')
        self.__config.read(os.path.expanduser('~/.py-art.cfg'))

        self.EntryFrame = Frame(self, bd=5)
        self.EntryFrame.grid(row=0, column=1)

        i = 0
        Label(self.EntryFrame, text='Engrave an image\n').grid(row=i,
                                                               column=0,
                                                               columnspan=2)

        i += 1
        Label(self.EntryFrame, text='Input file').grid(row=i,
                                                       column=0,
                                                       sticky=E)

        self.InputVar = StringVar()
        self.InputVar.set(self.__config.get(self.DEFAULT_SECTION, 'input'))
        Entry(self.EntryFrame, textvariable=self.InputVar,
              width=50).grid(row=i, column=1)

        i += 1
        Label(self.EntryFrame, text='Size').grid(row=i, column=0, sticky=E)

        self.SizeVar = IntVar()
        self.SizeVar.set(self.__config.get(self.DEFAULT_SECTION, 'size'))
        Entry(self.EntryFrame, textvariable=self.SizeVar,
              width=50).grid(row=i, column=1)

        i += 1
        Label(self.EntryFrame, text='Scale').grid(row=i, column=0, sticky=E)

        self.ScaleVar = IntVar()
        self.ScaleVar.set(self.__config.get(self.DEFAULT_SECTION, 'scale'))
        Entry(self.EntryFrame, textvariable=self.ScaleVar,
              width=50).grid(row=i, column=1)

        i += 1
        Label(self.EntryFrame, text='Rows').grid(row=i, column=0, sticky=E)

        self.RowsVar = IntVar()
        self.RowsVar.set(self.__config.get(self.DEFAULT_SECTION, 'rows'))
        Entry(self.EntryFrame, textvariable=self.RowsVar,
              width=50).grid(row=i, column=1)

        i += 1
        Label(self.EntryFrame, text='Columns').grid(row=i, column=0, sticky=E)

        self.ColumnsVar = IntVar()
        self.ColumnsVar.set(self.__config.get(self.DEFAULT_SECTION, 'columns'))
        Entry(self.EntryFrame, textvariable=self.ColumnsVar,
              width=50).grid(row=i, column=1)

        i += 1
        Label(self.EntryFrame, text='Image effects').grid(row=i,
                                                          column=0,
                                                          sticky=E)
        self.InvertedVar = IntVar()
        self.InvertedVar.set(
            self.__config.getint(self.DEFAULT_SECTION, 'inverted'))
        Checkbutton(self.EntryFrame,
                    text='Inverted',
                    variable=self.InvertedVar).grid(row=i, column=1, sticky=W)

        if IN_AXIS:
            i += 1
            Label(self.EntryFrame, text='Preamble').grid(row=i,
                                                         column=0,
                                                         sticky=E)

            self.PreambleVar = StringVar()
            self.PreambleVar.set(
                self.__config.get(self.DEFAULT_SECTION, 'preamble'))
            Entry(self.EntryFrame, textvariable=self.PreambleVar,
                  width=50).grid(row=i, column=1)

            i += 1
            Label(self.EntryFrame, text='Width').grid(row=i,
                                                      column=0,
                                                      sticky=E)

            self.WidthVar = DoubleVar()
            self.WidthVar.set(self.__config.get(self.DEFAULT_SECTION, 'width'))
            Entry(self.EntryFrame, textvariable=self.WidthVar,
                  width=50).grid(row=i, column=1)

            i += 1
            Label(self.EntryFrame, text='Height').grid(row=i,
                                                       column=0,
                                                       sticky=E)

            self.HeightVar = DoubleVar()
            self.HeightVar.set(
                self.__config.get(self.DEFAULT_SECTION, 'height'))
            Entry(self.EntryFrame, textvariable=self.HeightVar,
                  width=50).grid(row=i, column=1)

            i += 1
            Label(self.EntryFrame, text='Margin').grid(row=i,
                                                       column=0,
                                                       sticky=E)

            self.MarginVar = DoubleVar()
            self.MarginVar.set(
                self.__config.get(self.DEFAULT_SECTION, 'margin'))
            Entry(self.EntryFrame, textvariable=self.MarginVar,
                  width=50).grid(row=i, column=1)

            i += 1
            Label(self.EntryFrame, text='Tool diameter').grid(row=i,
                                                              column=0,
                                                              sticky=E)

            self.ToolDiameterVar = DoubleVar()
            self.ToolDiameterVar.set(
                self.__config.get(self.DEFAULT_SECTION, 'tool_diameter'))
            Entry(self.EntryFrame, textvariable=self.ToolDiameterVar,
                  width=50).grid(row=i, column=1)

            i += 1
            Label(self.EntryFrame, text='Thickness').grid(row=i,
                                                          column=0,
                                                          sticky=E)

            self.ThicknessVar = DoubleVar()
            self.ThicknessVar.set(
                self.__config.get(self.DEFAULT_SECTION, 'thickness'))
            Entry(self.EntryFrame, textvariable=self.ThicknessVar,
                  width=50).grid(row=i, column=1)

            i += 1
            Button(self.EntryFrame,
                   text='Trace boundary in AXIS and Quit',
                   command=self.WriteBoundaryTraceToAxis).grid(row=i,
                                                               column=1,
                                                               sticky=E)

            i += 1
            Button(self.EntryFrame,
                   text='Engrave in AXIS and Quit',
                   command=self.WriteEngravingToAxis).grid(row=i,
                                                           column=1,
                                                           sticky=E)

            i += 1
            Button(self.EntryFrame,
                   text='Cut boundary in AXIS and Quit',
                   command=self.WriteBoundaryCutToAxis).grid(row=i,
                                                             column=1,
                                                             sticky=E)
        else:
            i += 1
            Label(self.EntryFrame, text='Output file').grid(row=i,
                                                            column=0,
                                                            sticky=E)

            self.OutputVar = StringVar()
            self.OutputVar.set(
                self.__config.get(self.DEFAULT_SECTION, 'output'))
            Entry(self.EntryFrame, textvariable=self.OutputVar,
                  width=50).grid(row=i, column=1)

            i += 1
            Button(self.EntryFrame,
                   text='Write to SVG and Quit',
                   command=self.WriteToSvg).grid(row=i, column=1, sticky=E)

    def WriteConfig(self):
        with open(os.path.expanduser('~/.py-art.cfg'), 'wb') as configfile:
            self.__config.set(self.DEFAULT_SECTION, 'input',
                              self.InputVar.get())
            self.__config.set(self.DEFAULT_SECTION, 'inverted',
                              self.InvertedVar.get())
            self.__config.set(self.DEFAULT_SECTION, 'size', self.SizeVar.get())
            self.__config.set(self.DEFAULT_SECTION, 'scale',
                              self.ScaleVar.get())
            self.__config.set(self.DEFAULT_SECTION, 'rows', self.RowsVar.get())
            self.__config.set(self.DEFAULT_SECTION, 'columns',
                              self.ColumnsVar.get())
            if IN_AXIS:
                self.__config.set(self.DEFAULT_SECTION, 'height',
                                  self.ToolDiameterVar.get())
                self.__config.set(self.DEFAULT_SECTION, 'margin',
                                  self.ToolDiameterVar.get())
                self.__config.set(self.DEFAULT_SECTION, 'preamble',
                                  self.PreambleVar.get())
                self.__config.set(self.DEFAULT_SECTION, 'tool_diameter',
                                  self.ToolDiameterVar.get())
                self.__config.set(self.DEFAULT_SECTION, 'thickness',
                                  self.ThicknessVar.get())
                self.__config.set(self.DEFAULT_SECTION, 'width',
                                  self.ToolDiameterVar.get())
            else:
                self.__config.set(self.DEFAULT_SECTION, 'output',
                                  self.OutputVar.get())

            self.__config.write(configfile)

    def DisplayAndQuit(self, display, size):
        image = Image.open(os.path.expanduser(self.InputVar.get()))
        rows = self.RowsVar.get()
        columns = self.ColumnsVar.get()
        curve = Curve(display, image)
        curve.scale = (self.ScaleVar.get(), self.ScaleVar.get())
        curve.is_inverted = self.InvertedVar.get()
        curve.calculate_levels(5, 0, 0, columns * size, rows * size)
        self.display_nxm_with_border(rows, columns, size, display, curve)
        display.close()
        self.WriteConfig()
        self.quit()

    def WriteToSvg(self):
        size = int(self.SizeVar.get())
        display = SvgDisplay(os.path.expanduser(self.OutputVar.get()),
                             self.ColumnsVar.get() * size + 22,
                             self.RowsVar.get() * size + 22)
        #        display = TurtleDisplay()
        display.offset = (11, 11)

        self.DisplayAndQuit(display, size)

    def WriteBoundaryMoves(self, display, cut_depth):
        width = self.WidthVar.get()
        height = self.HeightVar.get()
        tool_radius = self.ToolDiameterVar.get() / 2
        corner_radius = 1.0
        display.add_point((-tool_radius, -corner_radius))
        print "g1 y%f z%f" % (-height + corner_radius, -cut_depth)
        print "g3 x%f y%f i%f" % (corner_radius, -height - tool_radius,
                                  tool_radius + corner_radius)
        print "g1 x%f" % (width - corner_radius)
        print "g3 x%f y%f j%f" % (width + tool_radius, -height + corner_radius,
                                  tool_radius + corner_radius)
        print "g1 y%f" % (-corner_radius)
        print "g3 x%f y%f i%f" % (width - corner_radius, tool_radius,
                                  -tool_radius - corner_radius)
        print "g1 x%f" % (corner_radius)
        print "g3 x%f y%f j%f" % (-tool_radius, -corner_radius,
                                  -tool_radius - corner_radius)

    def WriteBoundaryTraceToAxis(self):
        display = self.CreateGCodeDisplay()
        self.WriteBoundaryMoves(display, cut_depth=0)
        display.close_curve()
        display.close()

        self.WriteConfig()
        self.quit()

    def WriteBoundaryCutToAxis(self):
        display = self.CreateGCodeDisplay()
        tool_radius = self.ToolDiameterVar.get() / 2
        thickness = self.ThicknessVar.get()
        zstart = 0.05
        zstep = -tool_radius / 2
        zstop = -thickness - 0.5
        display.depth = -zstart

        for cut_depth in range(zstart, zstop, zstep):
            self.WriteBoundaryMoves(display, cut_depth)

        display.close_curve()
        display.close()

        self.WriteConfig()
        self.quit()

    def CreateGCodeDisplay(self):
        display = GCodeDisplay()
        display.preamble = self.PreambleVar.get()
        display.feed_rate = 1200
        return display

    def WriteEngravingToAxis(self):
        display = self.CreateGCodeDisplay()
        width = self.WidthVar.get()
        height = self.HeightVar.get()
        margin = self.MarginVar.get()
        size = self.SizeVar.get()
        xscale = (width - 2 * margin) / (3 * size)
        yscale = (height - 2 * margin) / (2 * size)
        scale = min(xscale, yscale)
        xoff = width / 2 + 1.5 * size * scale
        yoff = -height / 2 + size * scale
        display.scale = (-scale, -scale)
        display.offset = (xoff, yoff)

        self.DisplayAndQuit(display, size)

    def display_rectangle(self, size, display, curve):
        display.add_point((-1, -1))
        display.add_point((-1, size))
        display.add_point((size * 2, size))
        display.add_point((size * 2, -1))
        display.close_curve()

        curve.draw_cell((size - 1, 0), (size - 1, size - 1), -1)
        display.add_point((size, size - 1))
        curve.draw_cell((size, size - 1), (size, 0), -1)
        display.add_point((size - 1, 0))
        display.close_curve()

    def display_square(self, size, display, curve):
        display.add_point((-1, -1))
        display.add_point((-1, size))
        display.add_point((size, size))
        display.add_point((size, -1))
        display.close_curve()

        curve.draw_cell((size / 2 - 1, 0), (size / 2 - 1, size / 2 - 1), -1)
        display.add_point((size / 2 - 1, size / 2))
        curve.draw_cell((size / 2 - 1, size / 2), (size / 2 - 1, size - 1), -1)
        display.add_point((size / 2, size - 1))
        curve.draw_cell((size / 2, size - 1), (size / 2, size / 2), -1)
        display.add_point((size / 2, size / 2 - 1))
        curve.draw_cell((size / 2, size / 2 - 1), (size / 2, 0), -1)
        display.add_point((size / 2 - 1, 0))
        display.close_curve()

    def display_nxm_with_border(self, n, m, size, display, curve):
        """ Draw the curve using n rows and m columns.
        
        n - the number of rows must be even.
        m - the number of columns.
        size - the size of each row and column.
        display - the place to draw the curve.
        curve - the curve to draw.
        """
        display.add_point((-1, n / 2 * size))
        display.add_point((-1, n * size))
        display.add_point((m * size, n * size))
        display.add_point((m * size, -1))
        display.add_point((-1, -1))
        display.add_point((-1, n / 2 * size - 1))

        self.display_nxm(n, m, size, display, curve)

    def display_nxm(self, n, m, size, display, curve):
        for i in range(n / 2 - 1):
            display.add_point((0, (n / 2 - i) * size - 1))
            curve.draw_cell((0, (n / 2 - i) * size - 1),
                            (0, (n / 2 - i - 1) * size), -1)

        for i in range(m):
            display.add_point((i * size, size - 1))
            curve.draw_cell((i * size, size - 1),
                            ((i + 1) * size - 1, size - 1), 1)

        for i in range(n / 2 - 1):
            for j in range(m - 2):
                display.add_point(((m - j) * size - 1, (2 * i + 1) * size))
                curve.draw_cell(((m - j) * size - 1, (2 * i + 1) * size),
                                ((m - j - 1) * size, (2 * i + 1) * size), 1)
            for j in range(2):
                display.add_point((2 * size - 1, (2 * i + j + 1) * size))
                curve.draw_cell((2 * size - 1, (2 * i + j + 1) * size),
                                (2 * size - 1, (2 * i + j + 2) * size - 1), -1)
            for j in range(m - 2):
                display.add_point(((2 + j) * size, (2 * i + 3) * size))
                curve.draw_cell(((2 + j) * size, (2 * i + 3) * size - 1),
                                ((3 + j) * size - 1, (2 * i + 3) * size - 1),
                                1)

        for i in range(m):
            display.add_point(((m - i) * size - 1, (n - 1) * size))
            curve.draw_cell(((m - i) * size - 1, (n - 1) * size),
                            ((m - i - 1) * size, (n - 1) * size), 1)

        for i in range(n / 2 - 1):
            display.add_point((0, (n - i - 1) * size - 1))
            curve.draw_cell((0, (n - i - 1) * size - 1),
                            (0, (n - i - 2) * size), -1)

        display.close_curve()
コード例 #27
0
ファイル: layout.py プロジェクト: farmborst/pytswa
def cs_Dblentry(root, r, c, value, entry_conf={}, grid_conf={}):
    entrydbl = DoubleVar()
    entrydbl.set(float(value))
    entry = Entry(root, textvariable=entrydbl, **entry_conf)
    entry.grid(row=r, column=c, **grid_conf)
    return entry
コード例 #28
0
class CoefficientEditor:
    """Gets the chemical reaction information for each layer."""
    def __init__(self, master, system, coefficient, coefficients, editflag):
        """Constructor method.  Makes the GUI using the list of "Layer" objects
        "layers." """

        # Read the Tkinter information
        self.master = master
        self.version = system.version
        self.fonttype = system.fonttype
        self.formulatype = system.formulatype
        self.superfont = get_superfont(self.fonttype)
        self.subfont = get_superfont(self.formulatype)
        self.tframe = Frame(master.tframe)
        self.frame = Frame(master.frame)
        self.bframe = Frame(master.bframe)
        self.bgcolor = self.frame.cget('bg')
        self.top = None

        self.concunit = system.concunit
        self.timeunit = system.timeunit

        self.concunits = system.concunits

        # Convert mass concentration units to corresponding molar concentration units
        if self.concunit == system.concunits[0]:
            self.concunit = system.concunits[3]
        if self.concunit == system.concunits[1]:
            self.concunit = system.concunits[4]
        if self.concunit == system.concunits[2]:
            self.concunit = system.concunits[5]

        # Read the system information
        self.coefficient = coefficient
        self.coefficients = coefficients
        self.layers = system.layers
        self.editflag = editflag

        if self.editflag == 0:

            self.layer_full_list = []
            self.reaction_full_list = []
            for layer in system.layers:
                self.layer_full_list.append(layer.name)
            for reaction in system.reactions:
                self.reaction_full_list.append(reaction.name)

            self.layer_list = []
            self.reaction_list = []

            for layer in system.layers:
                coef_check = 0
                for reaction in system.reactions:
                    if coefficients[layer.name][reaction.name].lam == 0:
                        coef_check = 1
                if coef_check == 1:
                    self.layer_list.append(layer.name)
                    self.reaction_list.append([])
                    for reaction in system.reactions:
                        if coefficients[layer.name][reaction.name].lam == 0:
                            self.reaction_list[-1].append(reaction.name)

            self.layer = StringVar(value=self.layer_list[0])
            self.reaction = StringVar(value=self.reaction_list[0][0])
            self.equation = StringVar(value='')

        else:
            self.layer = coefficient.layer.name
            self.reaction = coefficient.reaction.name
            self.equation = coefficient.reaction.equation
            self.reactants = coefficient.reaction.reactants

        self.lam = DoubleVar(value=coefficient.lam)

        self.cancelflag = 0

    def make_widgets(self):

        self.instructions = Label(
            self.frame,
            text=
            'Please input the following information for the added kinetic process:        '
        )

        self.blankcolumn = Label(self.frame, text=' ', width=2)
        self.layercolumn = Label(self.frame, text=' ', width=15)
        self.namecolumn = Label(self.frame, text=' ', width=15)
        self.equationcolumn = Label(self.frame, text=' ', width=20)
        self.ratecolumn = Label(self.frame, text=' ', width=10)
        self.coef1column = Label(self.frame, text=' ', width=10)
        self.unit1column = Label(self.frame, text=' ', width=3)
        self.endcolumn = Label(self.frame, text=' ', width=2)

        self.layerlabel = Label(self.frame, text='Layer')
        self.namelabel = Label(self.frame, text='Reaction')
        self.equationlabel = Label(self.frame, text='Chemical equation')
        self.ratelabel = Label(self.frame, text='Rate equation')
        self.lamcoeflabel = Label(self.frame, text='Coefficient  ' + u'\u03BB')

        self.blank1 = Label(self.frame, text=' ')

        self.instructions.grid(row=0,
                               column=0,
                               padx=8,
                               sticky='W',
                               columnspan=11)

        self.blankcolumn.grid(row=1, column=0, padx=2, sticky='WE')
        self.layercolumn.grid(row=1, column=1, padx=2, sticky='WE')
        self.namecolumn.grid(row=1, column=2, padx=2, sticky='WE')
        self.equationcolumn.grid(row=1, column=3, padx=2, sticky='WE')
        self.ratecolumn.grid(row=1, column=4, padx=2, sticky='WE')
        self.coef1column.grid(row=1, column=5, padx=2, sticky='WE')
        self.unit1column.grid(row=1, column=6, padx=2, sticky='WE')
        self.endcolumn.grid(row=1, column=7, padx=2, sticky='WE')

        self.layerlabel.grid(row=2, column=1, padx=2, sticky='WE')
        self.namelabel.grid(row=2, column=2, padx=2, sticky='WE')
        self.equationlabel.grid(row=2, column=3, padx=2, sticky='WE')
        self.ratelabel.grid(row=2, column=4, padx=2, sticky='WE')

        self.blank1.grid(row=3, column=0, padx=2, sticky='WE', columnspan=11)

        if self.editflag == 1:

            self.layerwidget = Label(self.frame, text=self.layer)
            self.namewidget = Label(self.frame, text=self.reaction)
            self.equationwidget = Label(self.frame,
                                        text=self.equation,
                                        font=self.formulatype)

            self.layerwidget.grid(row=4, column=1, padx=2)
            self.namewidget.grid(row=4, column=2, padx=2)
            self.equationwidget.grid(row=4, column=3, padx=10)

            if self.coefficient.reaction.model == 'User-defined' or self.coefficient.reaction.model == 'Fundamental':

                unitindex = 0

                tkfont = tkFont.Font(font=self.formulatype)
                tksubfont = tkFont.Font(font=self.subfont)
                tksuperfont = tkFont.Font(font=self.superfont)

                # Measure the width of rate and lambda expression
                rate_len = tkfont.measure(u'r = \u03BB')
                rate_len = rate_len + tksubfont.measure(
                    str(self.coefficient.reaction.number) + ',')
                if self.coefficient.layer.number == 0:
                    rate_len = rate_len + tksubfont.measure('D')
                else:
                    rate_len = rate_len + tksubfont.measure(
                        str(self.coefficient.layer.number))
                for reactant in self.coefficient.reaction.reactants:
                    if reactant.index <> 0:
                        rate_len = rate_len + tkfont.measure('C')
                        rate_len = rate_len + tksubfont.measure(
                            reactant.formula)
                        if reactant.index == int(reactant.index):
                            index = int(reactant.index)
                        else:
                            index = reactant.index

                        if index <> 1.:
                            rate_len = rate_len + tksuperfont.measure(
                                str(index) + ' ')
                        unitindex = unitindex + reactant.index
                print(unitindex)
                lam_len = tkfont.measure('')
                if unitindex == int(unitindex): unitindex = int(unitindex)
                print(unitindex)
                if (unitindex - 1) != 0:
                    if (unitindex - 1) != -1:
                        lam_len = lam_len + tkfont.measure('(' +
                                                           self.concunit + ')')
                        lam_len = lam_len + tksuperfont.measure(
                            str(-(unitindex - 1)))
                    else:
                        lam_len = lam_len + tkfont.measure(self.concunit + ' ')
                lam_len = lam_len + tkfont.measure(self.timeunit)
                lam_len = lam_len + tksuperfont.measure('-1')

                self.ratewidget = Text(self.frame,
                                       width=int(rate_len * 1.1424219345 / 8) +
                                       1,
                                       height=1,
                                       font=self.formulatype)
                self.ratewidget.insert('end', u'r')
                self.ratewidget.insert('end', ' = ')
                self.ratewidget.insert('end', u'\u03BB')
                self.ratewidget.insert(
                    'end',
                    str(self.coefficient.reaction.number) + ',', 'sub')
                if self.coefficient.layer.number == 0:
                    self.ratewidget.insert('end', 'D', 'sub')
                else:
                    self.ratewidget.insert('end',
                                           str(self.coefficient.layer.number),
                                           'sub')
                for reactant in self.coefficient.reaction.reactants:
                    if reactant.index <> 0:
                        self.ratewidget.insert('end', 'C')
                        self.ratewidget.insert('end', reactant.formula, 'sub')
                        if reactant.index == int(reactant.index):
                            index = int(reactant.index)
                        else:
                            index = reactant.index
                        if index <> 1.:
                            self.ratewidget.insert('end',
                                                   str(index) + ' ', 'super')

                self.ratewidget.tag_config('sub', offset=-4, font=self.subfont)
                self.ratewidget.tag_config('super',
                                           offset=5,
                                           font=self.superfont)
                self.ratewidget.tag_config('right', justify='right')
                self.ratewidget.config(state='disabled',
                                       background=self.bgcolor,
                                       borderwidth=0,
                                       spacing3=3)

                self.lamcoefwidget = Entry(self.frame,
                                           textvariable=self.lam,
                                           justify='center',
                                           width=8)

                self.lamunitlabel = Text(
                    self.frame,
                    width=int(lam_len * 1.1424219345 / 8) + 1,
                    height=1,
                    font=self.formulatype)
                if (unitindex - 1) != 0:
                    if (unitindex - 1) != -1:
                        self.lamunitlabel.insert('end',
                                                 '(' + self.concunit + ')')
                        self.lamunitlabel.insert('end', str(-(unitindex - 1)),
                                                 'super')
                    else:
                        self.lamunitlabel.insert('end', self.concunit + ' ')
                self.lamunitlabel.insert('end', self.timeunit)
                self.lamunitlabel.insert('end', '-1', 'super')
                self.lamunitlabel.tag_config('sub',
                                             offset=-4,
                                             font=self.subfont)
                self.lamunitlabel.tag_config('super',
                                             offset=5,
                                             font=self.superfont)
                self.lamunitlabel.tag_config('right', justify='right')
                self.lamunitlabel.config(state='disabled',
                                         background=self.bgcolor,
                                         borderwidth=0,
                                         spacing3=3)

                self.lamcoeflabel.grid(row=2,
                                       column=5,
                                       padx=2,
                                       sticky='WE',
                                       columnspan=4)

                self.ratewidget.grid(row=4, column=4, padx=5)
                self.lamcoefwidget.grid(row=4, column=5, padx=2, sticky='E')
                self.lamunitlabel.grid(row=4, column=6, padx=2, sticky='W')

        else:
            self.layerwidget = OptionMenu(self.frame,
                                          self.layer,
                                          *self.layer_list,
                                          command=self.click_layer)
            self.layerwidget.grid(row=4, column=1, padx=2, sticky='WE')
            self.click_layer()

        self.okbutton = Button(self.frame,
                               text='OK',
                               width=20,
                               command=self.OK)
        self.cancelbutton = Button(self.frame,
                                   text='Cancel',
                                   width=20,
                                   command=self.Cancel)
        self.blank2 = Label(self.frame, text=' ')
        self.blank3 = Label(self.frame, text=' ')
        self.blank4 = Label(self.frame, text=' ')
        self.focusbutton = None

        self.blank3.grid(row=5)
        self.okbutton.grid(row=6, columnspan=11)
        self.cancelbutton.grid(row=7, columnspan=11)
        self.blank4.grid(row=8)
        self.okbutton.bind('<Return>', self.OK)
        self.focusbutton = self.okbutton

    def click_layer(self, event=None):

        try:
            self.namewidget.grid_forget()
        except:
            pass

        self.reaction.set(self.reaction_list[self.layer_list.index(
            self.layer.get())][0])
        self.namewidget = OptionMenu(self.frame,
                                     self.reaction,
                                     *self.reaction_list[self.layer_list.index(
                                         self.layer.get())],
                                     command=self.click_reaction)
        self.namewidget.grid(row=4, column=2, padx=2, sticky='WE')

        self.click_reaction()

    def click_reaction(self, event=None):

        try:
            self.equationwidget.grid_forget()
            self.ratewidget.grid_forget()
        except:
            pass

        try:
            self.lamcoeflabel.grid_forget()
            self.lamcoefwidget.grid_forget()
            self.lamunitlabel.grid_forget()
        except:
            pass

        coefficient = self.coefficients[self.layer.get()][self.reaction.get()]
        self.lam.set(coefficient.lam)

        self.equation = coefficient.reaction.equation
        self.equationwidget = Label(self.frame,
                                    text=self.equation,
                                    font=self.formulatype)
        self.equationwidget.grid(row=4, column=3, padx=5)

        if coefficient.reaction.model == 'User-defined' or coefficient.reaction.model == 'Fundamental':

            unitindex = 0

            tkfont = tkFont.Font(font=self.formulatype)
            tksubfont = tkFont.Font(font=self.subfont)
            tksuperfont = tkFont.Font(font=self.superfont)

            # Measure the width of rate and lambda expression
            rate_len = tkfont.measure(u'r = \u03BB')
            rate_len = rate_len + tksubfont.measure(
                str(coefficient.reaction.number) + ',')
            if coefficient.layer.number == 0:
                rate_len = rate_len + tksubfont.measure('D')
            else:
                rate_len = rate_len + tksubfont.measure(
                    str(coefficient.layer.number))
            for reactant in coefficient.reaction.reactants:
                if reactant.index <> 0:
                    rate_len = rate_len + tkfont.measure('C')
                    rate_len = rate_len + tksubfont.measure(reactant.formula)
                    if reactant.index == int(reactant.index):
                        index = int(reactant.index)
                    else:
                        index = reactant.index

                    if index <> 1.:
                        rate_len = rate_len + tksuperfont.measure(
                            str(index) + ' ')
                    unitindex = unitindex + reactant.index

            lam_len = tkfont.measure('')
            if unitindex == int(unitindex): unitindex = int(unitindex)
            if (unitindex - 1) != 0:
                if (unitindex - 1) != 1:
                    lam_len = lam_len + tkfont.measure('(' + self.concunit +
                                                       ')')
                    lam_len = lam_len + tksuperfont.measure(
                        str(-(unitindex - 1)))
                else:
                    lam_len = lam_len + tkfont.measure(self.concunit)
            lam_len = lam_len + tkfont.measure(self.timeunit)
            lam_len = lam_len + tksuperfont.measure('-1')

            self.ratewidget = Text(self.frame,
                                   width=int(rate_len * 1.1424219345 / 8) + 1,
                                   height=1,
                                   font=self.formulatype)
            self.ratewidget.insert('end', u'r = \u03BB')
            self.ratewidget.insert('end',
                                   str(coefficient.reaction.number) + ',',
                                   'sub')
            if coefficient.layer.number == 0:
                self.ratewidget.insert('end', 'D', 'sub')
            else:
                self.ratewidget.insert('end', str(coefficient.layer.number),
                                       'sub')
            for reactant in coefficient.reaction.reactants:
                if reactant.index <> 0:
                    self.ratewidget.insert('end', 'C')
                    self.ratewidget.insert('end', reactant.formula, 'sub')
                    if reactant.index == int(reactant.index):
                        index = int(reactant.index)
                    else:
                        index = reactant.index
                    if index <> 1.:
                        self.ratewidget.insert('end',
                                               str(index) + ' ', 'super')

            self.ratewidget.tag_config('sub', offset=-4, font=self.subfont)
            self.ratewidget.tag_config('super', offset=5, font=self.superfont)
            self.ratewidget.tag_config('right', justify='right')
            self.ratewidget.config(state='disabled',
                                   background=self.bgcolor,
                                   borderwidth=0,
                                   spacing3=3)

            self.lamcoefwidget = Entry(self.frame,
                                       textvariable=self.lam,
                                       justify='center',
                                       width=8)

            self.lamunitlabel = Text(self.frame,
                                     width=int(lam_len * 1.1424219345 / 8) + 1,
                                     height=1,
                                     font=self.formulatype)
            if unitindex == int(unitindex): unitindex = int(unitindex)
            if (unitindex - 1) != 0:
                if (unitindex - 1) != 1:
                    self.lamunitlabel.insert('end', '(' + self.concunit + ')')
                    self.lamunitlabel.insert('end', str(-(unitindex - 1)),
                                             'super')
                else:
                    self.lamunitlabel.insert('end', self.concunit)
            self.lamunitlabel.insert('end', self.timeunit)
            self.lamunitlabel.insert('end', '-1', 'super')
            self.lamunitlabel.tag_config('sub', offset=-4, font=self.subfont)
            self.lamunitlabel.tag_config('super',
                                         offset=5,
                                         font=self.superfont)
            self.lamunitlabel.tag_config('right', justify='right')
            self.lamunitlabel.config(state='disabled',
                                     background=self.bgcolor,
                                     borderwidth=0,
                                     spacing3=3)

            self.lamcoeflabel.grid(row=2,
                                   column=5,
                                   padx=2,
                                   sticky='WE',
                                   columnspan=4)

            self.ratewidget.grid(row=4, column=4, padx=5)
            self.lamcoefwidget.grid(row=4, column=5, padx=2, sticky='E')
            self.lamunitlabel.grid(row=4, column=6, padx=2, sticky='W')

        self.frame.update()
        self.focusbutton = None
        self.master.geometry()
        self.master.center()

    def OK(self, event=None):
        """Finish and move on.  Checks that the number chemicals are less than the
        total number of chemicals in database."""

        if self.editflag == 0:
            self.layerindex = self.layer_full_list.index(self.layer.get())
            self.reactionnumber = self.reaction_full_list.index(
                self.reaction.get()) + 1

        if self.master.window.top is not None: self.master.open_toplevel()
        elif self.lam == 0: self.coefficient_error()
        else: self.master.tk.quit()

    def coefficient_error(self):

        tkmb.showerror(
            title=self.version,
            message='At least one chemical has been replicated in the reaction!'
        )
        self.focusbutton = self.okbutton
        self.master.tk.lift()

    def Cancel(self):

        try:
            self.reactants = self.reaction.reactants
            self.products = self.reaction.products

        except:
            self.cancelflag = 1

        if self.master.window.top is not None: self.master.open_toplevel()
        else: self.master.tk.quit()
コード例 #29
0
ファイル: layout.py プロジェクト: kramerfelix/accpy
def cs_dbl(value):
    dvar = DoubleVar()
    dvar.set(float(value))
    return dvar
コード例 #30
0
ファイル: cockpit.py プロジェクト: hisie/eaglebone
class Cockpit(ttkFrame):
    '''
    Remote controller GUI 
    '''
    
    KEY_ANG_SPEED = "ang-speed"
    KEY_ANGLES = "angles"
    KEY_ACCEL = "accel"
    
    PID_KEYS = ["P", "I", "D"]

    DEFAULT_DRONE_IP = "192.168.1.130"
    DEFAULT_DRONE_PORT = 2121

    DIR_NONE = 0
    DIR_VERTICAL = 1
    DIR_HORIZONTAL = 2
    
    MAX_ACCEL = 10.0 #TODO angles. Replace by m/s²
    MAX_ACCEL_Z = 0.1 #m/s²
    MAX_ANGLE_SPEED = 50.0 #º/s

    def __init__(self, parent, isDummy = False, droneIp = DEFAULT_DRONE_IP, dronePort = DEFAULT_DRONE_PORT):
        '''
        Constructor
        '''
        ttkFrame.__init__(self, parent)
        
        self._started = IntVar()
        self._integralsEnabled = IntVar()
        self._target = [0.0] * 4        
        
        self._selectedPidConstats = "--"
        self._pidConstants = {
                              Cockpit.KEY_ANG_SPEED:{
                                           "X":{
                                                "P": 0.0,
                                                "I": 0.0,
                                                "D": 0.0
                                                },
                                           "Y":{
                                                "P": 0.0,
                                                "I": 0.0,
                                                "D": 0.0
                                                },
                                           "Z":{
                                                "P": 0.0,
                                                "I": 0.0,
                                                "D": 0.0
                                                }
                                           },
                               Cockpit.KEY_ANGLES: {
                                          "X":{
                                                "P": 0.0,
                                                "I": 0.0,
                                                "D": 0.0
                                                },
                                           "Y":{
                                                "P": 0.0,
                                                "I": 0.0,
                                                "D": 0.0
                                                }
                                          },
                               Cockpit.KEY_ACCEL:{
                                           "X":{
                                                "P": 0.0,
                                                "I": 0.0,
                                                "D": 0.0
                                                },
                                           "Y":{
                                                "P": 0.0,
                                                "I": 0.0,
                                                "D": 0.0
                                                },
                                            "Z":{
                                                "P": 0.0,
                                                "I": 0.0,
                                                "D": 0.0
                                                 }
                                          }
                              }
        
        self.parent = parent

        self.initUI()

        self._controlKeysLocked = False

        if not isDummy:
            self._link = INetLink(droneIp, dronePort)
        else:
            self._link = ConsoleLink()
            
        self._link.open()

        self._updateInfoThread = Thread(target=self._updateInfo)
        self._updateInfoThreadRunning = False
        self._readingState = False

        self._start()
        
            
    def initUI(self):
        
        self.parent.title("Drone control")
        self.style = Style()
        self.style.theme_use("default")
        
        self.pack(fill=BOTH, expand=1)
        
        self.parent.bind_all("<Key>", self._keyDown)
        self.parent.bind_all("<KeyRelease>", self._keyUp)

        if system() == "Linux":
            self.parent.bind_all("<Button-4>", self._onMouseWheelUp)
            self.parent.bind_all("<Button-5>", self._onMouseWheelDown)

        else:
            #case of Windows
            self.parent.bind_all("<MouseWheel>", self._onMouseWheel)
        
        #Commands        
        commandsFrame = tkFrame(self)
        commandsFrame.grid(column=0, row=0, sticky="WE")
        
        self._startedCB = Checkbutton(commandsFrame, text="On", variable=self._started, command=self._startedCBChanged)
        self._startedCB.pack(side=LEFT, padx=4)
        
        self._integralsCB = Checkbutton(commandsFrame, text="Int.", variable=self._integralsEnabled, \
                                        command=self._integralsCBChanged, state=DISABLED)
        self._integralsCB.pack(side=LEFT, padx=4)
        
        self._quitButton = Button(commandsFrame, text="Quit", command=self.exit)
        self._quitButton.pack(side=LEFT, padx=2, pady=2)
        
#         self._angleLbl = Label(commandsFrame, text="Angle")
#         self._angleLbl.pack(side=LEFT, padx=4)
#         
#         self._angleEntry = Entry(commandsFrame, state=DISABLED)
#         self._angleEntry.pack(side=LEFT)
        
        #Info
        infoFrame = tkFrame(self)
        infoFrame.grid(column=1, row=1, sticky="E", padx=4)
        
        #Throttle
        Label(infoFrame, text="Throttle").grid(column=0, row=0, sticky="WE")        
        self._throttleTexts = [StringVar(),StringVar(),StringVar(),StringVar()]
        Entry(infoFrame, textvariable=self._throttleTexts[3], state=DISABLED, width=5).grid(column=0, row=1)                
        Entry(infoFrame, textvariable=self._throttleTexts[0], state=DISABLED, width=5).grid(column=1, row=1)
        Entry(infoFrame, textvariable=self._throttleTexts[2], state=DISABLED, width=5).grid(column=0, row=2)
        Entry(infoFrame, textvariable=self._throttleTexts[1], state=DISABLED, width=5).grid(column=1, row=2)
        
        #Angles
        Label(infoFrame, text="Angles").grid(column=0, row=3, sticky="WE")        
        self._angleTexts = [StringVar(),StringVar(),StringVar()]
        for index in range(3):
            Entry(infoFrame, textvariable=self._angleTexts[index], state=DISABLED, width=5).grid(column=index, row=4)
               
        #Accels
        Label(infoFrame, text="Accels").grid(column=0, row=5, sticky="WE")
        self._accelTexts = [StringVar(),StringVar(),StringVar()]
        for index in range(3):
            Entry(infoFrame, textvariable=self._accelTexts[index], state=DISABLED, width=5).grid(column=index, row=6)
        
        #Speeds
        Label(infoFrame, text="Speeds").grid(column=0, row=7, sticky="WE")
        self._speedTexts = [StringVar(),StringVar(),StringVar()]
        for index in range(3):
            Entry(infoFrame, textvariable=self._speedTexts[index], state=DISABLED, width=5).grid(column=index, row=8)
        
        #Height
        Label(infoFrame, text="Height").grid(column=0, row=9, sticky="W")
        self._heightText = StringVar()
        Entry(infoFrame, state=DISABLED, width=5).grid(column=1, row=9)
        
        #control
        
        controlFrame = tkFrame(self)
        controlFrame.grid(column=0, row=1, sticky="W")
        
        self._throttle = DoubleVar()
        self._thrustScale = Scale(controlFrame, orient=VERTICAL, from_=100.0, to=-100.0, \
                            tickinterval=0, variable=self._throttle, \
                            length=200, showvalue=1, \
                            state=DISABLED,
                            command=self._onThrustScaleChanged)
        self._thrustScale.bind("<Double-Button-1>", self._onThrustScaleDoubleButton1, "+")
        self._thrustScale.grid(column=0)
        
        self._shiftCanvas = Canvas(controlFrame, bg="white", height=400, width=400, \
                             relief=SUNKEN)
        self._shiftCanvas.bind("<Button-1>", self._onMouseButton1)
        #self._shiftCanvas.bind("<ButtonRelease-1>", self._onMouseButtonRelease1)
        self._shiftCanvas.bind("<B1-Motion>", self._onMouseButton1Motion)
        self._shiftCanvas.bind("<Double-Button-1>", self._onMouseDoubleButton1)

        self._shiftCanvas.bind("<Button-3>", self._onMouseButton3)
        #self._shiftCanvas.bind("<ButtonRelease-3>", self._onMouseButtonRelease3)
        self._shiftCanvas.bind("<B3-Motion>", self._onMouseButton3Motion)

        self._shiftCanvas.grid(row=0,column=1, padx=2, pady=2)
        self._shiftCanvas.create_oval(2, 2, 400, 400, outline="#ff0000")
        self._shiftCanvas.create_line(201, 2, 201, 400, fill="#ff0000")
        self._shiftCanvas.create_line(2, 201, 400, 201, fill="#ff0000")
        self._shiftMarker = self._shiftCanvas.create_oval(197, 197, 205, 205, outline="#0000ff", fill="#0000ff")
        
        self._yaw = DoubleVar()
        self._yawScale = Scale(controlFrame, orient=HORIZONTAL, from_=-100.0, to=100.0, \
                            tickinterval=0, variable=self._yaw, \
                            length=200, showvalue=1, \
                            command=self._onYawScaleChanged)
        self._yawScale.bind("<Double-Button-1>", self._onYawScaleDoubleButton1, "+")
        self._yawScale.grid(row=1, column=1)
        
        self._controlKeyActive = False
        

        #PID calibration

        pidCalibrationFrame = tkFrame(self)
        pidCalibrationFrame.grid(column=0, row=2, sticky="WE");

        self._pidSelected = StringVar()
        self._pidSelected.set("--")
        self._pidListBox = OptionMenu(pidCalibrationFrame, self._pidSelected, "--", \
                                      Cockpit.KEY_ANG_SPEED, Cockpit.KEY_ANGLES, Cockpit.KEY_ACCEL, \
                                       command=self._onPidListBoxChanged)
        self._pidListBox.pack(side=LEFT, padx=2)
        self._pidListBox.config(width=10)
        
        self._axisSelected = StringVar()
        self._axisSelected.set("--")
        self._axisListBox = OptionMenu(pidCalibrationFrame, self._axisSelected, "--", "X", "Y", "Z", \
                                       command=self._onAxisListBoxChanged)
        self._axisListBox.pack(side=LEFT, padx=2)
        self._axisListBox.config(state=DISABLED)

        Label(pidCalibrationFrame, text="P").pack(side=LEFT, padx=(14, 2))

        self._pidPString = StringVar()
        self._pidPString.set("0.00")
        self._pidPSpinbox = Spinbox(pidCalibrationFrame, width=5, from_=0.0, to=100.0, increment=0.01, state=DISABLED, \
                                         textvariable=self._pidPString, command=self._onPidSpinboxChanged)
        self._pidPSpinbox.pack(side=LEFT, padx=2)

        Label(pidCalibrationFrame, text="I").pack(side=LEFT, padx=(14, 2))

        self._pidIString = StringVar()
        self._pidIString.set("0.00")
        self._pidISpinbox = Spinbox(pidCalibrationFrame, width=5, from_=0.0, to=100.0, increment=0.01, state=DISABLED, \
                                         textvariable=self._pidIString, command=self._onPidSpinboxChanged)
        self._pidISpinbox.pack(side=LEFT, padx=2)
        
        Label(pidCalibrationFrame, text="D").pack(side=LEFT, padx=(14, 2))
        
        self._pidDString = StringVar()
        self._pidDString.set("0.00")
        self._pidDSpinbox = Spinbox(pidCalibrationFrame, width=5, from_=0.0, to=100.0, increment=0.01, state=DISABLED, \
                                         textvariable=self._pidDString, command=self._onPidSpinboxChanged)
        self._pidDSpinbox.pack(side=LEFT, padx=2)
        
        #debug
        debugFrame = tkFrame(self)
        debugFrame.grid(column=0, row=3, sticky="WE")
        
        self._debugMsg = Message(debugFrame, anchor="nw", justify=LEFT, relief=SUNKEN, width=300)
        self._debugMsg.pack(fill=BOTH, expand=1)



    def _start(self):

        self._readDroneConfig()
        
    
    def exit(self):
        
        self._link.send({"key": "close", "data": None})
        
        self._stopUpdateInfoThread()
        
        self._link.close()

        self.quit()


    def _updateTarget(self):
        
        markerCoords = self._shiftCanvas.coords(self._shiftMarker)
        coords = ((markerCoords[0] + markerCoords[2]) / 2, (markerCoords[1] + markerCoords[3]) / 2)
        
        self._target[1] = float(coords[0] - 201) * Cockpit.MAX_ACCEL / 200.0
        self._target[0] = float(coords[1] - 201) * Cockpit.MAX_ACCEL / 200.0      
        #Remote control uses clockwise angle, but the drone's referece system uses counter-clockwise angle
        self._target[2] = -self._yaw.get() * Cockpit.MAX_ANGLE_SPEED / 100.0
        
        self._target[3] = self._throttle.get() * Cockpit.MAX_ACCEL_Z / 100.0
        
        self._sendTarget() 
    
        
    def _keyDown(self, event):

        if event.keysym == "Escape":            
            self._throttle.set(0)
            self._started.set(0)
            self._thrustScale.config(state=DISABLED)
            self._stopUpdateInfoThread()
            self._sendIsStarted()
            
        elif event.keysym.startswith("Control"):            
            self._controlKeyActive = True
            
        elif not self._controlKeysLocked and self._controlKeyActive:
            
            if event.keysym == "Up":
                self._thrustScaleUp()
                
            elif event.keysym == "Down":
                self._thrustScaleDown()
                
            elif event.keysym == "Left":
                self._yawLeft()
                
            elif event.keysym == "Right":
                self._yawRight()
                
            elif event.keysym == "space":
                self._yawReset()
                self._thrustReset()
                
        elif not self._controlKeysLocked and not self._controlKeyActive:
            
            if event.keysym == "Up":
                self._moveShiftCanvasMarker((0,-5))
                
            elif event.keysym == "Down":
                self._moveShiftCanvasMarker((0,5))
                
            elif event.keysym == "Left":
                self._moveShiftCanvasMarker((-5,0))
                
            elif event.keysym == "Right":
                self._moveShiftCanvasMarker((5,0))
                
            elif event.keysym == "space":
                self._resetShiftCanvasMarker()                
    
    
    def _keyUp(self, eventArgs):
        
        if eventArgs.keysym.startswith("Control"):
            self._controlKeyActive = False
            
    
    def _onMouseButton1(self, eventArgs):

        self._lastMouseCoords = (eventArgs.x, eventArgs.y)

        
    def _onMouseButtonRelease1(self, eventArgs):

        self._shiftCanvas.coords(self._shiftMarker, 197, 197, 205, 205)

    
    def _limitCoordsToSize(self, coords, size):
        
        if coords[0] > size:
            x = size
        
        elif coords[0] < 0:
            x = 0
            
        else:
            x = coords[0]
            
            
        if coords[1] > size:
            y = size
            
        elif coords[1] < 0:
            y = 0
            
        else:
            y = coords[1]
            
        
        return (x,y)
    
    
    def _moveShiftCanvasMarker(self, shift):

        lastCoords = self._shiftCanvas.coords(self._shiftMarker)
        newCoords = (lastCoords[0] + shift[0], lastCoords[1] + shift[1])        
        newCoords = self._limitCoordsToSize(newCoords, 400)
    
        self._shiftCanvas.coords(self._shiftMarker, newCoords[0], newCoords[1], newCoords[0] + 8, newCoords[1] + 8)
        self._updateTarget()
    
    
    def _resetShiftCanvasMarker(self):
    
        self._shiftCanvas.coords(self._shiftMarker, 197, 197, 205, 205)
        self._updateTarget()
        
    
    def _onMouseButton1Motion(self, eventArgs):

        deltaCoords = (eventArgs.x - self._lastMouseCoords[0], eventArgs.y - self._lastMouseCoords[1])
        self._moveShiftCanvasMarker(deltaCoords)
        self._lastMouseCoords = (eventArgs.x, eventArgs.y)
  
      
    def _onMouseDoubleButton1(self, eventArgs):
        
        self._resetShiftCanvasMarker()        
            

    def _onMouseButton3(self, eventArgs):

        self._lastMouseCoords = (eventArgs.x, eventArgs.y)
        self._mouseDirection = Cockpit.DIR_NONE

        
    def _onMouseButtonRelease3(self, eventArgs):

        self._shiftCanvas.coords(self._shiftMarker, 197, 197, 205, 205)

        
    def _onMouseButton3Motion(self, eventArgs):

        deltaCoords = (eventArgs.x - self._lastMouseCoords[0], eventArgs.y - self._lastMouseCoords[1])

        if self._mouseDirection == Cockpit.DIR_NONE:
            if abs(deltaCoords[0]) > abs(deltaCoords[1]):
                self._mouseDirection = Cockpit.DIR_HORIZONTAL
            else:
                self._mouseDirection = Cockpit.DIR_VERTICAL

        if self._mouseDirection == Cockpit.DIR_HORIZONTAL:
            deltaCoords = (deltaCoords[0], 0)
        else:
            deltaCoords = (0, deltaCoords[1])

        self._moveShiftCanvasMarker(deltaCoords)
        self._lastMouseCoords = (eventArgs.x, eventArgs.y)
        
    
    def _thrustScaleUp(self):

        if self._started.get(): 
            newValue = self._thrustScale.get() + 1
            self._thrustScale.set(newValue)
            
            self._updateTarget()
    
    
    def _thrustScaleDown(self):
        
        if self._started.get():
            newValue = self._thrustScale.get() - 1
            self._thrustScale.set(newValue)
            
            self._updateTarget()
            
    
    def _thrustReset(self):
        
        if self._started.get():
            self._thrustScale.set(0.0)
            
            self._updateTarget()
            
            
    def _onThrustScaleDoubleButton1(self, eventArgs):
        
        self._thrustReset()
        
        return "break"
        
    
    def _yawRight(self):
        
        newValue = self._yaw.get() + 1
        self._yaw.set(newValue)
        self._updateTarget()
            

    def _yawLeft(self):
        
        newValue = self._yaw.get() - 1
        self._yaw.set(newValue)
        self._updateTarget()
        
        
    def _yawReset(self):
        
        self._yaw.set(0)
        self._updateTarget()
        
        
    def _onMouseWheelUp(self, eventArgs):
        
        if not self._controlKeyActive:
            self._thrustScaleUp()
            
        else:
            self._yawRight()
            

    def _onMouseWheelDown(self, eventArgs):

        if not self._controlKeyActive:
            self._thrustScaleDown()
            
        else:
            self._yawLeft()
    

    def _onMouseWheel(self, eventArgs):

        factor = int(eventArgs.delta/120)

        if not self._controlKeyActive:
        
            if self._started.get():
                newValue = self._thrustScale.get() + factor
                self._thrustScale.set(newValue)
                
                self._updateTarget()
        else:
            newValue = self._yaw.get() + factor
            self._yaw.set(newValue)
            self._updateTarget()

    
    def _onYawScaleChanged(self, eventArgs):
        
        self._updateTarget()
    
    
    def _onYawScaleDoubleButton1(self, eventArgs):
        
        self._yawReset()
        
        return "break"
        
    
    def _startedCBChanged(self):
        
        if not self._started.get():
            self._throttle.set(0)
            self._thrustScale.config(state=DISABLED)            
            self._integralsCB.config(state=DISABLED)
            self._stopUpdateInfoThread()
        else:
            self._thrustScale.config(state="normal")            
            self._integralsCB.config(state="normal")
            self._startUpdateInfoThread()
            
        self._sendIsStarted()
     
    
    def _integralsCBChanged(self):
    
        self._link.send({"key": "integrals", "data":self._integralsEnabled.get() != 0})
            
     
    def _onThrustScaleChanged(self, eventArgs):
        
        self._updateTarget()
    

    def _sendTarget(self):
        
        self._link.send({"key": "target", "data": self._target})
        
        
    def _sendIsStarted(self):
        
        isStarted = self._started.get() != 0        
        self._link.send({"key": "is-started", "data": isStarted})
        

    def _sendPidCalibrationData(self):

        if self._pidSelected.get() != "--" and self._axisSelected.get() != "--":

            pidData = {
                "pid": self._pidSelected.get(),
                "axis": self._axisSelected.get(), 
                "p": float(self._pidPSpinbox.get()),
                "i": float(self._pidISpinbox.get()),
                "d": float(self._pidDSpinbox.get())}
        
            self._link.send({"key": "pid-calibration", "data": pidData})


    def _updatePidCalibrationData(self):

        pid = self._pidSelected.get()
        axis = self._axisSelected.get()

        if pid != "--" and axis != "--":
             
            self._pidConstants[pid][axis]["P"] = float(self._pidPSpinbox.get())
            self._pidConstants[pid][axis]["I"] = float(self._pidISpinbox.get())
            self._pidConstants[pid][axis]["D"] = float(self._pidDSpinbox.get())
            

    def _readDroneConfig(self):

        self._link.send({"key": "read-drone-config", "data": None}, self._onDroneConfigRead)


    def _readDroneState(self):
        
        if not self._readingState:
            self._readingState = True
            self._link.send({"key": "read-drone-state", "data": None}, self._onDroneStateRead)


    def _readPidConfigItem(self, message, cockpitKey, axises, configKeys):
        
        for i in range(len(axises)):
            for j in range(len(Cockpit.PID_KEYS)):
                self._pidConstants[cockpitKey][axises[i]][Cockpit.PID_KEYS[j]] = message[configKeys[j]][i]
                

    def _onDroneConfigRead(self, message):

        #TODO Show current configuration within the GUI (at least relevant settings)
        if message:
            
            #Angle-speeds
            self._readPidConfigItem(message, Cockpit.KEY_ANG_SPEED, ["X", "Y", "Z"], \
                                    [Configuration.PID_ANGLES_SPEED_KP, \
                                     Configuration.PID_ANGLES_SPEED_KI, \
                                     Configuration.PID_ANGLES_SPEED_KD])
            
            #Angles
            self._readPidConfigItem(message, Cockpit.KEY_ANGLES, ["X", "Y"], \
                                    [Configuration.PID_ANGLES_KP, \
                                     Configuration.PID_ANGLES_KI, \
                                     Configuration.PID_ANGLES_KD])
                        
            #Accels
            self._readPidConfigItem(message, Cockpit.KEY_ACCEL, ["X", "Y", "Z"], \
                                    [Configuration.PID_ACCEL_KP, \
                                     Configuration.PID_ACCEL_KI, \
                                     Configuration.PID_ACCEL_KD])
        

    def _onDroneStateRead(self, state):
        
        if state:
            for index in range(4):
                self._throttleTexts[index].set("{0:.3f}".format(state["_throttles"][index]))
                
            for index in range(3):
                self._accelTexts[index].set("{0:.3f}".format(state["_accels"][index]))
                self._angleTexts[index].set("{0:.3f}".format(state["_angles"][index]))
                
        else:
            self._stopUpdateInfoThread()
            
        self._readingState = False
   

    def _onPidSpinboxChanged(self):

        self._updatePidCalibrationData()
        self._sendPidCalibrationData()

    
    def _onPidListBoxChanged(self, pid):
        
        self._axisSelected.set("--")
        
        self._pidPString.set("--")
        self._pidIString.set("--")
        self._pidDString.set("--")

        self._pidPSpinbox.config(state=DISABLED)
        self._pidISpinbox.config(state=DISABLED)
        self._pidDSpinbox.config(state=DISABLED)

        self._selectedPidConstats = pid

        if pid == "--":
            self._axisListBox.config(state=DISABLED)
            self._controlKeysLocked = False
                       
        else:
            self._axisListBox.config(state="normal")
            self._controlKeysLocked = True


    def _onAxisListBoxChanged(self, axis):
        
        if axis == "--" or (self._selectedPidConstats == Cockpit.KEY_ANGLES and axis == "Z"):
            
            self._pidPString.set("--")
            self._pidIString.set("--")
            self._pidDString.set("--")
            
            self._pidPSpinbox.config(state=DISABLED)
            self._pidISpinbox.config(state=DISABLED)
            self._pidDSpinbox.config(state=DISABLED)
            
            self._controlKeysLocked = axis != "--"
            
        else:
            
            self._pidPString.set("{:.2f}".format(self._pidConstants[self._selectedPidConstats][axis]["P"]))
            self._pidIString.set("{:.2f}".format(self._pidConstants[self._selectedPidConstats][axis]["I"]))
            self._pidDString.set("{:.2f}".format(self._pidConstants[self._selectedPidConstats][axis]["D"]))
            
            self._pidPSpinbox.config(state="normal")
            self._pidISpinbox.config(state="normal")
            self._pidDSpinbox.config(state="normal")
            
            self._controlKeysLocked = True

            
    def _updateInfo(self):
        
        while self._updateInfoThreadRunning:
            
            self._readDroneState()
            
            time.sleep(1.0)
            

    def _startUpdateInfoThread(self):
        
        self._updateInfoThreadRunning = True
        if not self._updateInfoThread.isAlive():                
            self._updateInfoThread.start()
        
            
    def _stopUpdateInfoThread(self):
        
        self._updateInfoThreadRunning = False
        if self._updateInfoThread.isAlive():
            self._updateInfoThread.join()
コード例 #31
0
ファイル: devices.py プロジェクト: if1live/marika
class FakeDeviceApp(Frame):
    MIN_X = 0
    MAX_X = 50
    MIN_Y = 0
    MAX_Y = 100
    MIN_Z = 0
    MAX_Z = 200

    def __init__(self, parent):
        Frame.__init__(self, parent, background='white')
        self.parent = parent
        self.init_ui()

    def init_ui(self):
        self.parent.title('Fake Device')
        self.style = Style()
        self.style.theme_use('default')

        self.pack(fill=BOTH, expand=1)

        x_scale = Scale(self, from_=self.MIN_X, to=self.MAX_X, command=self.on_scale_x)
        x_scale.place(x=0, y=0)

        y_scale = Scale(self, from_=self.MIN_Y, to=self.MAX_Y, command=self.on_scale_y)
        y_scale.place(x=0, y=20)

        z_scale = Scale(self, from_=self.MIN_Z, to=self.MAX_Z, command=self.on_scale_z)
        z_scale.place(x=0, y=40)

        angle_scale = Scale(self, from_=0, to=math.pi/2, command=self.on_scale_angle)
        angle_scale.place(x=0, y=80)

        self.x_var = IntVar()
        self.x_label = Label(self, text=0, textvariable=self.x_var)
        self.x_label.place(x=100, y=0)

        self.y_var = IntVar()
        self.y_label = Label(self, text=0, textvariable=self.y_var)
        self.y_label.place(x=100, y=20)

        self.z_var = IntVar()
        self.z_label = Label(self, text=0, textvariable=self.z_var)
        self.z_label.place(x=100, y=40)

        self.angle_var = DoubleVar()
        self.angle_label = Label(self, text=0, textvariable=self.angle_var)
        self.angle_label.place(x=100, y=80)

        self.button = Button(self, text='test', command=self.on_button)
        self.button.place(x=0, y=100)

    def on_button(self):
        print('hello')

    def on_scale_angle(self, val):
        v = float(val)
        self.angle_var.set(v)
        self.update()


    def on_scale_x(self, val):
        v = int(float(val))
        self.x_var.set(v)
        self.update()

    def on_scale_y(self, val):
        v = int(float(val))
        self.y_var.set(v)
        self.update()

    def on_scale_z(self, val):
        v = int(float(val))
        self.z_var.set(v)
        self.update()

    def update(self):
        x = self.x_var.get()
        y = self.y_var.get()
        z = self.z_var.get()
        angle = self.angle_var.get()

        sensor = events.sensor_storage
        sensor.reset()
        if not (x == 0 and y == 0 and z == 0):
            index_pos = [x, y, z]
            sensor.index_finger_pos = index_pos
            sensor.cmd_angle = angle
コード例 #32
0
class ConfigClass:
    def __init__(self,textbox,FOLDER,APPNAME):
        self.folder=os.path.join(FOLDER,'config')
        self.appname=APPNAME
        # Das Standard App Verzeichniss fuer das Betriebssystem abfragen
        self.make_settings_folder()

        # eine ConfigParser Instanz oeffnen und evt. vorhandenes Config File Laden        
        self.parser = ConfigParser.ConfigParser()
        self.cfg_file_name=self.appname+'_config.cfg'
        self.parser.read(os.path.join(self.folder,self.cfg_file_name))

        # Falls kein Config File vorhanden ist oder File leer ist neue File anlegen und neu laden
        if len(self.parser.sections())==0:
            self.make_new_Config_file()
            self.parser.read(os.path.join(self.folder,self.cfg_file_name))
            textbox.prt((_('\nNo config file found generated new on at: %s') \
                             %os.path.join(self.folder,self.cfg_file_name)))
        else:
            textbox.prt((_('\nLoading config file:%s') \
                             %os.path.join(self.folder,self.cfg_file_name)))

        #Tkinter Variablen erstellen zur späteren Verwendung in den Eingabefeldern        
        self.get_all_vars()

        #DEBUG INFORMATIONEN
        #Übergeben des geladenen Debug Level
        textbox.set_debuglevel(DEBUG=self.debug)
        textbox.prt(_('\nDebug Level: %i') %(self.debug),1)
        textbox.prt(str(self),1)

    def make_settings_folder(self): 
        # create settings folder if necessary 
        try: 
            os.mkdir(self.folder) 
        except OSError: 
            pass 

    def make_new_Config_file(self):
        
        #Generelle Einstellungen für Export
        self.parser.add_section('General')
        self.parser.set('General', 'write_to_stdout', 0)
        
        self.parser.add_section('Paths') 
        self.parser.set('Paths', 'load_path', 'C:\Users\Christian Kohloeffel\Documents\DXF2GCODE\trunk\dxf')
        self.parser.set('Paths', 'save_path', 'C:\Users\Christian Kohloeffel\Documents')

        self.parser.add_section('Import Parameters') 
        self.parser.set('Import Parameters', 'point_tolerance', 0.01)
        self.parser.set('Import Parameters', 'fitting_tolerance', 0.01)   
        self.parser.set('Import Parameters', 'spline_check',1)  
                   
        self.parser.add_section('Tool Parameters') 
        self.parser.set('Tool Parameters', 'diameter', 0.1)
        self.parser.set('Tool Parameters', 'start_radius', 0.05)

        self.parser.add_section('Plane Coordinates') 
        self.parser.set('Plane Coordinates', 'axis1_start_end', 0)
        self.parser.set('Plane Coordinates', 'axis2_start_end', 0)

        self.parser.add_section('Depth Coordinates') 
        self.parser.set('Depth Coordinates', 'axis3_retract', 1)
        self.parser.set('Depth Coordinates', 'axis3_safe_margin', 1.0)
        self.parser.set('Depth Coordinates', 'axis3_mill_depth', -1.0)
        self.parser.set('Depth Coordinates', 'axis3_slice_depth', -1.0)

        self.parser.add_section('Feed Rates')
        self.parser.set('Feed Rates', 'f_g1_depth', 300)
        self.parser.set('Feed Rates', 'f_g1_plane', 5000)

        self.parser.add_section('Axis letters')
        self.parser.set('Axis letters', 'ax1_letter', 'X')
        self.parser.set('Axis letters', 'ax2_letter', 'Y')
        self.parser.set('Axis letters', 'ax3_letter', 'Z')                  

        self.parser.add_section('Route Optimisation')
        self.parser.set('Route Optimisation', 'Begin art','heurestic')
        self.parser.set('Route Optimisation', 'Max. population', 20)
        self.parser.set('Route Optimisation', 'Max. iterations', 300)  
        self.parser.set('Route Optimisation', 'Mutation Rate', 0.95)

        self.parser.add_section('Filters')
        self.parser.set('Filters', 'pstoedit_cmd','C:\Program Files (x86)\pstoedit\pstoedit')
        self.parser.set('Filters', 'pstoedit_opt', ['-f','dxf','-mm'])
                     
        self.parser.add_section('Debug')
        self.parser.set('Debug', 'global_debug_level', 0)         
                
        open_file = open(os.path.join(self.folder,self.cfg_file_name), "w") 
        self.parser.write(open_file) 
        open_file.close()
            
    def get_all_vars(self):
        #try:   
        self.write_to_stdout=int(self.parser.get('General', 'write_to_stdout'))
        

        self.tool_dia=DoubleVar()
        self.tool_dia.set(float(self.parser.get('Tool Parameters','diameter')))

        self.start_rad=DoubleVar()
        self.start_rad.set(float(self.parser.get('Tool Parameters','start_radius')))        
       
        self.axis1_st_en=DoubleVar()
        self.axis1_st_en.set(float(self.parser.get('Plane Coordinates','axis1_start_end')))

        self.axis2_st_en=DoubleVar()
        self.axis2_st_en.set(float(self.parser.get('Plane Coordinates','axis2_start_end')))        
        
        self.axis3_retract=DoubleVar()
        self.axis3_retract.set(float(self.parser.get('Depth Coordinates','axis3_retract')))
        
        self.axis3_safe_margin=DoubleVar()
        self.axis3_safe_margin.set(float(self.parser.get('Depth Coordinates','axis3_safe_margin')))

        self.axis3_slice_depth=DoubleVar()
        self.axis3_slice_depth.set(float(self.parser.get('Depth Coordinates','axis3_slice_depth')))        

        self.axis3_mill_depth=DoubleVar()
        self.axis3_mill_depth.set(float(self.parser.get('Depth Coordinates','axis3_mill_depth')))        
        
        self.F_G1_Depth=DoubleVar()
        self.F_G1_Depth.set(float(self.parser.get('Feed Rates','f_g1_depth')))

        self.F_G1_Plane=DoubleVar()
        self.F_G1_Plane.set(float(self.parser.get('Feed Rates','f_g1_plane')))

        self.points_tolerance=DoubleVar()
        self.points_tolerance.set(float(self.parser.get('Import Parameters','point_tolerance')))

        self.fitting_tolerance=DoubleVar()
        self.fitting_tolerance.set(float(self.parser.get('Import Parameters','fitting_tolerance')))
        self.spline_check=int(self.parser.get('Import Parameters', 'spline_check')  )

        #Zuweisen der Werte fuer die TSP Optimierung
        self.begin_art=self.parser.get('Route Optimisation', 'Begin art')
        self.max_population=int((int(self.parser.get('Route Optimisation', 'Max. population'))/4)*4)
        self.max_iterations=int(self.parser.get('Route Optimisation', 'Max. iterations'))  
        self.mutate_rate=float(self.parser.get('Route Optimisation', 'Mutation Rate', 0.95))

        #Zuweisen der Axis Letters
        self.ax1_letter=self.parser.get('Axis letters', 'ax1_letter')
        self.ax2_letter=self.parser.get('Axis letters', 'ax2_letter')
        self.ax3_letter=self.parser.get('Axis letters', 'ax3_letter')

        #Holen der restlichen Variablen
        #Verzeichnisse
        self.load_path=self.parser.get('Paths','load_path')
        self.save_path=self.parser.get('Paths','save_path')

        #Holen der Commandos fuer pstoedit
        self.pstoedit_cmd=self.parser.get('Filters','pstoedit_cmd')
        self.pstoedit_opt=self.parser.get('Filters','pstoedit_opt')

        #Setzen des Globalen Debug Levels
        self.debug=int(self.parser.get('Debug', 'global_debug_level'))
        
        
#        except:
#            showerror(_("Error during reading config file"), _("Please delete or correct\n %s")\
#                      %(os.path.join(self.folder,self.cfg_file_name)))
#            raise Exception, _("Problem during import from INI File") 
#            
    def __str__(self):

        str=''
        for section in self.parser.sections(): 
            str= str +"\nSection: "+section 
            for option in self.parser.options(section): 
                str= str+ "\n   -> %s=%s" % (option, self.parser.get(section, option))
        return str
コード例 #33
0
ファイル: move_plugin.py プロジェクト: kindlychung/sk1
class MovePanel(PluginPanel):
	name='Move'
	title = _("Move")


	def init(self, master):
		PluginPanel.init(self, master)

		root=self.mw.root
		self.var_width_number=DoubleVar(root)
		self.var_height_number=DoubleVar(root)
		
		self.var_width_base=DoubleVar(root)
		self.var_height_base=DoubleVar(root)

		var_width_unit = StringVar(root)
		var_height_unit = StringVar(root)
		
		unit = config.preferences.default_unit
		self.var_width = LengthVar(10, unit, self.var_width_number, var_width_unit)
		self.var_height = LengthVar(10, unit,self.var_height_number,var_height_unit)
		
		jump=config.preferences.default_unit_jump
		self.var_width.set(0)
		self.var_height.set(0)
		
		self.var_width_base.set(0)
		self.var_height_base.set(0)
		
		self.var_position = StringVar(root)
		self.var_position.set(ABSOLUTE)
		
		self.var_basepoint = StringVar(root)
		self.var_basepoint.set('C')
		 
		#---------------------------------------------------------
		top = TFrame(self.panel, style='FlatFrame')
		top.pack(side = TOP, fill=BOTH)
		#---------------------------------------------------------
		# Horisontal size
		size_frameH = TFrame(top, style='FlatFrame', borderwidth=3)
		size_frameH.pack(side = TOP, fill = BOTH)
		
		label = TLabel(size_frameH, style='FlatLabel', image='move_h')
		label.pack(side = LEFT, padx=5)
		self.entry_width = TSpinbox(size_frameH,  var=0, vartype=1, textvariable = self.var_width_number, 
									min = -50000, max = 50000, step = jump, width = 10, command=self.apply_move)
		self.entry_width.pack(side = LEFT)

		self.labelwunit = TLabel(size_frameH, style='FlatLabel', text = self.var_width.unit)
		self.labelwunit.pack(side = LEFT, padx=5)
		#---------------------------------------------------------
		# Vertical 
		
		size_frameV = TFrame(top, style='FlatFrame', borderwidth=3)
		size_frameV.pack(side = TOP, fill = BOTH)
		label = TLabel(size_frameV, style='FlatLabel', image='move_v')
		label.pack(side = LEFT, padx=5)
		
		self.entry_height = TSpinbox(size_frameV, var=0, vartype=1, textvariable = self.var_height_number, 
									min = -50000, max = 50000, step = jump, width = 10, command=self.apply_move)
		self.entry_height.pack(side = LEFT)
		
		self.labelhunit = TLabel(size_frameV, style='FlatLabel', text = self.var_height.unit)
		self.labelhunit.pack(side = LEFT, padx=5)
		
		#---------------------------------------------------------
		# position chek
		
		self.position_check = TCheckbutton(top, text = _("Absolute Coordinates"), variable = self.var_position,
												onvalue=ABSOLUTE, offvalue=RELATIVE, command = self.position)
		self.position_check.pack(side = TOP, anchor=W, padx=5,pady=5)
		
		#---------------------------------------------------------
		# Basepoint check
		
		label = TLabel(top, style='FlatLabel', text = _("Basepoint:"))
		label.pack(side = TOP, fill = BOTH, padx=5)
		basepoint_frame=TLabelframe(top, labelwidget=label, style='Labelframe', borderwidth=4)
		basepoint_frame.pack(side = TOP, fill=X, padx=5, pady=2)
		
		self.Basepoint = BasePointSelector(basepoint_frame, anchor=self.var_basepoint, command = self.apply_basepoint)
		self.Basepoint.pack(side = LEFT, fill = BOTH, padx=5)
		
		label = TLabel(basepoint_frame, style='FlatLabel', image = 'coordinate_sys')
		label.pack(side = LEFT, fill = BOTH, padx=10)
			
		#---------------------------------------------------------
		# Button frame 
		
		button_frame = TFrame(top, style='FlatFrame', borderwidth=5)
		button_frame.pack(side = BOTTOM, fill = BOTH)
		
		self.update_buttons = []
		self.button = UpdatedButton(top, text = _("Apply"),
								command = self.apply_move)
		self.button.pack(in_ = button_frame, side = BOTTOM, expand = 1, fill = X, pady=3)
		
		self.button_copy = UpdatedButton(top, text = _("Apply to Copy"),
								command = self.apply_to_copy)
		self.button_copy.pack(in_ = button_frame, side = BOTTOM, expand = 1, fill = X)
		
		self.init_from_doc()
		self.subscribe_receivers()


###############################################################################

	def subscribe_receivers(self):
		self.document.Subscribe(SELECTION, self.Update)
		self.document.Subscribe(EDITED, self.update_var)
		config.preferences.Subscribe(CHANGED, self.update_pref)

	def unsubscribe_receivers(self):
		self.document.Unsubscribe(SELECTION, self.Update)
		self.document.Unsubscribe(EDITED, self.update_var)
		config.preferences.Unsubscribe(CHANGED, self.update_pref)

	def init_from_doc(self, *arg):
			self.Update()

	def Update(self, *arg):
		if self.is_selection():
			self.entry_width.set_state(NORMAL)
			self.entry_height.set_state(NORMAL)
			self.position_check['state']=NORMAL
			self.button['state']=NORMAL
			self.button_copy['state']=NORMAL
		else:
			self.entry_width.set_state(DISABLED)
			self.entry_height.set_state(DISABLED)
			self.position_check['state']=DISABLED
			self.button['state']=DISABLED
			self.button_copy['state']=DISABLED
			
		self.update_pref()

	def apply_basepoint(self):
		self.Update()

	def position(self):
		if self.var_position.get()==ABSOLUTE and self.var_basepoint.get()=='USER':
			self.var_basepoint.set('C')
		self.update_var()

	def apply_move(self, *arg):
		if self.button["state"]==DISABLED:
			return
		try:
				var_x=self.var_width.get()
				var_y=self.var_height.get()
		except:
				return
		
		x, y = self.coordinates(self.var_position.get())
		
		if self.var_position.get()==RELATIVE:
			if self.var_width_base != self.var_width.get() or self.var_height_base != self.var_height.get():
				self.var_basepoint.set('USER')
			x,y = var_x, var_y
		else:
			x,y = var_x-x, var_y-y
		
		if arg and arg[0] == 'Duplicate':
			self.document.MoveAndCopy(x, y, Point(0,0))
		else:
			self.document.MoveSelected(x, y)

	def apply_to_copy(self):
		self.apply_move('Duplicate')


	def coordinates(self, position):
		br=self.document.selection.coord_rect
		hor_sel=br.right - br.left
		ver_sel=br.top - br.bottom
		
		if position == RELATIVE:
			left, bottom = -hor_sel/2, -ver_sel/2
		else:
			left, bottom = br.left, br.bottom
		
		cnt_x,cnt_y=self.Basepoint.get_basepoint(hor_sel,ver_sel,left,bottom)
		
		if position == RELATIVE and cnt_x!=None:
			return cnt_x*2, cnt_y*2
		else:
			return cnt_x, cnt_y

	def update_pref(self, *arg):
		self.labelwunit['text']=config.preferences.default_unit
		self.labelhunit['text']=config.preferences.default_unit
		self.entry_width.step=config.preferences.default_unit_jump
		self.entry_height.step=config.preferences.default_unit_jump
		self.update_var()

	def update_var(self, *arg):
		if len(self.document.selection.GetInfo()):
			if self.var_basepoint.get() == 'USER':
				x=self.var_width.get()
				y=self.var_height.get()
				self.var_width.unit=config.preferences.default_unit
				self.var_height.unit=config.preferences.default_unit
				self.var_width.set(x)
				self.var_height.set(y)
				
			else:
				self.var_width.unit=config.preferences.default_unit
				self.var_height.unit=config.preferences.default_unit
				x, y = self.coordinates(self.var_position.get())
				self.var_width.set(x)
				self.var_height.set(y)
				self.var_width_base=self.var_width.get()
				self.var_height_base=self.var_height.get()

	def is_selection(self):
		return (len(self.document.selection) > 0)
コード例 #34
0
 def set(self, value):
     DoubleVar.set(self, round(value, self.precision))
コード例 #35
0
class FontPanel(StylePropertyPanel):

    title = _("Fonts")

    def __init__(self, master, main_window, doc):
        self.family_to_fonts = font.make_family_to_fonts()
        self.families = self.family_to_fonts.keys()
        self.families.sort()
        StylePropertyPanel.__init__(self,
                                    master,
                                    main_window,
                                    doc,
                                    name='fontdlg')
        self.family_list.SetList(self.families)
        index = self.families.index(self.font_family)
        self.family_list.select_set(index)
        self.family_list.yview(index)

    def build_dlg(self):
        top = self.top

        buttons = self.create_std_buttons(top)
        buttons.grid(row=3, column=4, columnspan=2, sticky="news")

        self.sample = FontPreview(top)
        self.sample.grid(column=0, row=3, columnspan=4, sticky="news")
        # XXX: the background color of the sample text should be
        # configurable

        label = Label(top, text=_("Font Family:"), anchor=W)
        label.grid(column=0, row=0, columnspan=2, sticky="ew")
        sb_vert = Scrollbar(top, takefocus=0)
        sb_vert.grid(column=1, row=1, rowspan=2, sticky="news")
        family_list = UpdatedListbox(top, name='families', height=8)
        family_list.grid(column=0, row=1, rowspan=2, sticky="news")
        family_list.Subscribe(SELECTION, self.family_selected)
        sb_vert['command'] = (family_list, 'yview')
        family_list['yscrollcommand'] = (sb_vert, 'set')
        self.family_list = family_list

        label = Label(top, text=_("Font Style:"), anchor=W)
        label.grid(column=2, row=0, sticky="ew")
        sb_vert = Scrollbar(top, takefocus=0)
        sb_vert.grid(column=3, row=1, rowspan=2, sticky="news")
        self.font_attr_list = UpdatedListbox(top,
                                             name='weights',
                                             height=4,
                                             width=15)
        self.font_attr_list.grid(column=2, row=1, rowspan=2, sticky="news")
        self.font_attr_list.Subscribe(SELECTION, self.attr_selected)
        sb_vert['command'] = (self.font_attr_list, 'yview')
        self.font_attr_list['yscrollcommand'] = (sb_vert, 'set')

        label = Label(top, text=_("Size:"), anchor=W)
        label.grid(column=4, row=0, columnspan=2, sticky="ew")

        frame = Frame(top)
        frame.grid(column=4, row=1, columnspan=2, sticky='ew')
        self.var_size = DoubleVar(top)
        scroll = MiniScroller(frame,
                              variable=self.var_size,
                              min=0.0,
                              max=None,
                              step=1)
        scroll.pack(side=RIGHT, fill=Y)
        self.size_entry = MyEntry(frame,
                                  textvariable=self.var_size,
                                  width=4,
                                  command=self.apply_size,
                                  justify=RIGHT)
        self.size_entry.pack(side=LEFT, expand=1, fill=BOTH)

        sb_vert = Scrollbar(top, takefocus=0)
        sb_vert.grid(column=5, row=2, sticky="news")
        self.size_list = UpdatedListbox(top, name='sizes', width=4, height=5)
        self.size_list.grid(column=4, row=2, sticky="news")
        self.size_list.Subscribe(SELECTION, self.size_selected)
        self.size_list.SetList(std_sizes)
        sb_vert['command'] = (self.size_list, 'yview')
        self.size_list['yscrollcommand'] = (sb_vert, 'set')

        top.columnconfigure(0, weight=1000)
        top.columnconfigure(4, weight=1)
        top.rowconfigure(2, weight=1)

    def init_from_doc(self):
        object = self.document.CurrentObject()
        if object is not None and object.is_Text:
            self.update_from_object_cb(object)
        else:
            default = font.GetFont(config.preferences.default_font)
            self.font_family = default.family
            self.font_attr = default.font_attrs
            self.update_from_family()
            self.update_size(properties.default_text_style.font_size)

    def Update(self):
        self.update_from_object_cb(self.document.CurrentObject())

    def update_from_object_cb(self, obj):
        if obj is not None and obj.is_Text:
            font = obj.Font()
            self.font_family = font.family
            self.font_attr = font.font_attrs
            self.update_size(obj.FontSize())
            self.update_from_family()

    def do_apply(self):
        name = self.current_font_ps()
        if not name:
            if __debug__:
                pdebug(None, 'FontPanel.do_apply: no ps name!')
            return
        kw = {
            'font': font.GetFont(name),
            'font_size': self.var_size.get(),
            # set if_type_present to one to make sure that font
            # properties are only set on objects that can have font
            # properties. This is not ideal, but it works and needed
            # only simple changes to base.py
            'if_type_present': 1
        }
        self.set_properties(_("Set Font `%s'") % name, 'font', kw)

    def update_from_family(self, set_view=1):
        index = self.families.index(self.font_family)
        self.family_list.Select(index, set_view)
        fonts = self.family_to_fonts[self.font_family]
        attrs = []
        for name in fonts:
            attrs.append(font.fontmap[name][1])

        attrs.sort()
        self.set_font_attrs(attrs)
        self.update_sample()

    def update_size(self, size):
        self.var_size.set(size)
        if size in std_sizes:
            self.size_list.Select(list(std_sizes).index(size), 1)
        else:
            self.size_list.SelectNone()

    def update_sample(self):
        self.sample.SetFont(self.current_font_ps())

    def set_font_attrs(self, attrs):
        self.font_attrs = attrs
        self.font_attr_list.SetList(attrs)
        self.font_attr = get_from_list(self.font_attr, attrs)
        self.font_attr_list.Select(attrs.index(self.font_attr), 1)

    def current_font_xlfd(self):
        fonts = self.family_to_fonts[self.font_family]
        for name in fonts:
            family, attrs, xlfd_start, encoding = font.fontmap[name]
            if attrs == self.font_attr:
                return font.xlfd_template % (xlfd_start, 24, encoding)
        return ''

    def current_font_ps(self):
        fonts = self.family_to_fonts[self.font_family]
        for name in fonts:
            family, attrs, xlfd_start, encoding = font.fontmap[name]
            if attrs == self.font_attr:
                return name
        return ''

    def family_selected(self):
        sel = self.family_list.curselection()
        if sel:
            index = sel[0]
            self.font_family = self.families[index]
            self.update_from_family(set_view=0)

    def attr_selected(self):
        sel = self.font_attr_list.curselection()
        if sel:
            index = sel[0]
            self.font_attr = self.font_attrs[index]
            self.update_sample()

    def size_selected(self):
        sel = self.size_list.curselection()
        if sel:
            self.var_size.set(self.size_list.get(sel[0]))

    def apply_size(self, *args):
        if self.can_apply():
            size = self.var_size.get()
            self.document.CallObjectMethod(text.CommonText,
                                           _("Set Font Size %.1f") % size,
                                           'SetFontSize', size)

    def save_prefs(self):
        StylePropertyPanel.save_prefs(self)
        config.preferences.sample_text = self.sample.Text()
コード例 #36
0
ファイル: fontdlg.py プロジェクト: shumik/skencil-c
class FontPanel(StylePropertyPanel):

    title = _("Fonts")

    def __init__(self, master, main_window, doc):
	self.family_to_fonts = font.make_family_to_fonts()
	self.families = self.family_to_fonts.keys()
	self.families.sort()
	StylePropertyPanel.__init__(self, master, main_window, doc,
                                    name = 'fontdlg')
	self.family_list.SetList(self.families)
	index = self.families.index(self.font_family)
	self.family_list.select_set(index)
	self.family_list.yview(index)

    def build_dlg(self):
	top = self.top

	buttons = self.create_std_buttons(top)
	buttons.grid(row = 3, column = 4, columnspan = 2, sticky = "news")

	self.sample_text = StringVar(top)
	self.sample_text.set(config.preferences.sample_text)
	self.sample = Entry(top, textvariable = self.sample_text,
			    relief = FLAT, bg = top['bg'],
			    width = len(config.preferences.sample_text))
	self.sample.grid(column = 0, row = 3, columnspan = 4, sticky = "news")
	# XXX: the background color of the sample text should be
	# configurable

	label = Label(top, text = _("Font Family:"), anchor = W)
	label.grid(column = 0, row = 0, columnspan = 2, sticky = "ew")
	sb_vert = Scrollbar(top, takefocus = 0)
	sb_vert.grid(column = 1, row = 1, rowspan = 2, sticky = "news")
	family_list = UpdatedListbox(top, name = 'families', height = 8)
	family_list.grid(column = 0, row = 1, rowspan = 2, sticky = "news")
	family_list.Subscribe(SELECTION, self.family_selected)
	sb_vert['command'] = (family_list, 'yview')
	family_list['yscrollcommand'] = (sb_vert, 'set')
	self.family_list = family_list

	label = Label(top, text = _("Font Style:"), anchor = W)
	label.grid(column = 2, row = 0, sticky = "ew")
	sb_vert = Scrollbar(top, takefocus = 0)
	sb_vert.grid(column = 3, row = 1, rowspan = 2, sticky = "news")
	self.font_attr_list = UpdatedListbox(top, name = 'weights', height = 4,
					     width = 15)
	self.font_attr_list.grid(column = 2, row = 1, rowspan = 2,
				 sticky = "news")
	self.font_attr_list.Subscribe(SELECTION, self.attr_selected)
	sb_vert['command'] = (self.font_attr_list, 'yview')
	self.font_attr_list['yscrollcommand'] = (sb_vert, 'set')

	label = Label(top, text = _("Size:"), anchor = W)
	label.grid(column = 4, row = 0, columnspan = 2, sticky = "ew")

	frame = Frame(top)
	frame.grid(column = 4, row = 1, columnspan = 2, sticky = 'ew')
	self.var_size = DoubleVar(top)
	scroll = MiniScroller(frame, variable = self.var_size,
			      min = 0.0, max = None, step = 1)
	scroll.pack(side = RIGHT, fill = Y)
	self.size_entry = MyEntry(frame, textvariable = self.var_size,
				  width = 4, command = self.apply_size,
				  justify = RIGHT)
	self.size_entry.pack(side = LEFT, expand = 1, fill = BOTH)

	sb_vert = Scrollbar(top, takefocus = 0)
	sb_vert.grid(column = 5, row = 2, sticky = "news")
	self.size_list = UpdatedListbox(top, name = 'sizes', width = 4,
					height = 5)
	self.size_list.grid(column = 4, row = 2, sticky = "news")
	self.size_list.Subscribe(SELECTION, self.size_selected)
	self.size_list.SetList(std_sizes)
	sb_vert['command'] = (self.size_list, 'yview')
	self.size_list['yscrollcommand'] = (sb_vert, 'set')

	top.columnconfigure(0, weight = 1000)
	top.columnconfigure(4, weight = 1)
	top.rowconfigure(2, weight = 1)

    def init_from_doc(self):
        object = self.document.CurrentObject()
        if object is not None and object.is_Text:
            self.update_from_object_cb(object)
        else:
            default = font.GetFont(config.preferences.default_font)
            self.font_family = default.family
            self.font_attr = default.font_attrs
            self.update_from_family()
            self.update_size(properties.default_text_style.font_size)
        
    def Update(self):
        self.update_from_object_cb(self.document.CurrentObject())

    def update_from_object_cb(self, obj):
	if obj is not None and obj.is_Text:
	    font = obj.Font()
            self.font_family = font.family
	    self.font_attr = font.font_attrs
            self.update_size(obj.FontSize())
	    self.update_from_family()

    def do_apply(self):
        name = self.current_font_ps()
        if not name:
            if __debug__:
                pdebug(None, 'FontPanel.do_apply: no ps name!')
            return
        kw = {'font': font.GetFont(name),
              'font_size': self.var_size.get(),
              # set if_type_present to one to make sure that font
              # properties are only set on objects that can have font
              # properties. This is not ideal, but it works and needed
              # only simple changes to base.py
              'if_type_present': 1}
        self.set_properties(_("Set Font `%s'") % name, 'font', kw)

    def update_from_family(self, set_view = 1):
	index = self.families.index(self.font_family)
	self.family_list.Select(index, set_view)
	fonts = self.family_to_fonts[self.font_family]
	attrs = []
	for name in fonts:
	    attrs.append(font.fontmap[name][1])

	attrs.sort()
	self.set_font_attrs(attrs)
	self.update_sample()

    def update_size(self, size):
        self.var_size.set(size)
        if size in std_sizes:
            self.size_list.Select(list(std_sizes).index(size), 1)
        else:
            self.size_list.SelectNone()

    def update_sample(self):
	xlfd = self.current_font_xlfd()
	if not xlfd:
	    xlfd = 'fixed'
	self.sample['font'] = xlfd

    def set_font_attrs(self, attrs):
	self.font_attrs = attrs
	self.font_attr_list.SetList(attrs)
	self.font_attr = get_from_list(self.font_attr, attrs)
	self.font_attr_list.Select(attrs.index(self.font_attr), 1)

    def current_font_xlfd(self):
	fonts = self.family_to_fonts[self.font_family]
	for name in fonts:
	    family, attrs, xlfd_start, encoding = font.fontmap[name]
	    if attrs == self.font_attr:
		return font.xlfd_template % (xlfd_start, 24, encoding)
	return ''

    def current_font_ps(self):
	fonts = self.family_to_fonts[self.font_family]
	for name in fonts:
	    family, attrs, xlfd_start, encoding = font.fontmap[name]
	    if attrs == self.font_attr:
		return name
	return ''

    def family_selected(self):
	sel = self.family_list.curselection()
	if sel:
	    index = string.atoi(sel[0])
	    self.font_family = self.families[index]
	    self.update_from_family(set_view = 0)

    def attr_selected(self):
	sel = self.font_attr_list.curselection()
	if sel:
	    index = string.atoi(sel[0])
	    self.font_attr = self.font_attrs[index]
	    self.update_sample()

    def size_selected(self):
	sel = self.size_list.curselection()
	if sel:
	    self.var_size.set(self.size_list.get(sel[0]))

    def apply_size(self, *args):
	if self.can_apply():
	    size = self.var_size.get()
	    self.document.CallObjectMethod(text.CommonText,
					   _("Set Font Size %.1f") % size,
					   'SetFontSize', size)

    def save_prefs(self):
	StylePropertyPanel.save_prefs(self)
	config.preferences.sample_text = self.sample_text.get()
コード例 #37
0
class wm_seg:
    """
    Simple GUI application
    If the application inside a container, automatic updates are removed.

    The application uses two frames (tabs):
    - training
    - testing
    """
    def __init__(self, master, container):

        self.master = master
        master.title("nicMSlesions")

        # running on a container
        self.container = container

        # gui attributes
        self.path = os.getcwd()
        self.default_config = None
        self.user_config = None
        self.current_folder = os.getcwd()
        self.list_train_pretrained_nets = []
        self.list_test_nets = []
        self.version = __version__
        if self.container is False:
            # version_number
            self.commit_version = subprocess.check_output(
                ['git', 'rev-parse', 'HEAD'])

        # queue and thread parameters. All processes are embedded
        # inside threads to avoid freezing the application
        self.train_task = None
        self.test_task = None
        self.test_queue = Queue.Queue()
        self.train_queue = Queue.Queue()

        # --------------------------------------------------
        # parameters. Mostly from the config/*.cfg files
        # --------------------------------------------------

        # data parameters
        self.param_training_folder = StringVar()
        self.param_test_folder = StringVar()
        self.param_FLAIR_tag = StringVar()
        self.param_T1_tag = StringVar()
        self.param_MOD3_tag = StringVar()
        self.param_MOD4_tag = StringVar()
        self.param_mask_tag = StringVar()
        self.param_model_tag = StringVar()
        self.param_register_modalities = BooleanVar()
        self.param_skull_stripping = BooleanVar()
        self.param_denoise = BooleanVar()
        self.param_denoise_iter = IntVar()
        self.param_save_tmp = BooleanVar()
        self.param_debug = BooleanVar()

        # train parameters
        self.param_net_folder = os.path.join(self.current_folder, 'nets')
        self.param_use_pretrained_model = BooleanVar()
        self.param_pretrained_model = StringVar()
        self.param_inference_model = StringVar()
        self.param_num_layers = IntVar()
        self.param_net_name = StringVar()
        self.param_net_name.set('None')
        self.param_balanced_dataset = StringVar()
        self.param_fract_negatives = DoubleVar()

        # model parameters
        self.param_pretrained = None
        self.param_min_th = DoubleVar()
        self.param_patch_size = IntVar()
        self.param_weight_paths = StringVar()
        self.param_load_weights = BooleanVar()
        self.param_train_split = DoubleVar()
        self.param_max_epochs = IntVar()
        self.param_patience = IntVar()
        self.param_batch_size = IntVar()
        self.param_net_verbose = IntVar()
        self.param_t_bin = DoubleVar()
        self.param_l_min = IntVar()
        self.param_min_error = DoubleVar()
        self.param_mode = BooleanVar()
        self.param_gpu_number = IntVar()

        # load the default configuration from the conf file
        self.load_default_configuration()

        # self frame (tabbed notebook)
        self.note = Notebook(self.master)
        self.note.pack()

        os.system('cls' if platform.system() == 'Windows' else 'clear')
        print "##################################################"
        print "# ------------                                   #"
        print "# nicMSlesions                                   #"
        print "# ------------                                   #"
        print "# MS WM lesion segmentation                      #"
        print "#                                                #"
        print "# -------------------------------                #"
        print "# (c) Sergi Valverde 2018                        #"
        print "# Neuroimage Computing Group                     #"
        print "# -------------------------------                #"
        print "##################################################\n"
        print "Please select options for training or inference in the menu..."

        # --------------------------------------------------
        # training tab
        # --------------------------------------------------
        self.train_frame = Frame()
        self.note.add(self.train_frame, text="Training")
        self.test_frame = Frame()
        self.note.add(self.test_frame, text="Inference")

        # label frames
        cl_s = 5
        self.tr_frame = LabelFrame(self.train_frame, text="Training images:")
        self.tr_frame.grid(row=0,
                           columnspan=cl_s,
                           sticky='WE',
                           padx=5,
                           pady=5,
                           ipadx=5,
                           ipady=5)
        self.model_frame = LabelFrame(self.train_frame, text="CNN model:")
        self.model_frame.grid(row=5,
                              columnspan=cl_s,
                              sticky='WE',
                              padx=5,
                              pady=5,
                              ipadx=5,
                              ipady=5)

        # training options
        self.inFolderLbl = Label(self.tr_frame, text="Training folder:")
        self.inFolderLbl.grid(row=0, column=0, sticky='E', padx=5, pady=2)
        self.inFolderTxt = Entry(self.tr_frame)
        self.inFolderTxt.grid(row=0,
                              column=1,
                              columnspan=5,
                              sticky="W",
                              pady=3)
        self.inFileBtn = Button(self.tr_frame,
                                text="Browse ...",
                                command=self.load_training_path)
        self.inFileBtn.grid(row=0,
                            column=5,
                            columnspan=1,
                            sticky='W',
                            padx=5,
                            pady=1)

        self.optionsBtn = Button(self.tr_frame,
                                 text="Other options",
                                 command=self.parameter_window)
        self.optionsBtn.grid(row=0,
                             column=10,
                             columnspan=1,
                             sticky="W",
                             padx=(100, 1),
                             pady=1)

        # setting input modalities: FLAIR + T1 are mandatory
        # Mod 3 / 4 are optional
        self.flairTagLbl = Label(self.tr_frame, text="FLAIR tag:")
        self.flairTagLbl.grid(row=1, column=0, sticky='E', padx=5, pady=2)
        self.flairTxt = Entry(self.tr_frame, textvariable=self.param_FLAIR_tag)
        self.flairTxt.grid(row=1, column=1, columnspan=1, sticky="W", pady=1)

        self.t1TagLbl = Label(self.tr_frame, text="T1 tag:")
        self.t1TagLbl.grid(row=2, column=0, sticky='E', padx=5, pady=2)
        self.t1Txt = Entry(self.tr_frame, textvariable=self.param_T1_tag)
        self.t1Txt.grid(row=2, column=1, columnspan=1, sticky="W", pady=1)

        self.mod3TagLbl = Label(self.tr_frame, text="mod 3 tag:")
        self.mod3TagLbl.grid(row=3, column=0, sticky='E', padx=5, pady=2)
        self.mod3Txt = Entry(self.tr_frame, textvariable=self.param_MOD3_tag)
        self.mod3Txt.grid(row=3, column=1, columnspan=1, sticky="W", pady=1)

        self.mod4TagLbl = Label(self.tr_frame, text="mod 4 tag:")
        self.mod4TagLbl.grid(row=4, column=0, sticky='E', padx=5, pady=2)
        self.mod4Txt = Entry(self.tr_frame, textvariable=self.param_MOD4_tag)
        self.mod4Txt.grid(row=4, column=1, columnspan=1, sticky="W", pady=1)

        self.maskTagLbl = Label(self.tr_frame, text="MASK tag:")
        self.maskTagLbl.grid(row=5, column=0, sticky='E', padx=5, pady=2)
        self.maskTxt = Entry(self.tr_frame, textvariable=self.param_mask_tag)
        self.maskTxt.grid(row=5, column=1, columnspan=1, sticky="W", pady=1)

        # model options
        self.modelTagLbl = Label(self.model_frame, text="Model name:")
        self.modelTagLbl.grid(row=6, column=0, sticky='E', padx=5, pady=2)
        self.modelTxt = Entry(self.model_frame,
                              textvariable=self.param_net_name)
        self.modelTxt.grid(row=6, column=1, columnspan=1, sticky="W", pady=1)

        self.checkPretrain = Checkbutton(self.model_frame,
                                         text="use pretrained",
                                         var=self.param_use_pretrained_model)
        self.checkPretrain.grid(row=6, column=3, padx=5, pady=5)

        self.update_pretrained_nets()

        self.pretrainTxt = OptionMenu(self.model_frame,
                                      self.param_pretrained_model,
                                      *self.list_train_pretrained_nets)
        self.pretrainTxt.grid(row=6, column=5, sticky='E', padx=5, pady=5)

        # START button links
        self.trainingBtn = Button(self.train_frame,
                                  state='disabled',
                                  text="Start training",
                                  command=self.train_net)
        self.trainingBtn.grid(row=7, column=0, sticky='W', padx=1, pady=1)

        # --------------------------------------------------
        # inference tab
        # --------------------------------------------------
        self.tt_frame = LabelFrame(self.test_frame, text="Inference images:")
        self.tt_frame.grid(row=0,
                           columnspan=cl_s,
                           sticky='WE',
                           padx=5,
                           pady=5,
                           ipadx=5,
                           ipady=5)
        self.test_model_frame = LabelFrame(self.test_frame, text="CNN model:")
        self.test_model_frame.grid(row=5,
                                   columnspan=cl_s,
                                   sticky='WE',
                                   padx=5,
                                   pady=5,
                                   ipadx=5,
                                   ipady=5)

        # testing options
        self.test_inFolderLbl = Label(self.tt_frame, text="Testing folder:")
        self.test_inFolderLbl.grid(row=0, column=0, sticky='E', padx=5, pady=2)
        self.test_inFolderTxt = Entry(self.tt_frame)
        self.test_inFolderTxt.grid(row=0,
                                   column=1,
                                   columnspan=5,
                                   sticky="W",
                                   pady=3)
        self.test_inFileBtn = Button(self.tt_frame,
                                     text="Browse ...",
                                     command=self.load_testing_path)
        self.test_inFileBtn.grid(row=0,
                                 column=5,
                                 columnspan=1,
                                 sticky='W',
                                 padx=5,
                                 pady=1)

        self.test_optionsBtn = Button(self.tt_frame,
                                      text="Other options",
                                      command=self.parameter_window)
        self.test_optionsBtn.grid(row=0,
                                  column=10,
                                  columnspan=1,
                                  sticky="W",
                                  padx=(100, 1),
                                  pady=1)

        self.test_flairTagLbl = Label(self.tt_frame, text="FLAIR tag:")
        self.test_flairTagLbl.grid(row=1, column=0, sticky='E', padx=5, pady=2)
        self.test_flairTxt = Entry(self.tt_frame,
                                   textvariable=self.param_FLAIR_tag)
        self.test_flairTxt.grid(row=1,
                                column=1,
                                columnspan=1,
                                sticky="W",
                                pady=1)

        self.test_t1TagLbl = Label(self.tt_frame, text="T1 tag:")
        self.test_t1TagLbl.grid(row=2, column=0, sticky='E', padx=5, pady=2)
        self.test_t1Txt = Entry(self.tt_frame, textvariable=self.param_T1_tag)
        self.test_t1Txt.grid(row=2, column=1, columnspan=1, sticky="W", pady=1)

        self.test_mod3TagLbl = Label(self.tt_frame, text="mod 3 tag:")
        self.test_mod3TagLbl.grid(row=3, column=0, sticky='E', padx=5, pady=2)
        self.test_mod3Txt = Entry(self.tt_frame,
                                  textvariable=self.param_MOD3_tag)
        self.test_mod3Txt.grid(row=3,
                               column=1,
                               columnspan=1,
                               sticky="W",
                               pady=1)

        self.test_mod4TagLbl = Label(self.tt_frame, text="mod 4 tag:")
        self.test_mod4TagLbl.grid(row=4, column=0, sticky='E', padx=5, pady=2)
        self.test_mod4Txt = Entry(self.tt_frame,
                                  textvariable=self.param_MOD4_tag)
        self.test_mod4Txt.grid(row=4,
                               column=1,
                               columnspan=1,
                               sticky="W",
                               pady=1)

        self.test_pretrainTxt = OptionMenu(self.test_model_frame,
                                           self.param_inference_model,
                                           *self.list_test_nets)

        self.param_inference_model.set('None')
        self.test_pretrainTxt.grid(row=5, column=0, sticky='E', padx=5, pady=5)

        # START button links cto docker task
        self.inferenceBtn = Button(self.test_frame,
                                   state='disabled',
                                   text="Start inference",
                                   command=self.infer_segmentation)
        self.inferenceBtn.grid(row=7, column=0, sticky='W', padx=1, pady=1)

        # train / test ABOUT button
        self.train_aboutBtn = Button(self.train_frame,
                                     text="about",
                                     command=self.about_window)
        self.train_aboutBtn.grid(row=7,
                                 column=4,
                                 sticky='E',
                                 padx=(1, 1),
                                 pady=1)

        self.test_aboutBtn = Button(self.test_frame,
                                    text="about",
                                    command=self.about_window)
        self.test_aboutBtn.grid(row=7,
                                column=4,
                                sticky='E',
                                padx=(1, 1),
                                pady=1)

        # Processing state
        self.process_indicator = StringVar()
        self.process_indicator.set(' ')
        self.label_indicator = Label(master,
                                     textvariable=self.process_indicator)
        self.label_indicator.pack(side="left")

        # Closing processing events is implemented via
        # a master protocol
        self.master.protocol("WM_DELETE_WINDOW", self.close_event)

    def parameter_window(self):
        """
        Setting other parameters using an emerging window
        CNN parameters, CUDA device, post-processing....

        """
        t = Toplevel(self.master)
        t.wm_title("Other parameters")

        # data parameters
        t_data = LabelFrame(t, text="data options:")
        t_data.grid(row=0, sticky="WE")
        checkPretrain = Checkbutton(t_data,
                                    text="Register modalities",
                                    var=self.param_register_modalities)
        checkPretrain.grid(row=0, sticky='W')
        checkSkull = Checkbutton(t_data,
                                 text="Skull-strip modalities",
                                 var=self.param_skull_stripping)
        checkSkull.grid(row=1, sticky="W")
        checkDenoise = Checkbutton(t_data,
                                   text="Denoise masks",
                                   var=self.param_denoise)
        checkDenoise.grid(row=2, sticky="W")

        denoise_iter_label = Label(t_data,
                                   text=" Denoise iter:               ")
        denoise_iter_label.grid(row=3, sticky="W")
        denoise_iter_entry = Entry(t_data,
                                   textvariable=self.param_denoise_iter)
        denoise_iter_entry.grid(row=3, column=1, sticky="E")

        check_tmp = Checkbutton(t_data,
                                text="Save tmp files",
                                var=self.param_save_tmp)
        check_tmp.grid(row=4, sticky="W")
        checkdebug = Checkbutton(t_data,
                                 text="Debug mode",
                                 var=self.param_debug)
        checkdebug.grid(row=5, sticky="W")

        # model parameters
        t_model = LabelFrame(t, text="Model:")
        t_model.grid(row=5, sticky="EW")

        maxepochs_label = Label(t_model, text="Max epochs:                  ")
        maxepochs_label.grid(row=6, sticky="W")
        maxepochs_entry = Entry(t_model, textvariable=self.param_max_epochs)
        maxepochs_entry.grid(row=6, column=1, sticky="E")

        trainsplit_label = Label(t_model, text="Validation %:           ")
        trainsplit_label.grid(row=7, sticky="W")
        trainsplit_entry = Entry(t_model, textvariable=self.param_train_split)
        trainsplit_entry.grid(row=7, column=1, sticky="E")

        batchsize_label = Label(t_model, text="Test batch size:")
        batchsize_label.grid(row=8, sticky="W")
        batchsize_entry = Entry(t_model, textvariable=self.param_batch_size)
        batchsize_entry.grid(row=8, column=1, sticky="E")

        mode_label = Label(t_model, text="Verbosity:")
        mode_label.grid(row=9, sticky="W")
        mode_entry = Entry(t_model, textvariable=self.param_net_verbose)
        mode_entry.grid(row=9, column=1, sticky="E")

        #gpu_mode = Checkbutton(t_model,
        #                         text="GPU:",
        #                         var=self.param_mode)
        #gpu_mode.grid(row=10, sticky="W")

        gpu_number = Label(t_model, text="GPU number:")
        gpu_number.grid(row=10, sticky="W")
        gpu_entry = Entry(t_model, textvariable=self.param_gpu_number)
        gpu_entry.grid(row=10, column=1, sticky="W")

        # training parameters
        tr_model = LabelFrame(t, text="Training:")
        tr_model.grid(row=12, sticky="EW")

        balanced_label = Label(tr_model, text="Balanced dataset:    ")
        balanced_label.grid(row=13, sticky="W")
        balanced_entry = Entry(tr_model,
                               textvariable=self.param_balanced_dataset)
        balanced_entry.grid(row=13, column=1, sticky="E")

        fraction_label = Label(tr_model, text="Fraction negative/positives: ")
        fraction_label.grid(row=14, sticky="W")
        fraction_entry = Entry(tr_model,
                               textvariable=self.param_fract_negatives)
        fraction_entry.grid(row=14, column=1, sticky="E")

        # postprocessing parameters
        t_post = LabelFrame(t, text="Post-processing:  ")
        t_post.grid(row=15, sticky="EW")
        t_bin_label = Label(t_post, text="Out probability th:      ")
        t_bin_label.grid(row=16, sticky="W")
        t_bin_entry = Entry(t_post, textvariable=self.param_t_bin)
        t_bin_entry.grid(row=16, column=1, sticky="E")

        l_min_label = Label(t_post, text="Min out region size:         ")
        l_min_label.grid(row=17, sticky="W")
        l_min_entry = Entry(t_post, textvariable=self.param_l_min)
        l_min_entry.grid(row=17, column=1, sticky="E")

        vol_min_label = Label(t_post, text="Min vol error (ml):   ")
        vol_min_label.grid(row=18, sticky="W")
        vol_min_entry = Entry(t_post, textvariable=self.param_min_error)
        vol_min_entry.grid(row=18, column=1, sticky="E")

    def load_default_configuration(self):
        """
        load the default configuration from /config/default.cfg
        This method assign each of the configuration parameters to
        class attributes
        """

        default_config = ConfigParser.SafeConfigParser()
        default_config.read(os.path.join(self.path, 'config', 'default.cfg'))

        # dastaset parameters
        self.param_training_folder.set(
            default_config.get('database', 'train_folder'))
        self.param_test_folder.set(
            default_config.get('database', 'inference_folder'))
        self.param_FLAIR_tag.set(default_config.get('database', 'flair_tags'))
        self.param_T1_tag.set(default_config.get('database', 't1_tags'))
        self.param_MOD3_tag.set(default_config.get('database', 'mod3_tags'))
        self.param_MOD4_tag.set(default_config.get('database', 'mod4_tags'))
        self.param_mask_tag.set(default_config.get('database', 'roi_tags'))
        self.param_register_modalities.set(
            default_config.get('database', 'register_modalities'))
        self.param_denoise.set(default_config.get('database', 'denoise'))
        self.param_denoise_iter.set(
            default_config.getint('database', 'denoise_iter'))
        self.param_skull_stripping.set(
            default_config.get('database', 'skull_stripping'))
        self.param_save_tmp.set(default_config.get('database', 'save_tmp'))
        self.param_debug.set(default_config.get('database', 'debug'))

        # train parameters
        self.param_use_pretrained_model.set(
            default_config.get('train', 'full_train'))
        self.param_pretrained_model.set(
            default_config.get('train', 'pretrained_model'))
        self.param_inference_model.set("      ")
        self.param_balanced_dataset.set(
            default_config.get('train', 'balanced_training'))
        self.param_fract_negatives.set(
            default_config.getfloat('train', 'fraction_negatives'))

        # model parameters
        self.param_net_folder = os.path.join(self.current_folder, 'nets')
        self.param_net_name.set(default_config.get('model', 'name'))
        self.param_train_split.set(
            default_config.getfloat('model', 'train_split'))
        self.param_max_epochs.set(default_config.getint('model', 'max_epochs'))
        self.param_patience.set(default_config.getint('model', 'patience'))
        self.param_batch_size.set(default_config.getint('model', 'batch_size'))
        self.param_net_verbose.set(default_config.get('model', 'net_verbose'))
        self.param_gpu_number.set(default_config.getint('model', 'gpu_number'))
        # self.param_mode.set(default_config.get('model', 'gpu_mode'))

        # post-processing
        self.param_l_min.set(default_config.getint('postprocessing', 'l_min'))
        self.param_t_bin.set(default_config.getfloat('postprocessing',
                                                     't_bin'))
        self.param_min_error.set(
            default_config.getfloat('postprocessing', 'min_error'))

    def write_user_configuration(self):
        """
        write the configuration into config/configuration.cfg
        """
        user_config = ConfigParser.RawConfigParser()

        # dataset parameters
        user_config.add_section('database')
        user_config.set('database', 'train_folder',
                        self.param_training_folder.get())
        user_config.set('database', 'inference_folder',
                        self.param_test_folder.get())
        user_config.set('database', 'flair_tags', self.param_FLAIR_tag.get())
        user_config.set('database', 't1_tags', self.param_T1_tag.get())
        user_config.set('database', 'mod3_tags', self.param_MOD3_tag.get())
        user_config.set('database', 'mod4_tags', self.param_MOD4_tag.get())
        user_config.set('database', 'roi_tags', self.param_mask_tag.get())

        user_config.set('database', 'register_modalities',
                        self.param_register_modalities.get())
        user_config.set('database', 'denoise', self.param_denoise.get())
        user_config.set('database', 'denoise_iter',
                        self.param_denoise_iter.get())
        user_config.set('database', 'skull_stripping',
                        self.param_skull_stripping.get())
        user_config.set('database', 'save_tmp', self.param_save_tmp.get())
        user_config.set('database', 'debug', self.param_debug.get())

        # train parameters
        user_config.add_section('train')
        user_config.set('train', 'full_train',
                        not (self.param_use_pretrained_model.get()))
        user_config.set('train', 'pretrained_model',
                        self.param_pretrained_model.get())
        user_config.set('train', 'balanced_training',
                        self.param_balanced_dataset.get())
        user_config.set('train', 'fraction_negatives',
                        self.param_fract_negatives.get())
        # model parameters
        user_config.add_section('model')
        user_config.set('model', 'name', self.param_net_name.get())
        user_config.set('model', 'pretrained', self.param_pretrained)
        user_config.set('model', 'train_split', self.param_train_split.get())
        user_config.set('model', 'max_epochs', self.param_max_epochs.get())
        user_config.set('model', 'patience', self.param_patience.get())
        user_config.set('model', 'batch_size', self.param_batch_size.get())
        user_config.set('model', 'net_verbose', self.param_net_verbose.get())
        # user_config.set('model', 'gpu_mode', self.param_mode.get())
        user_config.set('model', 'gpu_number', self.param_gpu_number.get())

        # postprocessing parameters
        user_config.add_section('postprocessing')
        user_config.set('postprocessing', 't_bin', self.param_t_bin.get())
        user_config.set('postprocessing', 'l_min', self.param_l_min.get())
        user_config.set('postprocessing', 'min_error',
                        self.param_min_error.get())

        # Writing our configuration file to 'example.cfg'
        with open(os.path.join(self.path, 'config', 'configuration.cfg'),
                  'wb') as configfile:
            user_config.write(configfile)

    def load_training_path(self):
        """
        Select training path from disk and write it.
        If the app is run inside a container,
        link the iniitaldir with /data
        """
        initialdir = '/data' if self.container else os.getcwd()
        fname = askdirectory(initialdir=initialdir)
        if fname:
            try:
                self.param_training_folder.set(fname)
                self.inFolderTxt.delete(0, END)
                self.inFolderTxt.insert(0, self.param_training_folder.get())
                self.trainingBtn['state'] = 'normal'
            except:
                pass

    def load_testing_path(self):
        """
        Selecet the inference path from disk and write it
        If the app is run inside a container,
        link the iniitaldir with /data
        """
        initialdir = '/data' if self.container else os.getcwd()
        fname = askdirectory(initialdir=initialdir)
        if fname:
            try:
                self.param_test_folder.set(fname)
                self.test_inFolderTxt.delete(0, END)
                self.test_inFolderTxt.insert(0, self.param_test_folder.get())
                self.inferenceBtn['state'] = 'normal'
            except:
                pass

    def update_pretrained_nets(self):
        """
        get a list of the  different net configuration present in the system.
        Each model configuration is represented by a folder containing the network
        weights for each of the networks. The baseline net config is always
        included by default

        """
        folders = os.listdir(self.param_net_folder)
        self.list_train_pretrained_nets = folders
        self.list_test_nets = folders

    def write_to_console(self, txt):
        """
        to doc:
        important method
        """
        self.command_out.insert(END, str(txt))

    def write_to_test_console(self, txt):
        """
        to doc:
        important method
        """
        self.test_command_out.insert(END, str(txt))

    def infer_segmentation(self):
        """
        Method implementing the inference process:
        - Check network selection
        - write the configuration to disk
        - Run the process on a new thread
        """

        if self.param_inference_model.get() == 'None':
            print "ERROR: Please, select a network model before starting...\n"
            return
        if self.test_task is None:
            self.inferenceBtn.config(state='disabled')
            self.param_net_name.set(self.param_inference_model.get())
            self.param_use_pretrained_model.set(False)
            self.write_user_configuration()
            print "\n-----------------------"
            print "Running configuration:"
            print "-----------------------"
            print "Inference model:", self.param_model_tag.get()
            print "Inference folder:", self.param_test_folder.get(), "\n"

            print "Method info:"
            print "------------"
            self.test_task = ThreadedTask(self.write_to_test_console,
                                          self.test_queue,
                                          mode='testing')
            self.test_task.start()
            self.master.after(100, self.process_container_queue)

    def train_net(self):
        """
        Method implementing the training process:
        - write the configuration to disk
        - Run the process on a new thread
        """

        if self.param_net_name.get() == 'None':
            print "ERROR: Please, define network name before starting...\n"
            return

        self.trainingBtn['state'] = 'disable'

        if self.train_task is None:
            self.trainingBtn.update()
            self.write_user_configuration()
            print "\n-----------------------"
            print "Running configuration:"
            print "-----------------------"
            print "Train model:", self.param_net_name.get()
            print "Training folder:", self.param_training_folder.get(), "\n"

            print "Method info:"
            print "------------"

            self.train_task = ThreadedTask(self.write_to_console,
                                           self.test_queue,
                                           mode='training')
            self.train_task.start()
            self.master.after(100, self.process_container_queue)

    def check_update(self):
        """
            check update version and propose to download it if differnt
            So far, a rudimentary mode is used to check the last version.
            """

        # I have to discard possible local changes :(
        print "---------------------------------------"
        print "Updating software"
        print "current version:", self.commit_version

        remote_commit = subprocess.check_output(['git', 'stash'])
        remote_commit = subprocess.check_output(['git', 'fetch'])
        remote_commit = subprocess.check_output(
            ['git', 'rev-parse', 'origin/master'])

        if remote_commit != self.commit_version:
            proc = subprocess.check_output(['git', 'pull', 'origin', 'master'])
            self.check_link.config(text="Updated")
            self.commit_version = remote_commit
            print "updated version:", self.commit_version
        else:
            print "This software is already in the latest version"
        print "---------------------------------------"

    def about_window(self):
        """
        Window showing information about the software and
        version number, including auto-update. If the application
        is run from a container, then auto-update is disabled
        """
        def callback(event):
            """
            open webbrowser when clicking links
            """
            webbrowser.open_new(event.widget.cget("text"))

        # main window
        t = Toplevel(self.master, width=500, height=500)
        t.wm_title("About")

        # NIC logo + name
        title = Label(t,
                      text="nicMSlesions v" + self.version + "\n"
                      "Multiple Sclerosis White Matter Lesion Segmentation")
        title.grid(row=2, column=1, padx=20, pady=10)
        img = ImageTk.PhotoImage(Image.open('./logonic.png'))
        imglabel = Label(t, image=img)
        imglabel.image = img
        imglabel.grid(row=1, column=1, padx=10, pady=10)
        group_name = Label(t,
                           text="Copyright Sergi Valverde (2018-) \n " +
                           "NeuroImage Computing Group")
        group_name.grid(row=3, column=1)
        group_link = Label(t,
                           text=r"http://atc.udg.edu/nic",
                           fg="blue",
                           cursor="hand2")
        group_link.grid(row=4, column=1)
        group_link.bind("<Button-1>", callback)

        license_content = "Licensed under the BSD 2-Clause license. \n" + \
                          "A copy of the license is present in the root directory."

        license_label = Label(t, text=license_content)
        license_label.grid(row=5, column=1, padx=20, pady=20)

        # if self.container is False:
        #     # check version and updates
        #     version_number = Label(t, text="commit: " + self.commit_version)
        #     version_number.grid(row=6, column=1, padx=20, pady=(1, 1))
        #
        #     self.check_link = Button(t,
        #                         text="Check for updates",
        #                         command=self.check_update)
        #     self.check_link.grid(row=7, column=1)

    def process_container_queue(self):
        """
        Process the threading queue. When the threaded processes are
        finished, buttons are reset and a message is shown in the app.
        """
        self.process_indicator.set('Running... please wait')
        try:
            msg = self.test_queue.get(0)
            self.process_indicator.set('Done. See log for more details.')
            self.inferenceBtn['state'] = 'normal'
            self.trainingBtn['state'] = 'normal'
        except Queue.Empty:
            self.master.after(100, self.process_container_queue)

    def close_event(self):
        """
        Stop the thread processes using OS related calls.
        """
        if self.train_task is not None:
            self.train_task.stop_process()
        if self.test_task is not None:
            self.test_task.stop_process()
        os.system('cls' if platform.system == "Windows" else 'clear')
        root.destroy()
コード例 #38
0
class RotatePanel(PluginPanel):
    name = 'Rotate'
    title = _("Rotate")

    def init(self, master):
        PluginPanel.init(self, master)

        root = self.mw.root

        self.var_angle = DoubleVar(root)
        self.var_angle.set(0)

        self.var_width_number = DoubleVar(root)
        self.var_height_number = DoubleVar(root)

        self.var_width_base = DoubleVar(root)
        self.var_height_base = DoubleVar(root)

        self.cnt_x_absolute = None
        self.cnt_y_absolute = None

        var_width_unit = StringVar(root)
        var_height_unit = StringVar(root)

        unit = config.preferences.default_unit
        self.var_width = LengthVar(10, unit, self.var_width_number,
                                   var_width_unit)
        self.var_height = LengthVar(10, unit, self.var_height_number,
                                    var_height_unit)

        jump = config.preferences.default_unit_jump
        self.var_width.set(0)
        self.var_height.set(0)

        self.var_width_base.set(0)
        self.var_height_base.set(0)

        self.var_position = StringVar(root)
        self.var_position.set(ABSOLUTE)

        self.var_basepoint = StringVar(root)
        self.var_basepoint.set('C')

        #---------------------------------------------------------
        top = TFrame(self.panel, style='FlatFrame')
        top.pack(side=TOP, fill=BOTH)
        #---------------------------------------------------------

        angle_frame = TFrame(top, style='FlatFrame', borderwidth=3)
        angle_frame.pack(side=TOP, fill=BOTH)
        label = TLabel(angle_frame,
                       style='FlatLabel',
                       text=" " + _("Angle:") + " ")
        label.pack(side=LEFT, padx=5)

        self.entry_angle = TSpinbox(angle_frame,
                                    var=0,
                                    vartype=1,
                                    textvariable=self.var_angle,
                                    min=-360,
                                    max=360,
                                    step=5,
                                    width=6,
                                    command=self.apply_rotate)
        self.entry_angle.pack(side=LEFT, anchor=E)
        label = TLabel(angle_frame, style='FlatLabel', text=_("deg"))
        label.pack(side=LEFT, padx=5)
        #---------------------------------------------------------
        label = TLabel(top, style='FlatLabel', text=_("Center:"))
        label.pack(side=TOP, fill=BOTH, padx=5)

        #---------------------------------------------------------
        # Horisontal
        size_frameH = TFrame(top, style='FlatFrame', borderwidth=3)
        size_frameH.pack(side=TOP, fill=BOTH)

        label = TLabel(size_frameH, style='FlatLabel', image='center_h')
        label.pack(side=LEFT, padx=5)
        self.entry_width = TSpinbox(size_frameH,
                                    var=0,
                                    vartype=1,
                                    textvariable=self.var_width_number,
                                    min=-50000,
                                    max=50000,
                                    step=jump,
                                    width=10,
                                    command=self.apply_rotate)
        self.entry_width.pack(side=LEFT)

        self.labelwunit = TLabel(size_frameH,
                                 style='FlatLabel',
                                 text=self.var_width.unit)
        self.labelwunit.pack(side=LEFT, padx=5)
        #---------------------------------------------------------
        # Vertical

        size_frameV = TFrame(top, style='FlatFrame', borderwidth=3)
        size_frameV.pack(side=TOP, fill=BOTH)
        label = TLabel(size_frameV, style='FlatLabel', image='center_v')
        label.pack(side=LEFT, padx=5)

        self.entry_height = TSpinbox(size_frameV,
                                     var=0,
                                     vartype=1,
                                     textvariable=self.var_height_number,
                                     min=-50000,
                                     max=50000,
                                     step=jump,
                                     width=10,
                                     command=self.apply_rotate)
        self.entry_height.pack(side=LEFT)

        self.labelhunit = TLabel(size_frameV,
                                 style='FlatLabel',
                                 text=self.var_height.unit)
        self.labelhunit.pack(side=LEFT, padx=5)

        #---------------------------------------------------------
        # position chek

        self.position_check = TCheckbutton(top,
                                           text=_("Absolute Center"),
                                           variable=self.var_position,
                                           onvalue=ABSOLUTE,
                                           offvalue=RELATIVE,
                                           command=self.position)
        self.position_check.pack(side=TOP, anchor=W, padx=5, pady=5)

        #---------------------------------------------------------
        # Basepoint check

        label = TLabel(top, style='FlatLabel', text=_("Basepoint:"))
        label.pack(side=TOP, fill=BOTH, padx=5)
        basepoint_frame = TLabelframe(top,
                                      labelwidget=label,
                                      style='Labelframe',
                                      borderwidth=4)
        basepoint_frame.pack(side=TOP, fill=X, padx=5, pady=2)

        self.Basepoint = BasePointSelector(basepoint_frame,
                                           anchor=self.var_basepoint,
                                           command=self.apply_basepoint)
        self.Basepoint.pack(side=LEFT, fill=BOTH, padx=5)

        label = TLabel(basepoint_frame,
                       style='FlatLabel',
                       image='coordinate_deg')
        label.pack(side=LEFT, fill=BOTH, padx=10)

        self.position_check.pack(side=TOP, anchor=W, padx=5, pady=5)

        #---------------------------------------------------------
        # Button frame

        button_frame = TFrame(top, style='FlatFrame', borderwidth=5)
        button_frame.pack(side=BOTTOM, fill=BOTH)

        self.update_buttons = []
        self.button = UpdatedButton(top,
                                    text=_("Apply"),
                                    command=self.apply_rotate)
        self.button.pack(in_=button_frame,
                         side=BOTTOM,
                         expand=1,
                         fill=X,
                         pady=3)

        self.button_copy = UpdatedButton(top,
                                         text=_("Apply to Copy"),
                                         command=self.apply_to_copy)
        self.button_copy.pack(in_=button_frame, side=BOTTOM, expand=1, fill=X)

        self.init_from_doc()
        self.subscribe_receivers()


###############################################################################

    def subscribe_receivers(self):
        self.document.Subscribe(SELECTION, self.Update)
        self.document.Subscribe(EDITED, self.update_var)
        config.preferences.Subscribe(CHANGED, self.update_pref)

    def unsubscribe_receivers(self):
        self.document.Unsubscribe(SELECTION, self.Update)
        self.document.Unsubscribe(EDITED, self.update_var)
        config.preferences.Unsubscribe(CHANGED, self.update_pref)

    def init_from_doc(self, *arg):
        self.Update()

    def Update(self, *arg):
        if self.is_selection():
            self.entry_angle.set_state(NORMAL)
            self.entry_width.set_state(NORMAL)
            self.entry_height.set_state(NORMAL)
            self.position_check['state'] = NORMAL
            self.button['state'] = NORMAL
            self.button_copy['state'] = NORMAL
            self.TestBasepoint()
        else:
            self.entry_angle.set_state(DISABLED)
            self.entry_width.set_state(DISABLED)
            self.entry_height.set_state(DISABLED)
            self.position_check['state'] = DISABLED
            self.button['state'] = DISABLED
            self.button_copy['state'] = DISABLED

        self.update_pref()

    def apply_basepoint(self):
        self.update_var()

    def position(self):
        self.update_var()

    def RotateSelected(self, angle, cnt=None):
        text = _("Rotation")
        if self.document.selection:
            self.document.begin_transaction(text)
            try:
                try:
                    if cnt is None:
                        cnt = self.document.selection.coord_rect.center()
                    angle = angle * degrees
                    trafo = Rotation(angle, cnt)
                    self.document.TransformSelected(trafo, text)
                except:
                    self.document.abort_transaction()
            finally:
                self.document.end_transaction()

    def RotateAndCopy(self, angle, cnt=None):
        text = _("Rotation&Copy")
        if self.document.selection:
            self.document.begin_transaction(text)
            try:
                try:
                    if cnt is None:
                        cnt = self.document.selection.coord_rect.center()
                    angle = angle * degrees
                    trafo = Rotation(angle, cnt)
                    self.document.ApplyToDuplicate()
                    self.document.TransformSelected(trafo, text)
                except:
                    self.document.abort_transaction()
            finally:
                self.document.end_transaction()

    def apply_rotate(self, *arg):
        if self.button["state"] == DISABLED:
            return

        try:
            var_x = self.var_width.get()
            var_y = self.var_height.get()
            var_a = self.var_angle.get()
        except:
            return

        if var_a < 0:
            if var_a < -360:
                var_a += int(var_a / 360) * 360
            var_a += 360

        if self.var_basepoint.get() != 'USER':
            self.cnt_x_absolute, self.cnt_y_absolute = self.coordinates(
                ABSOLUTE)
            self.var_basepoint.set('USER')

        if self.var_width_base != var_x or self.var_height_base != var_y:

            if self.var_position.get() == ABSOLUTE:
                self.cnt_x_absolute = var_x
                self.cnt_y_absolute = var_y
            else:
                x, y = self.coordinates(ABSOLUTE, 'C')
                self.cnt_x_absolute = var_x + x
                self.cnt_y_absolute = var_y + y

            self.var_basepoint.set('USER')

        if arg and arg[0] == 'Duplicate':
            self.RotateAndCopy(var_a,
                               Point(self.cnt_x_absolute, self.cnt_y_absolute))
        else:
            self.RotateSelected(
                var_a, Point(self.cnt_x_absolute, self.cnt_y_absolute))

    def apply_to_copy(self):
        self.apply_rotate('Duplicate')

    def coordinates(self, position, anchor=None):
        br = self.document.selection.coord_rect
        hor_sel = br.right - br.left
        ver_sel = br.top - br.bottom

        if position == RELATIVE:
            left, bottom = -hor_sel / 2.0, -ver_sel / 2.0
        else:
            left, bottom = br.left, br.bottom

        cnt_x, cnt_y = self.Basepoint.get_basepoint(hor_sel, ver_sel, left,
                                                    bottom, anchor)
        return cnt_x, cnt_y

    def TestBasepoint(self):
        if self.cnt_x_absolute is None:
            return
        base = ['C', 'NW', 'N', 'NE', 'W', 'E', 'SW', 'S', 'SE']
        for b in xrange(len(base)):
            cnt_x, cnt_y = self.coordinates(ABSOLUTE, base[b])
            if round(cnt_x,2) == round(self.cnt_x_absolute,2) and \
                 round(cnt_y,2) == round(self.cnt_y_absolute,2):
                self.var_basepoint.set(base[b])
                return
        self.var_basepoint.set('USER')

    def update_pref(self, *arg):
        self.labelwunit['text'] = config.preferences.default_unit
        self.labelhunit['text'] = config.preferences.default_unit
        self.entry_width.step = config.preferences.default_unit_jump
        self.entry_height.step = config.preferences.default_unit_jump
        self.update_var()

    def update_var(self, *arg):
        if len(self.document.selection.GetInfo()):

            self.var_width.unit = config.preferences.default_unit
            self.var_height.unit = config.preferences.default_unit

            if self.var_basepoint.get() == 'USER':

                if self.var_position.get() == ABSOLUTE:
                    self.var_width.set(self.cnt_x_absolute)
                    self.var_height.set(self.cnt_y_absolute)
                else:
                    x, y = self.coordinates(ABSOLUTE, 'C')
                    self.var_width.set(self.cnt_x_absolute - x)
                    self.var_height.set(self.cnt_y_absolute - y)

            else:
                x, y = self.coordinates(self.var_position.get())
                self.var_width.set(x)
                self.var_height.set(y)
                self.var_width_base = self.var_width.get()
                self.var_height_base = self.var_height.get()

    def is_selection(self):
        return (len(self.document.selection) > 0)
コード例 #39
0
ファイル: DBLR_GUI.py プロジェクト: jjgomezcadenas/IC
class Example(Frame):

	def __init__(self, parent):
		Frame.__init__(self, parent)
		self.parent = parent
		self.initUI()
		self.centerUI(w=560,h=235)

	def initUI(self):

		self.parent.title("DBLR for Dummies")
		self.pack(fill=BOTH, expand=True)

		#self.columnconfigure(0, weight=1)
		#self.rowconfigure(0, weight=1)
		# weight attibute is used to make them growable

		self.meas       = IntVar()
		self.point      = IntVar()
		self.base_path  = StringVar()
		self.coef       = DoubleVar()
		self.noise      = DoubleVar()
		self.n_sigma    = DoubleVar()
		self.thr1       = DoubleVar()
		self.thr2       = DoubleVar()
		self.thr3       = DoubleVar()
		self.graph_sw   = BooleanVar()

		#Image
		factor=0.65
		search = Image.open("NEXT_official_logo.jpg")
		width_org, height_org = search.size
		search_temp = search.resize((int(width_org*factor),
							int(height_org*factor)), Image.ANTIALIAS)
		search_aux = ImageTk.PhotoImage(search_temp)
		label1 = Label(self, image=search_aux)
		label1.image = search_aux
		label1.grid(row=0, column=4,
				columnspan=2, rowspan=3, padx=5)


		#self.base_path.set("F:/DATOS_DAC/2052/pmt_0_trace_evt_")
		self.base_path.set("Argon.h5.z")
		e1 = Entry(self, textvariable=self.base_path, width=40)
		e1.grid(row=0,column=1, sticky=W, columnspan=3, pady=5, padx=5)
		e1_label = Label(self, text="Path & Name")
		e1_label.grid(row=0,column=0, columnspan=1, sticky=E, pady=5, padx=5)

		self.point.set("0")
		sb1 = Spinbox(self, from_=0, to=100,
				  width=4, textvariable=self.point)
		sb1.grid(row=2,column=1, sticky=W, pady=5, padx=5)
		sb1_label = Label(self, text="PMT")
		sb1_label.grid(row=2,column=0, padx=5, sticky=E)

		self.meas.set("0")
		sb1 = Spinbox(self, from_=0, to=100,
				  width=4, textvariable=self.meas)
		sb1.grid(row=2,column=3, sticky=W, pady=5, padx=5)
		sb1_label = Label(self, text="Event")
		sb1_label.grid(row=2,column=2, padx=5, sticky=E)

		#Check buttons
#		cb1 = Checkbutton(self, text="New Graph", variable=self.graph_sw)
#		cb1.select()
#		cb1.grid(row=2,column=2, sticky=W)


		#PARAMETERS
		Integration_label = Label(self, text="PARAMETERS",
		                          font = "Verdana 12 bold")
		Integration_label.grid(row=3,column=1,
						padx=5,
						columnspan = 2, pady=10, sticky=E)

		self.coef.set("1.65E-3")
		e2 = Entry(self, width=12, textvariable=self.coef)
		e2.grid(row=4,column=1, sticky=W, pady=5, padx=5)
		e2_label = Label(self, text="DBLR Coef")
		e2_label.grid(row=4,column=0, sticky=E, pady=5, padx=5)

		self.noise.set("0.75")
		e3 = Entry(self, width=12, textvariable=self.noise)
		e3.grid(row=4,column=3, sticky=W, pady=5, padx=5)
		e3_label = Label(self, text="Noise (LSB)")
		e3_label.grid(row=4,column=2, sticky=E, pady=5, padx=5)

		self.n_sigma.set("4")
		e4 = Entry(self, width=12, textvariable=self.n_sigma)
		e4.grid(row=4,column=5, sticky=W, pady=5, padx=5)
		e4_label = Label(self, text="Noise Threshold")
		e4_label.grid(row=4,column=4, sticky=E, pady=5, padx=5)

		self.thr1.set("0")
		e5 = Entry(self, width=12, textvariable=self.thr1)
		e5.grid(row=5,column=1, sticky=W, pady=5, padx=5)
		e5_label = Label(self, text="Threshold 1")
		e5_label.grid(row=5,column=0, sticky=E, pady=5, padx=5)

		self.thr2.set("0")
		e6 = Entry(self, width=12, textvariable=self.thr2)
		e6.grid(row=5,column=3, sticky=W, pady=5, padx=5)
		e6_label = Label(self, text="Threshold 2")
		e6_label.grid(row=5,column=2, sticky=E, pady=5, padx=5)

		self.thr3.set("0")
		e7 = Entry(self, width=12, textvariable=self.thr3)
		e7.grid(row=5,column=5, sticky=W, pady=5, padx=5)
		e7_label = Label(self, text="Threshold 3")
		e7_label.grid(row=5,column=4, sticky=E, pady=5, padx=5)





		# Main buttons
		obtn = Button(self, text="GO!!", command=self.DBLR_f)
		obtn.grid(row=6, column=4, sticky=E, pady=10)

		cbtn = Button(self, text="Quit", command=self.quit)
		cbtn.grid(row=6, column=5, sticky=E, pady=10)

		hbtn = Button(self, text="Help", command=self.help_f)
		hbtn.grid(row=6, column=0, sticky=W, pady=10)


	def help_f(self):
		top = Toplevel()
		top.title("HELP")
		msg = Message(top, width= 500,
                 text="Noise Threshold (NT) - Noise Based Main Threshold \
(Sigmas)\n Threshold 1 - Main Threshold (LSBs) \n Threshold 2 - End of Pulse Error \n \
Threshold 3 - End of Tail Error \n When Thr1 = Thr3 = 0 their values are defined as: \n \
(THR1 = NT (LSBs) / THR3 = NT*NOISE_ADC / 5)")
		msg.pack()

		button = Button(top, text="Close", command=top.destroy)
		button.pack()


	def DBLR_f(self):

		global a

		#if (self.graph_sw.get()==True):
		b=a
		a=a+1
		#else:
		#	b=0

		aux=self.base_path.get()
#		path=''.join([aux,str(self.meas.get()),'.txt'])
#		g=pd.read_csv(path)
		f=read_panel_hdf5(aux, self.point.get(), self.meas.get())
		f=4096-f

		recons,energia = DB.BLR( signal_daq=f.flatten().astype(float),
						  coef=self.coef.get(),
						  n_sigma=self.n_sigma.get(),
						  NOISE_ADC=self.noise.get(),
						  thr1=self.thr1.get(),
						  thr2=self.thr2.get(),
						  thr3=self.thr3.get(),
						  plot=False)
		plt.figure(a)
		plt.plot(recons)
		plt.figtext(0.2,0.75, ('ENERGY = %0.2f ' % (energia)))
		plt.show()


	def centerUI(self,w,h):
		sw = self.parent.winfo_screenwidth()
		sh = self.parent.winfo_screenheight()
		x  = (sw-w)/2
		y  = (sh-h)/2
		self.parent.geometry('%dx%d+%d+%d' % (w,h,x,y))
コード例 #40
0
class ChemicalDeleter:
    def __init__(self, master, system, chemical):
        """Constructor method. Defines the parameters to be obtained in this window."""

        self.master = master
        self.fonttype = system.fonttype
        self.version = system.version
        self.superfont = get_superfont(self.fonttype)  #superscript font
        self.frame = Frame(master.frame)
        self.tframe = Frame(master.tframe)
        self.bframe = Frame(master.bframe)
        self.top = None  #flag for existence of toplevel#

        self.soluableflag = chemical.soluable
        self.name = StringVar(value=chemical.name)
        self.MW = DoubleVar(value=chemical.MW)
        self.formula = StringVar(value=chemical.formula)
        self.temp = DoubleVar(value=chemical.temp)
        self.Dw = DoubleVar(value=chemical.Dw)
        self.Koc = DoubleVar(value=chemical.Koc)
        self.Kdoc = DoubleVar(value=chemical.Kdoc)
        self.Ref = StringVar(value=chemical.Ref)
        self.Kf = DoubleVar(value=chemical.Kf)
        self.N = DoubleVar(value=chemical.N)

        self.chemical = chemical

        self.cancelflag = 0

    def make_widgets(self):

        self.bgcolor = self.frame.cget('bg')
        self.instructions = Label(
            self.frame,
            text=
            ' Are you sure to delete the following chemical?                   '
        )

        self.namelabel = Label(self.frame, text='Chemical name')
        self.MWlabel = Label(self.frame, text='Molecular\n weight')
        self.templabel = Label(self.frame, text='Temperature')
        self.Dwlabel = Label(self.frame,
                             text='Molecular diffusivity\n in water')
        self.Koclabel = Label(self.frame,
                              text='Organic carbon\n partition coefficient')
        self.Kdoclabel = Label(
            self.frame,
            text='Dissolved organic carbon\n partition coefficient')
        self.Reflabel = Label(self.frame, text='Reference')

        self.tempunits = Label(self.frame, text=unichr(176) + 'C')
        self.Dwunits = Label(self.frame, text=u'cm\u00B2/s')
        self.Kocunits = Label(self.frame, text='log(L/kg)')
        self.Kdocunits = Label(self.frame, text='log(L/kg)')

        self.namewidget = Label(self.frame,
                                width=20,
                                justify='center',
                                textvariable=self.name)
        if self.soluableflag == 1:
            self.tempwidget = Label(self.frame,
                                    width=10,
                                    justify='center',
                                    textvariable=self.temp)
            self.MWwidget = Label(self.frame,
                                  width=16,
                                  justify='center',
                                  textvariable=self.MW)
            self.Dwwidget = Label(self.frame,
                                  width=16,
                                  justify='center',
                                  textvariable=self.Dw)
            self.Kocwidget = Label(self.frame,
                                   width=16,
                                   justify='center',
                                   textvariable=self.Koc)
            self.Kdocwidget = Label(self.frame,
                                    width=16,
                                    justify='center',
                                    textvariable=self.Kdoc)
            self.Refwidget = Label(self.frame,
                                   width=16,
                                   justify='center',
                                   textvariable=self.Ref)
        else:
            self.tempwidget = Label(self.frame,
                                    width=10,
                                    justify='center',
                                    text=' ')
            self.Dwwidget = Label(self.frame,
                                  width=16,
                                  justify='center',
                                  text='Not applicable')
            self.Kocwidget = Label(self.frame,
                                   width=16,
                                   justify='center',
                                   text='Not applicable')
            self.Kdocwidget = Label(self.frame,
                                    width=16,
                                    justify='center',
                                    text='Not applicable')

        self.insoluablestate = Label(self.frame,
                                     width=16,
                                     justify='center',
                                     text='Not applicable')

        #show the widgets on the grid
        self.blankcolumn = Label(self.frame, text=' ', width=2)
        self.namecolumn = Label(self.frame, text=' ', width=20)
        self.MWcolumn = Label(self.frame, text=' ', width=18)
        self.tempcolumn = Label(self.frame, text=' ', width=10)
        self.Dwcolumn = Label(self.frame, text=' ', width=18)
        self.Koccolumn = Label(self.frame, text=' ', width=18)
        self.Kdoccolumn = Label(self.frame, text=' ', width=18)
        self.Refcolumn = Label(self.frame, text=' ', width=18)

        self.deletebutton = Button(self.frame,
                                   text='Delete',
                                   width=20,
                                   command=self.Delete)
        self.cancelbutton = Button(self.frame,
                                   text='Cancel',
                                   width=20,
                                   command=self.Cancel)
        self.blank1 = Label(self.frame, text=' ')
        self.blank2 = Label(self.frame, text=' ')

        self.instructions.grid(row=0,
                               column=0,
                               columnspan=6,
                               padx=8,
                               sticky='W')

        self.blankcolumn.grid(row=1, column=0, sticky='WE', padx=1, pady=1)
        self.namecolumn.grid(row=1, column=1, sticky='WE', padx=1, pady=1)
        self.MWcolumn.grid(row=1, column=2, sticky='WE', padx=1, pady=1)
        self.tempcolumn.grid(row=1, column=3, sticky='WE', padx=1, pady=1)
        self.Dwcolumn.grid(row=1, column=4, sticky='WE', padx=1, pady=1)
        self.Koccolumn.grid(row=1, column=5, sticky='WE', padx=1, pady=1)
        self.Kdoccolumn.grid(row=1, column=6, sticky='WE', padx=1, pady=1)
        self.Refcolumn.grid(row=1, column=7, sticky='WE', padx=1, pady=1)

        self.namelabel.grid(row=2, column=1, sticky='WE', padx=4, pady=1)
        self.MWlabel.grid(row=2, column=2, sticky='WE', padx=1, pady=1)
        self.templabel.grid(row=2, column=3, sticky='WE', padx=1, pady=1)
        self.Dwlabel.grid(row=2, column=4, sticky='WE', padx=1, pady=1)
        self.Koclabel.grid(row=2, column=5, sticky='WE', padx=1, pady=1)
        self.Kdoclabel.grid(row=2, column=6, sticky='WE', padx=1, pady=1)
        self.Reflabel.grid(row=2, column=7, sticky='WE', padx=1, pady=1)

        self.tempunits.grid(row=3, column=3, sticky='WE', padx=1, pady=1)
        self.Dwunits.grid(row=3, column=4, sticky='WE', padx=1, pady=1)
        self.Kocunits.grid(row=3, column=5, sticky='WE', padx=1, pady=1)
        self.Kdocunits.grid(row=3, column=6, sticky='WE', padx=1, pady=1)

        self.namewidget.grid(row=4, column=1, padx=2, pady=1, sticky='WE')
        self.MWwidget.grid(row=4, column=2, padx=2, pady=1, sticky='WE')
        self.tempwidget.grid(row=4, column=3, padx=2, pady=1, sticky='WE')
        self.Dwwidget.grid(row=4, column=4, padx=2, pady=1)
        self.Kocwidget.grid(row=4, column=5, padx=2, pady=1)
        self.Kdocwidget.grid(row=4, column=6, padx=2, pady=1)
        self.Refwidget.grid(row=4, column=7, padx=2, pady=1)

        self.blank1.grid(row=5)
        self.deletebutton.grid(row=6, columnspan=11)
        self.cancelbutton.grid(row=7, columnspan=11)
        self.blank2.grid(row=8)
        self.deletebutton.bind('<Return>', self.Delete)
        self.focusbutton = self.deletebutton

    def Delete(self, event=None):
        """Finish and move on.  Checks that the number chemicals are less than the
        total number of chemicals in database."""

        if self.master.window.top is not None: self.master.open_toplevel()
        else: self.master.tk.quit()

    def Cancel(self):

        try:
            self.name.set(self.chemical.name)
            self.temp.set(self.chemical.temp)
            self.MW.set(self.chemical.MW)
            self.formula.set(self.chemical.formula)
            self.Dw.set(self.chemical.Dw)
            self.Koc.set(self.chemical.Koc)
            self.Kdoc.set(self.chemical.Kdoc)
            self.Ref.set(self.chemical.Ref)
            self.Kf.set(self.chemical.Kf)
            self.N.set(self.chemical.N)
        except:
            self.cancelflag = 1

        if self.master.window.top is not None: self.master.open_toplevel()
        else: self.master.tk.quit()
コード例 #41
0
class Example(Frame):

	def __init__(self, parent):
		Frame.__init__(self, parent)
		self.parent = parent
		self.initUI()
		self.centerUI(w=450,h=340)

	def initUI(self):

		self.parent.title("LINEARITY TEST FOR PMT BASES")
		self.pack(fill=BOTH, expand=True)

		self.columnconfigure(0, weight=1)
		#self.rowconfigure(0, weight=1)
		# weight attibute is used to make them growable

#		self.graph_cb   = BooleanVar()
		self.init_point = IntVar()
		self.base_path  = StringVar()
		self.end_point  = IntVar()
		self.step       = IntVar()
		self.n_meas     = IntVar()
		self.inc_point  = IntVar()
		self.coef       = DoubleVar()
		self.noise      = DoubleVar()
		self.thr_sigma  = DoubleVar()
		self.SPE_DAQ    =	 DoubleVar()	
		
		search = Image.open("next_logo.jpg")
		search_temp = search.resize((170, 200), Image.ANTIALIAS)
		search_aux = ImageTk.PhotoImage(search_temp)
		label1 = Label(self, image=search_aux)
		label1.image = search_aux
		label1.grid(row=0, column=0,
				columnspan=10, rowspan=10, sticky=E+W+S+N)

		#Text Box
		self.base_path.set("F:/DATOS_DAC/2052/pmt_0_trace_evt_")
		e1 = Entry(self, textvariable=self.base_path, width=40)
		e1.grid(row=1,column=2, sticky=W, columnspan=5, pady=5)
		e1_label = Label(self, text="DataSet path (including name file)")
		e1_label.grid(row=0,column=2,sticky=W, columnspan=5, pady=5)		
		
		#Spin Boxes
		self.n_meas.set("20")
		sb1 = Spinbox(self, from_=1, to=1000, 
				  width=6, textvariable=self.n_meas)
		sb1.grid(row=3,column=3, sticky=W)
		sb1_label = Label(self, text="Measurements")
		sb1_label.grid(row=2,column=3, padx=0, sticky=W)		
		
		self.step.set("10")
		sb2 = Spinbox(self, from_=10, to=200, 
				  width=6, textvariable=self.step)
		sb2.grid(row=3,column=4, sticky=W)
		sb2_label = Label(self, text="Pulse Width Step")
		sb2_label.grid(row=2,column=4, padx=0, sticky=W)
		
		# INTEGRATION LIMITS
		Integration_label = Label(self, text="INTEGRATION LIMITS",
		                          font = "Verdana 12 bold")
		Integration_label.grid(row=4,column=3, 
						padx=5,
						columnspan = 3, pady=10)
		self.init_point.set("30")
		sb3 = Spinbox(self, from_=1, to=10000, 
				  width=6, textvariable=self.init_point)
		sb3.grid(row=7,column=3, sticky=W)
		sb3_label = Label(self, text="Start (usec)")
		sb3_label.grid(row=6,column=3, padx=0, sticky=W)		
		
		self.end_point.set("160")
		sb4 = Spinbox(self, from_=1, to=10000, 
				  width=6, textvariable=self.end_point)
		sb4.grid(row=7,column=4, sticky=W)
		sb4_label = Label(self, text="End (usec)")
		sb4_label.grid(row=6,column=4, padx=0, sticky=W)

		
		# PARAMETERS
		Integration_label = Label(self, text="PARAMETERS",
		                          font = "Verdana 12 bold")
		Integration_label.grid(row=8,column=3, 
						padx=5,
						columnspan = 3, pady=10)
		self.inc_point.set("3")
		sb5 = Spinbox(self, from_=1, to=100, 
				  width=6, textvariable=self.inc_point)
		sb5.grid(row=11,column=3, sticky=W)
		sb5_label = Label(self, text="First point")
		sb5_label.grid(row=10,column=3, padx=0, sticky=W)
		
		self.coef.set("1.636E-3")
		e6 = Entry(self, width=10, textvariable=self.coef)
		e6.grid(row=11,column=4, sticky=W)
		e6_label = Label(self, text="DBLR Coef")
		e6_label.grid(row=10,column=4, sticky=W)
				
		self.noise.set("0.75")
		e7 = Entry(self, width=10, textvariable=self.noise)
		e7.grid(row=11,column=5, sticky=W)
		e7_label = Label(self, text="Noise (LSB)")
		e7_label.grid(row=10,column=5, sticky=W)

		self.thr_sigma.set("40")
		e8 = Entry(self, width=10, textvariable=self.thr_sigma)
		e8.grid(row=13,column=3, sticky=W)
		e8_label = Label(self, text="Threshold")
		e8_label.grid(row=12,column=3, sticky=W)
		
		self.SPE_DAQ.set("20.5")
		e9 = Entry(self, width=10, textvariable=self.SPE_DAQ)
		e9.grid(row=13,column=4, sticky=W)
		e9_label = Label(self, text="SPE (LSB)")
		e9_label.grid(row=12,column=4, sticky=W)		
		
		
#		#Check buttons
#		cb1 = Checkbutton(self, text="MultiGraph Output", variable=self.graph_cb					)
#		cb1.select()
#		cb1.grid(row=7,column=6, sticky=W)

		
		# Main buttons
		obtn = Button(self, text="GO!!", command=self.linearity_f)
		obtn.grid(row=14, column=4, sticky=E, pady=10)

		cbtn = Button(self, text="Quit", command=self.quit)
		cbtn.grid(row=14, column=5, sticky=E, pady=10)

		hbtn = Button(self, text="Help")
		hbtn.grid(row=14, column=0, sticky=W, pady=10)

	def linearity_f(self):
		
#		global a
#		
#		if (self.graph_cb.get()==True): 
#			b=a
#			a=a+1
#		else: 
#			b=0				
		
		linearity.linearity_test(base_path=self.base_path.get(),
				              init_point=self.init_point.get(), 
				              end_point=self.end_point.get(), 
				              step=self.step.get(), 
				              n_meas=self.n_meas.get(), 
				              inc_point=self.inc_point.get(), 
				              coef=self.coef.get(),
				              noise=self.noise.get(),
					        thr_sigma=self.thr_sigma.get(),
					        SPE_DAQ=self.SPE_DAQ.get(),
					        graph_sw=0
						)
		
		
	def centerUI(self,w,h):
		sw = self.parent.winfo_screenwidth()
		sh = self.parent.winfo_screenheight()
		x  = (sw-w)/2
		y  = (sh-h)/2
		self.parent.geometry('%dx%d+%d+%d' % (w,h,x,y))
コード例 #42
0
class SolidDatabaseEditor:
    """Gets the contaminant properties."""
    def __init__(self, master, system, solid, solid_database, editflag):
        """Constructor method.  Defines the parameters to be obtained in this
        window."""

        self.master = master
        self.fonttype = system.fonttype
        self.version = system.version
        self.superfont = get_superfont(self.fonttype)  #superscript font
        self.tframe = Frame(master.tframe)
        self.frame = Frame(master.frame)
        self.bframe = Frame(master.bframe)
        self.top = None  #flag for existence of toplevel#

        self.solid = solid
        self.solid_database = solid_database

        self.torts = ['Millington & Quirk', 'Boudreau', 'None']
        self.sorps = [
            'Linear--Kd specified', 'Linear--Kocfoc', 'Freundlich', 'Langmuir'
        ]

        self.name = StringVar(
            value='Solid ' +
            str(solid_database[-1].number))  #stores the chemical name
        self.e = DoubleVar(value=0.5)  #stores the porosity
        self.rho = DoubleVar(value=1.0)  #stores the bulk density
        self.foc = DoubleVar(value=0.01)  #stores the organic carbon fraction
        self.tort = StringVar(
            value=self.torts[0])  #stores the default tortuosity correction
        self.sorp = StringVar(
            value=self.sorps[0])  #stores the default sorption correction
        self.Ref = StringVar(value='')  #stores the density

        self.editflag = editflag
        self.cancelflag = 0

        if editflag == 1:  #Detemine whether the chemical is added or edited

            self.name.set(solid.name)
            self.e.set(solid.e)
            self.rho.set(solid.rho)
            self.foc.set(solid.foc)
            self.tort.set(solid.tort)
            self.sorp.set(solid.sorp)
            self.Ref.set(solid.Ref)

    def make_widgets(self):
        """Make the widgets for the window."""

        self.bgcolor = self.frame.cget('bg')
        self.instructions = Label(
            self.frame,
            text=
            ' Please provide the following properties for the solid/mixture:                    '
        )

        self.blankcolumn = Label(self.frame, text=' ', width=1)
        self.namecolumn = Label(self.frame, text=' ', width=18)
        self.ecolumn = Label(self.frame, text=' ', width=10)
        self.rhocolumn = Label(self.frame, text=' ', width=10)
        self.foccolumn = Label(self.frame, text=' ', width=10)
        self.tortcolumn = Label(self.frame, text=' ', width=18)
        self.sorpcolumn = Label(self.frame, text=' ', width=20)
        self.refcolumn = Label(self.frame, text=' ', width=18)
        self.endcolumn = Label(self.frame, text=' ', width=2)

        self.namelabel = Label(self.frame, text='Material')
        self.elabel = Label(self.frame, text='Porosity')
        self.rholabel = Label(self.frame, text='Bulk density')
        self.foclabel = Label(self.frame, text='Organic carbon fraction')
        self.tortlabel = Label(self.frame, text='Tortruosity correction')
        self.sorplabel = Label(self.frame, text='Sorption isotherms')
        self.reflabel = Label(self.frame, text='Reference')

        self.rhounitlabel = Label(self.frame, text=u'g/cm\u00B3')

        self.namewidget = Entry(self.frame,
                                width=16,
                                justify='center',
                                textvariable=self.name)
        self.ewidget = Entry(self.frame,
                             width=8,
                             justify='center',
                             textvariable=self.e)
        self.rhowidget = Entry(self.frame,
                               width=8,
                               justify='center',
                               textvariable=self.rho)
        self.focwidget = Entry(self.frame,
                               width=8,
                               justify='center',
                               textvariable=self.foc)
        self.tortwidget = OptionMenu(self.frame, self.tort, *self.torts)
        self.sorpwidget = OptionMenu(self.frame, self.sorp, *self.sorps)
        self.refwidget = Entry(self.frame,
                               width=15,
                               justify='center',
                               textvariable=self.Ref)

        self.okbutton = Button(self.frame,
                               text='OK',
                               width=20,
                               command=self.OK)
        self.cancelbutton = Button(self.frame,
                                   text='Cancel',
                                   width=20,
                                   command=self.Cancel)
        self.blank1 = Label(self.frame, text=' ')
        self.blank2 = Label(self.frame, text=' ')

        #show the widgets on the grid
        self.instructions.grid(row=0,
                               column=0,
                               columnspan=7,
                               padx=8,
                               sticky='W')

        self.blankcolumn.grid(row=1, column=0, sticky='WE', padx=1, pady=1)
        self.namecolumn.grid(row=1, column=1, sticky='WE', padx=1, pady=1)
        self.ecolumn.grid(row=1, column=2, sticky='WE', padx=1, pady=1)
        self.rhocolumn.grid(row=1, column=3, sticky='WE', padx=1, pady=1)
        self.foccolumn.grid(row=1, column=4, sticky='WE', padx=1, pady=1)
        self.tortcolumn.grid(row=1, column=5, sticky='WE', padx=1, pady=1)
        self.sorpcolumn.grid(row=1, column=6, sticky='WE', padx=1, pady=1)
        self.refcolumn.grid(row=1, column=7, sticky='WE', padx=1, pady=1)
        self.endcolumn.grid(row=1, column=8, sticky='WE', padx=1, pady=1)

        self.namelabel.grid(row=2, column=1, sticky='WE', padx=1, pady=1)
        self.elabel.grid(row=2, column=2, sticky='WE', padx=1, pady=1)
        self.rholabel.grid(row=2, column=3, sticky='WE', padx=1, pady=1)
        self.foclabel.grid(row=2, column=4, sticky='WE', padx=1, pady=1)
        self.tortlabel.grid(row=2, column=5, sticky='WE', padx=1, pady=1)
        self.sorplabel.grid(row=2, column=6, sticky='WE', padx=1, pady=1)
        self.reflabel.grid(row=2, column=7, sticky='WE', padx=1, pady=1)

        self.rhounitlabel.grid(row=3, column=3, sticky='WE', padx=1, pady=1)

        self.namewidget.grid(row=4, column=1, padx=1, pady=1)
        self.ewidget.grid(row=4, column=2, padx=1, pady=1)
        self.rhowidget.grid(row=4, column=3, padx=1, pady=1)
        self.focwidget.grid(row=4, column=4, padx=1, pady=1)
        self.tortwidget.grid(row=4, column=5, padx=1, pady=1, sticky='WE')
        self.sorpwidget.grid(row=4, column=6, padx=1, pady=1, sticky='WE')
        self.refwidget.grid(row=4, column=7, padx=1, pady=1)

        self.blank1.grid(row=5)
        self.okbutton.grid(row=6, columnspan=11)
        self.cancelbutton.grid(row=7, columnspan=11)
        self.blank2.grid(row=8)
        self.okbutton.bind('<Return>', self.OK)
        self.focusbutton = self.okbutton

    def OK(self, event=None):
        """Finish and move on.  Checks that the number chemicals are less than the
        total number of chemicals in database."""

        if self.editflag == 0:
            check = [(solid.name == self.name.get())
                     for solid in self.solid_database[0:-1]]
        if self.editflag == 1:
            check = [(solid.name == self.name.get()
                      and self.solid.name != self.name.get())
                     for solid in self.solid_database[0:-1]]

        if self.master.window.top is not None: self.master.open_toplevel()
        elif self.e.get() > 1 or self.e.get() < 0:
            tkmb.showinfo(
                self.version,
                'The porosity of a solid can not be larger than 1 or smaller than 0'
            )
            self.e.set(0.5)
        elif self.rho.get() < 0:
            tkmb.showinfo(self.version,
                          'The bulk density of a solid can not be negative')
            self.e.set(1.0)
        elif self.foc.get() > 1 or self.foc.get() < 0:
            tkmb.showinfo(
                self.version,
                'The organic carbon fraction of a solid can not be larger than 1 or smaller than 0'
            )
            self.e.set(1.0)
        elif sum(check) >= 1 or self.name.get() == '':
            self.solids_error()
        else:
            self.master.tk.quit()

    def solids_error(self):

        tkmb.showerror(
            title=self.version,
            message=
            'This solid material has already been added to the database!')
        self.focusbutton = self.okbutton
        self.master.tk.lift()

    def Cancel(self):

        try:
            self.name.set(self.solid.name)
            self.e.set(self.solid.e)
            self.rho.set(self.solid.rho)
            self.foc.set(self.solid.foc)
            self.tort.set(self.solid.tort)
            self.sorp.set(self.solid.sorp)
            self.Ref.set(self.solid.Ref)
        except:
            self.cancelflag = 1

        if self.master.window.top is not None: self.master.open_toplevel()
        else: self.master.tk.quit()
コード例 #43
0
class ChemicalImporter:
    def __init__(self, master, system, database):
        """The constructor method."""

        self.version = system.version
        self.fonttype = system.fonttype
        self.sfont = get_superfont(self.fonttype)
        self.master = master
        self.frame = Frame(master.frame)
        self.tframe = Frame(master.tframe)
        self.bframe = Frame(master.bframe)
        self.tkfont = tkFont.Font(font=system.fonttype)

        self.chemicals_list = database.keys()
        self.chemicals_list.sort()

        self.name = StringVar()
        self.formula = StringVar()
        self.MW = DoubleVar()
        self.temp = DoubleVar(value=0)
        self.Dw = DoubleVar(value=0)
        self.Ref = StringVar(value=0)
        self.Koc = DoubleVar(value=0)
        self.Kdoc = DoubleVar(value=0)
        self.Kf = DoubleVar(value=0)
        self.N = DoubleVar(value=0)

        self.importedchemicals = {}
        for name in self.chemicals_list:
            self.importedchemicals[name] = ChemicalData(name)
            self.importedchemicals[name].read_database(database[name])

        self.name_width = 10
        self.ref_width = 10

        for chemical_name in self.chemicals_list:
            if (self.tkfont.measure(chemical_name) + 10) > self.name_width:
                self.name_width = self.tkfont.measure(chemical_name) + 10
            for temp in self.importedchemicals[chemical_name].temps:
                if (self.tkfont.measure(
                        self.importedchemicals[chemical_name].Ref[temp]) +
                        10) > self.name_width:
                    self.name_width = self.tkfont.measure(
                        self.importedchemicals[chemical_name].Ref[temp]) + 10

        if self.name_width < 150: self.name_width = 150
        if self.ref_width < 150: self.ref_width = 150

        self.cancelflag = 0

        self.sname = StringVar(self.frame, value='')

    def make_widgets(self):

        self.instructions = Label(
            self.tframe,
            text='Please select the chemical you would like to add       ')

        self.leftcolumn = Label(self.tframe, text=' ', width=2)
        self.checkcolumn = Label(self.tframe, text=' ', width=5)
        self.orinamecolumn = Label(
            self.tframe,
            text=' ',
            width=int(self.name_width * 1.1424219345 / 8) + 1)
        self.tempcolumn = Label(self.tframe, text=' ', width=15)
        self.ref1column = Label(
            self.tframe,
            text=' ',
            width=int(self.ref_width * 1.1424219345 / 8 / 2) + 1)
        self.ref2column = Label(
            self.tframe,
            text=' ',
            width=int(self.ref_width * 1.1424219345 / 8 / 2) + 1)
        self.rightcolumn = Label(self.tframe, text=' ', width=2)

        self.search_label = Label(self.tframe, text='Search:')
        self.search_entry = Entry(self.tframe, textvariable=self.sname)

        self.namelabel = Label(self.tframe, text='Name')
        self.templabel = Label(self.tframe, text='Temperature')
        self.reflabel = Label(self.tframe, text='Reference')

        self.botleftcolumn = Label(self.frame, text=' ', width=2)
        self.botcheckcolumn = Label(self.frame, text=' ', width=5)
        self.botorinamecolumn = Label(
            self.frame,
            text=' ',
            width=int(self.name_width * 1.1424219345 / 8) + 1)
        self.bottempcolumn = Label(self.frame, text=' ', width=15)
        self.botref1column = Label(
            self.frame,
            text=' ',
            width=int(self.ref_width * 1.1424219345 / 8 / 2) + 1)
        self.botref2column = Label(
            self.frame,
            text=' ',
            width=int(self.ref_width * 1.1424219345 / 8 / 2) + 1)
        self.botrightcolumn = Label(self.frame, text=' ', width=2)

        self.importbutton = Button(self.bframe,
                                   text='Import',
                                   command=self.OK,
                                   width=20)
        self.cancelbutton = Button(self.bframe,
                                   text='Cancel',
                                   command=self.cancel,
                                   width=20)

        self.blank1 = Label(self.tframe, text=' ')
        self.blank2 = Label(self.frame, text=' ')
        self.blank3 = Label(self.bframe, text=' ')
        self.blank4 = Label(self.bframe, text=' ')

        #show the widgets on the grid (top to bottom and left to right)

        self.instructions.grid(row=0, columnspan=5, sticky='W', padx=8)

        self.leftcolumn.grid(row=1, column=0, sticky='WE')
        self.checkcolumn.grid(row=1, column=1, sticky='WE')
        self.orinamecolumn.grid(row=1, column=2, sticky='WE')
        self.tempcolumn.grid(row=1, column=3, sticky='WE')
        self.ref1column.grid(row=1, column=4, sticky='WE')
        self.ref2column.grid(row=1, column=5, sticky='WE')
        self.rightcolumn.grid(row=1, column=6, sticky='WE')

        self.search_label.grid(row=2, column=1, sticky='E', padx=4)
        self.search_entry.grid(row=2,
                               column=2,
                               columnspan=4,
                               sticky='WE',
                               padx=4)

        self.blank1.grid(row=3)

        self.namelabel.grid(row=4, column=2, sticky='WE')
        self.templabel.grid(row=4, column=3, sticky='WE')
        self.reflabel.grid(row=4, column=4, columnspan=2, sticky='WE')

        self.botleftcolumn.grid(row=1, column=0, sticky='WE')
        self.botcheckcolumn.grid(row=1, column=1, sticky='WE')
        self.botorinamecolumn.grid(row=1, column=2, sticky='WE')
        self.bottempcolumn.grid(row=1, column=3, sticky='WE')
        self.botref1column.grid(row=1, column=4, sticky='WE')
        self.botref2column.grid(row=1, column=5, sticky='WE')
        self.botrightcolumn.grid(row=1, column=6, sticky='WE')

        self.searchname()

        self.sname.trace('w', self.searchname)

    def searchname(self, event=None, *args):

        row = 2

        for name in self.chemicals_list:
            try:
                self.importedchemicals[name].remove_selectchemicalwidgets()
            except:
                pass

        if self.sname.get() == '':
            for name in self.chemicals_list:
                self.importedchemicals[name].selectchemicalwidgets(
                    self.frame,
                    row=row,
                    master=self.master,
                    namewidth=int(self.name_width * 1.1424219345 / 8) + 1,
                    refwidth=int(self.ref_width * 1.1424219345 / 8) + 1)
                row = row + 1
        else:
            for name in self.chemicals_list:
                if name.lower()[:len(self.sname.get())].count(
                        self.sname.get().lower()) >= 1:
                    self.importedchemicals[name].selectchemicalwidgets(
                        self.frame,
                        row=row,
                        master=self.master,
                        namewidth=int(self.name_width * 1.1424219345 / 8) + 1,
                        refwidth=int(self.ref_width * 1.1424219345 / 8) + 1)
                    row = row + 1
                else:
                    self.importedchemicals[name].check = IntVar(
                        value=self.importedchemicals[name].check)

        self.blank2.grid(row=row)

        row = 2

        self.blank3.grid(row=row)
        row = row + 1
        self.importbutton.grid(row=row, column=0, columnspan=5, pady=1)
        row = row + 1
        self.cancelbutton.grid(row=row, column=0, columnspan=5, pady=1)
        row = row + 1
        self.blank4.grid(row=row)

        self.cancelbutton.bind('<Return>', self.cancel)

        self.focusbutton = self.cancelbutton

    def selectchemicaldata(self, name):

        for othername in self.chemicals_list:
            if othername <> name:
                try:
                    self.importedchemicals[othername].check.set(0)
                except:
                    pass

    def cancelname(self, event=None):

        self.sname.set('')
        self.searchname()

    def OK(self, event=None):

        for name in self.chemicals_list:
            if self.importedchemicals[name].check.get() == 1:

                self.name.set(name)
                self.formula.set(self.importedchemicals[name].formula)
                self.MW.set(self.importedchemicals[name].MW)
                self.temp.set(self.importedchemicals[name].temp.get())
                self.Dw.set(self.importedchemicals[name].Dw[self.temp.get()])
                self.Ref.set(self.importedchemicals[name].Ref[self.temp.get()])
                self.Koc.set(self.importedchemicals[name].Koc[self.temp.get()])
                self.Kdoc.set(
                    self.importedchemicals[name].Kdoc[self.temp.get()])
                self.Kf.set(self.importedchemicals[name].Kf[self.temp.get()])
                self.N.set(self.importedchemicals[name].N[self.temp.get()])

        for name in self.chemicals_list:
            try:
                self.importedchemicals[name].remove_selectchemicalwidgets()
            except:
                pass

        self.frame.quit()

    def cancel(self, event=None):

        self.cancelflag = 1
        self.frame.quit()
コード例 #44
0
ファイル: number_entry.py プロジェクト: sp3ru/wt_tmp
class NumberEntry(BaseWiget):
	def __init__(self,  parent, def_dict=None, tracker_path=None, to_game_callback=None, **kw):
		BaseWiget.__init__(self,parent, def_dict=def_dict, tracker_path=tracker_path, to_game_callback=to_game_callback, **kw)
		self.old_value = 0
		outType = def_dict.get("outType", float)
		if outType == int:
			integer = True
		elif outType == float:
			integer = False
		else:
			raise TypeError("outType = %s"%repr(outType))

		minVal = def_dict.get("minVal", 0)	
		maxVal = def_dict.get("maxVal", 100)	

		step = 1
		if integer:
			self.var = IntVar()
		else:
			self.var = DoubleVar()
			step = 1.0
			if (maxVal-minVal) < 5:
				step = 0.01

		if "step" in def_dict:
			step = def_dict["step"]
	
		self.entry = Control(self, label=self.caption, integer=integer,
						variable=self.var, min=minVal, max=maxVal, step = step,
						options='entry.width 20 label.width 1 label.anchor w entry.anchor w')

		if not integer:
			number_zero = len(str(step).split(".")[1])
			foramter = "%.{0}f".format(number_zero)
			def foo(num):
				num = float(num)
				if num>maxVal:num = maxVal
				if num<minVal:num = minVal
				return  foramter%num
			self.entry['validatecmd'] = foo

		self.entry.grid(row=1, column=1) 
		self.var.trace("w", self._onChangeVariable)
		self.old_value = self.getValue()
		self.onEndSetupContent()



	#------------- интерфейс -------------#
	def setValue(self, value):
		if self.old_value  == value:
			return

		self.old_value  = value
		self.var.set(value)


	def getValue(self):
		return self.var.get()

	def getDictValue(self):
		raise NotImplementedError

	def forceSave(self):
		"""принудительное сохранение"""
		self._onEndInput()



	#------------- внутренние методы -------------#	

	def _onEndInput(self):
		value = self.getValue()
		if self.old_value != value:
			self.onValueChangeEndByUser(self.old_value, value)
			self.old_value = value


	def _onChangeVariable(self, *event):
		value = self.getValue()
		if self.old_value == value:
			return
		self._onEndInput()
コード例 #45
0
class CreateStarDlg(SKModal):

	title = _("Create Star")

	def __init__(self, master, **kw):
		# This constructor is here just for illustration purposes; it's
		# not really needed here, as it simply passes all parameters on
		# to the base class' constructor.
		#
		# The parameter master is the window this dialog belongs to. It
		# should normally be the top-level application window.
		apply(SKModal.__init__, (self, master), kw)

	def build_dlg(self):
		# The SKModal constructor automatically calls this method to
		# create the widgets in the dialog.
		#
		# self.top is the top-level window of the dialog. All widgets of
		# the dialog must contained in it.

		top = self.top

		# The rest is normal Tkinter code.

		self.var_corners = IntVar(top)
		self.var_corners.set(5)
		label = Label(top, text = _("Corners"), anchor = 'e')
		label.grid(column = 0, row = 0, sticky = 'ew')
		entry = Entry(top, textvariable = self.var_corners, width = 15)
		entry.grid(column = 1, row = 0, sticky = 'ew')
		
		self.var_steps = IntVar(top)
		self.var_steps.set(2)
		label = Label(top, text = _("Steps"), anchor = 'e')
		label.grid(column = 0, row = 1, sticky = 'ew')
		entry = Entry(top, textvariable = self.var_steps, width = 15)
		entry.grid(column = 1, row = 1, sticky = 'ew')

		self.var_radius = DoubleVar(top)
		self.var_radius.set(100)
		label = Label(top, text = _("Radius"), anchor = 'e')
		label.grid(column = 0, row = 2, sticky = 'ew')
		entry = Entry(top, textvariable = self.var_radius, width = 15)
		entry.grid(column = 1, row = 2, sticky = 'ew')
		

		but_frame = Frame(top)
		but_frame.grid(column = 0, row = 3, columnspan = 2)

		button = Button(but_frame, text = _("OK"), command = self.ok)
		button.pack(side = 'left', expand = 1)
		# The self.cancel method is provided by the base class and
		# cancels the dialog.
		button = Button(but_frame, text = _("Cancel"), command = self.cancel)
		button.pack(side = 'right', expand = 1)


	def ok(self, *args):
		# This method is bound to the OK-button. Its purpose is to
		# collect the values of the various edit fields and pass them as
		# one parameter to the close_dlg method.
		#
		# close_dlg() saves its parameter and closes the dialog.
		corners = self.var_corners.get()
		steps = self.var_steps.get()
		radius = self.var_radius.get()
		self.close_dlg((corners, steps, radius))
コード例 #46
0
ファイル: skew_plugin.py プロジェクト: sk1project/sk1-tk
class SkewPanel(PluginPanel):
    name = "Skew"
    title = _("Skew")

    def init(self, master):
        PluginPanel.init(self, master)

        self.width_priority = 1

        root = self.mw.root

        self.var_angleX = DoubleVar(root)
        self.var_angleY = DoubleVar(root)

        jump = 5
        self.var_angleX.set(0)
        self.var_angleY.set(0)

        self.var_proportional = IntVar(root)
        self.var_proportional.set(0)

        self.var_basepoint = StringVar(root)
        self.var_basepoint.set("C")

        # ---------------------------------------------------------
        top = TFrame(self.panel, style="FlatFrame")
        top.pack(side=TOP, fill=BOTH)
        # ---------------------------------------------------------
        # Horisontal
        size_frameH = TFrame(top, style="FlatFrame", borderwidth=3)
        size_frameH.pack(side=TOP, fill=BOTH)

        label = TLabel(size_frameH, style="FlatLabel", image="skew_h")
        label.pack(side=LEFT, padx=5)
        self.entry_angleX = TSpinbox(
            size_frameH,
            var=0,
            vartype=1,
            textvariable=self.var_angleX,
            min=-75,
            max=75,
            step=jump,
            width=10,
            command=self.apply_skew,
        )
        self.entry_angleX.pack(side=LEFT)

        self.labelwunit = TLabel(size_frameH, style="FlatLabel", text=_("deg"))
        self.labelwunit.pack(side=LEFT, padx=5)
        # ---------------------------------------------------------
        # Vertical

        size_frameV = TFrame(top, style="FlatFrame", borderwidth=3)
        size_frameV.pack(side=TOP, fill=BOTH)
        label = TLabel(size_frameV, style="FlatLabel", image="skew_v")
        label.pack(side=LEFT, padx=5)

        self.entry_angleY = TSpinbox(
            size_frameV,
            var=0,
            vartype=1,
            textvariable=self.var_angleY,
            min=-75,
            max=75,
            step=jump,
            width=10,
            command=self.apply_skew,
        )
        self.entry_angleY.pack(side=LEFT)

        self.labelhunit = TLabel(size_frameV, style="FlatLabel", text=_("deg"))
        self.labelhunit.pack(side=LEFT, padx=5)

        # ---------------------------------------------------------
        # Basepoint check
        label = TLabel(top, style="FlatLabel", text=_("Basepoint:"))
        label.pack(side=TOP, fill=BOTH, padx=5)
        basepoint_frame = TLabelframe(top, labelwidget=label, style="Labelframe", borderwidth=4)
        basepoint_frame.pack(side=TOP, fill=X, padx=5, pady=2)

        self.Basepoint = BasePointSelector(basepoint_frame, anchor=self.var_basepoint)
        self.Basepoint.pack(side=LEFT, fill=BOTH, padx=5)

        label = TLabel(basepoint_frame, style="FlatLabel", image="coordinate_deg")
        label.pack(side=LEFT, fill=BOTH, padx=10)

        # ---------------------------------------------------------
        # Button frame

        button_frame = TFrame(top, style="FlatFrame", borderwidth=5)
        button_frame.pack(side=BOTTOM, fill=BOTH)

        self.update_buttons = []
        self.button = UpdatedButton(top, text=_("Apply"), command=self.apply_skew)
        self.button.pack(in_=button_frame, side=BOTTOM, expand=1, fill=X, pady=3)

        self.button_copy = UpdatedButton(top, text=_("Apply to Copy"), command=self.apply_to_copy)
        self.button_copy.pack(in_=button_frame, side=BOTTOM, expand=1, fill=X)

        self.init_from_doc()
        self.subscribe_receivers()

    ###############################################################################

    def subscribe_receivers(self):
        self.document.Subscribe(SELECTION, self.Update)

    def unsubscribe_receivers(self):
        self.document.Unsubscribe(SELECTION, self.Update)

    def init_from_doc(self):
        self.Update()

    def Update(self, *arg):
        if self.is_selection():
            self.entry_angleX.set_state(NORMAL)
            self.entry_angleY.set_state(NORMAL)
            self.button["state"] = NORMAL
            self.button_copy["state"] = NORMAL
        else:
            self.entry_angleX.set_state(DISABLED)
            self.entry_angleY.set_state(DISABLED)
            self.button["state"] = DISABLED
            self.button_copy["state"] = DISABLED

    def SkewSelected(self, axisX=0, axisY=0):
        if self.document.selection:
            self.document.begin_transaction()
            try:
                try:
                    br = self.document.selection.coord_rect
                    hor_sel = br.right - br.left
                    ver_sel = br.top - br.bottom

                    cnt_x, cnt_y = self.Basepoint.get_basepoint(hor_sel, ver_sel, br.left, br.bottom)

                    text = _("Skew")
                    ax, ay = tan(axisX), tan(axisY)
                    sx = 1.0
                    sy = 1.0 - (ax * ay)
                    tx = cnt_x * ax
                    ty = cnt_y * ax * ay - cnt_y * ay
                    # Move the selection in the coordinates x0 y0
                    trafo = Trafo(1, 0, 0, 1, -cnt_x, -cnt_y)
                    # Skew and Scaling
                    trafo = Trafo(sx, ay, -ax, sy, 0, 0)(trafo)
                    # Move the selection in the coordinates basepoint
                    trafo = Trafo(1, 0, 0, 1, cnt_x, cnt_y)(trafo)
                    self.document.TransformSelected(trafo, text)
                except:
                    self.document.abort_transaction()
            finally:
                self.document.end_transaction()

    def apply_skew(self, *arg):
        if self.button["state"] == DISABLED:
            return
        try:
            angleX = self.var_angleX.get() * degrees
            angleY = self.var_angleY.get() * degrees
            self.SkewSelected(angleX, angleY)
        except:
            return

    def apply_to_copy(self):
        if self.button["state"] == DISABLED:
            return
        self.document.begin_transaction(_("Skew&Copy"))
        try:
            try:
                self.document.ApplyToDuplicate()
                self.apply_skew()
            except:
                self.document.abort_transaction()
        finally:
            self.document.end_transaction()

    def is_selection(self):
        return len(self.document.selection) > 0
コード例 #47
0
class SolidDatabaseDeleter:
    def __init__(self, master, system, solid):
        """Constructor method.  Defines the parameters to be obtained in this
        window."""

        self.master = master
        self.fonttype = system.fonttype
        self.version = system.version
        self.superfont = get_superfont(self.fonttype)  #superscript font
        self.tframe = Frame(master.tframe)
        self.frame = Frame(master.frame)
        self.bframe = Frame(master.bframe)
        self.top = None  #flag for existence of toplevel#

        self.name = StringVar(value=solid.name)  #stores the chemical name
        self.e = DoubleVar(value=solid.e)  #stores the porosity
        self.rho = DoubleVar(value=solid.rho)  #stores the bulk density
        self.foc = DoubleVar(
            value=solid.foc)  #stores the organic carbon fraction
        self.tort = StringVar(
            value=solid.tort)  #stores the default tortuosity correction
        self.sorp = StringVar(
            value=solid.sorp)  #stores the default sorption correction
        self.Ref = StringVar(
            value=solid.Ref)  #stores the default sorption correction

        self.cancelflag = 0

    def make_widgets(self):

        self.bgcolor = self.frame.cget('bg')
        self.instructions = Label(
            self.frame,
            text=
            ' Are you sure to delete the following solid from database?        '
        )

        self.namelabel = Label(self.frame, text='Material')
        self.elabel = Label(self.frame, text='Porosity')
        self.rholabel = Label(self.frame, text='Bulk density\n (kg/L')
        self.foclabel = Label(self.frame, text='Organic carbon\n fraction')
        self.tortlabel = Label(self.frame, text='Tortruosity correction')
        self.sorplabel = Label(self.frame, text='Sorption isotherms')
        self.Reflabel = Label(self.frame, text='Reference')

        self.rhounitlabel = Label(self.frame, text=u'g/cm\u00B3')

        self.namewidget = Label(self.frame,
                                width=16,
                                justify='center',
                                textvariable=self.name)
        self.ewidget = Label(self.frame,
                             width=8,
                             justify='center',
                             textvariable=self.e)
        self.rhowidget = Label(self.frame,
                               width=8,
                               justify='center',
                               textvariable=self.rho)
        self.focwidget = Label(self.frame,
                               width=8,
                               justify='center',
                               textvariable=self.foc)
        self.tortwidget = Label(self.frame,
                                width=16,
                                justify='center',
                                textvariable=self.tort)
        self.sorpwidget = Label(self.frame,
                                width=16,
                                justify='center',
                                textvariable=self.sorp)
        self.Refwidget = Label(self.frame,
                               width=10,
                               justify='center',
                               textvariable=self.Ref)

        self.blankcolumn = Label(self.frame, text=' ', width=1)
        self.namecolumn = Label(self.frame, text=' ', width=18)
        self.ecolumn = Label(self.frame, text=' ', width=10)
        self.rhocolumn = Label(self.frame, text=' ', width=10)
        self.foccolumn = Label(self.frame, text=' ', width=10)
        self.tortcolumn = Label(self.frame, text=' ', width=18)
        self.sorpcolumn = Label(self.frame, text=' ', width=18)
        self.Refcolumn = Label(self.frame, text=' ', width=18)
        self.endcolumn = Label(self.frame, text=' ', width=2)

        self.deletebutton = Button(self.frame,
                                   text='Delete',
                                   width=20,
                                   command=self.Delete)
        self.cancelbutton = Button(self.frame,
                                   text='Cancel',
                                   width=20,
                                   command=self.Cancel)
        self.blank1 = Label(self.frame, text=' ')
        self.blank2 = Label(self.frame, text=' ')

        #show the widgets on the grid
        self.instructions.grid(row=0,
                               column=0,
                               columnspan=6,
                               padx=8,
                               sticky='W')

        self.blankcolumn.grid(row=1, column=0, sticky='WE', padx=1, pady=1)
        self.namecolumn.grid(row=1, column=1, sticky='WE', padx=1, pady=1)
        self.ecolumn.grid(row=1, column=2, sticky='WE', padx=1, pady=1)
        self.rhocolumn.grid(row=1, column=3, sticky='WE', padx=1, pady=1)
        self.foccolumn.grid(row=1, column=4, sticky='WE', padx=1, pady=1)
        self.tortcolumn.grid(row=1, column=5, sticky='WE', padx=1, pady=1)
        self.sorpcolumn.grid(row=1, column=6, sticky='WE', padx=1, pady=1)
        self.Refcolumn.grid(row=1, column=7, sticky='WE', padx=1, pady=1)
        self.endcolumn.grid(row=1, column=8, sticky='WE', padx=1, pady=1)

        self.namelabel.grid(row=2, column=1, sticky='WE', padx=1, pady=1)
        self.elabel.grid(row=2, column=2, sticky='WE', padx=1, pady=1)
        self.rholabel.grid(row=2, column=3, sticky='WE', padx=1, pady=1)
        self.foclabel.grid(row=2, column=4, sticky='WE', padx=1, pady=1)
        self.tortlabel.grid(row=2, column=5, sticky='WE', padx=1, pady=1)
        self.sorplabel.grid(row=2, column=6, sticky='WE', padx=1, pady=1)
        self.Reflabel.grid(row=2, column=7, sticky='WE', padx=1, pady=1)

        self.rhounitlabel.grid(row=3, column=3, sticky='WE', padx=1, pady=1)

        self.namewidget.grid(row=4, column=1, padx=1, pady=1, sticky='WE')
        self.ewidget.grid(row=4, column=2, padx=1, pady=1)
        self.rhowidget.grid(row=4, column=3, padx=1, pady=1)
        self.focwidget.grid(row=4, column=4, padx=1, pady=1)
        self.tortwidget.grid(row=4, column=5, padx=1, pady=1)
        self.sorpwidget.grid(row=4, column=6, padx=1, pady=1)
        self.Refwidget.grid(row=4, column=7, padx=1, pady=1)

        self.blank1.grid(row=5)
        self.deletebutton.grid(row=6, columnspan=11)
        self.cancelbutton.grid(row=7, columnspan=11)
        self.blank2.grid(row=8)
        self.deletebutton.bind('<Return>', self.Delete)
        self.focusbutton = self.deletebutton

    def Delete(self, event=None):
        """Finish and move on.  Checks that the number chemicals are less than the
        total number of chemicals in database."""

        if self.master.window.top is not None: self.master.open_toplevel()
        else: self.master.tk.quit()

    def Cancel(self):

        try:
            self.name.set(self.solid.name)
            self.e.set(self.solid.e)
            self.rho.set(self.solid.rho)
            self.foc.set(self.solid.foc)
            self.tort.set(self.solid.tort)
            self.sorp.set(self.solid.sorp)
            self.Ref.set(self.solid.Ref)
        except:
            self.cancelflag = 1

        if self.master.window.top is not None: self.master.open_toplevel()
        else: self.master.tk.quit()
コード例 #48
0
class SkewPanel(PluginPanel):
    name = 'Skew'
    title = _("Skew")

    def init(self, master):
        PluginPanel.init(self, master)

        self.width_priority = 1

        root = self.mw.root

        self.var_angleX = DoubleVar(root)
        self.var_angleY = DoubleVar(root)

        jump = 5
        self.var_angleX.set(0)
        self.var_angleY.set(0)

        self.var_proportional = IntVar(root)
        self.var_proportional.set(0)

        self.var_basepoint = StringVar(root)
        self.var_basepoint.set('C')

        #---------------------------------------------------------
        top = TFrame(self.panel, style='FlatFrame')
        top.pack(side=TOP, fill=BOTH)
        #---------------------------------------------------------
        # Horisontal
        size_frameH = TFrame(top, style='FlatFrame', borderwidth=3)
        size_frameH.pack(side=TOP, fill=BOTH)

        label = TLabel(size_frameH, style='FlatLabel', image='skew_h')
        label.pack(side=LEFT, padx=5)
        self.entry_angleX = TSpinbox(size_frameH,
                                     var=0,
                                     vartype=1,
                                     textvariable=self.var_angleX,
                                     min=-75,
                                     max=75,
                                     step=jump,
                                     width=10,
                                     command=self.apply_skew)
        self.entry_angleX.pack(side=LEFT)

        self.labelwunit = TLabel(size_frameH, style='FlatLabel', text=_("deg"))
        self.labelwunit.pack(side=LEFT, padx=5)
        #---------------------------------------------------------
        # Vertical

        size_frameV = TFrame(top, style='FlatFrame', borderwidth=3)
        size_frameV.pack(side=TOP, fill=BOTH)
        label = TLabel(size_frameV, style='FlatLabel', image='skew_v')
        label.pack(side=LEFT, padx=5)

        self.entry_angleY = TSpinbox(size_frameV,
                                     var=0,
                                     vartype=1,
                                     textvariable=self.var_angleY,
                                     min=-75,
                                     max=75,
                                     step=jump,
                                     width=10,
                                     command=self.apply_skew)
        self.entry_angleY.pack(side=LEFT)

        self.labelhunit = TLabel(size_frameV, style='FlatLabel', text=_("deg"))
        self.labelhunit.pack(side=LEFT, padx=5)

        #---------------------------------------------------------
        # Basepoint check
        label = TLabel(top, style='FlatLabel', text=_("Basepoint:"))
        label.pack(side=TOP, fill=BOTH, padx=5)
        basepoint_frame = TLabelframe(top,
                                      labelwidget=label,
                                      style='Labelframe',
                                      borderwidth=4)
        basepoint_frame.pack(side=TOP, fill=X, padx=5, pady=2)

        self.Basepoint = BasePointSelector(basepoint_frame,
                                           anchor=self.var_basepoint)
        self.Basepoint.pack(side=LEFT, fill=BOTH, padx=5)

        label = TLabel(basepoint_frame,
                       style='FlatLabel',
                       image='coordinate_deg')
        label.pack(side=LEFT, fill=BOTH, padx=10)

        #---------------------------------------------------------
        # Button frame

        button_frame = TFrame(top, style='FlatFrame', borderwidth=5)
        button_frame.pack(side=BOTTOM, fill=BOTH)

        self.update_buttons = []
        self.button = UpdatedButton(top,
                                    text=_("Apply"),
                                    command=self.apply_skew)
        self.button.pack(in_=button_frame,
                         side=BOTTOM,
                         expand=1,
                         fill=X,
                         pady=3)

        self.button_copy = UpdatedButton(top,
                                         text=_("Apply to Copy"),
                                         command=self.apply_to_copy)
        self.button_copy.pack(in_=button_frame, side=BOTTOM, expand=1, fill=X)

        self.init_from_doc()
        self.subscribe_receivers()


###############################################################################

    def subscribe_receivers(self):
        self.document.Subscribe(SELECTION, self.Update)

    def unsubscribe_receivers(self):
        self.document.Unsubscribe(SELECTION, self.Update)

    def init_from_doc(self):
        self.Update()

    def Update(self, *arg):
        if self.is_selection():
            self.entry_angleX.set_state(NORMAL)
            self.entry_angleY.set_state(NORMAL)
            self.button['state'] = NORMAL
            self.button_copy['state'] = NORMAL
        else:
            self.entry_angleX.set_state(DISABLED)
            self.entry_angleY.set_state(DISABLED)
            self.button['state'] = DISABLED
            self.button_copy['state'] = DISABLED

    def SkewSelected(self, axisX=0, axisY=0):
        if self.document.selection:
            self.document.begin_transaction()
            try:
                try:
                    br = self.document.selection.coord_rect
                    hor_sel = br.right - br.left
                    ver_sel = br.top - br.bottom

                    cnt_x, cnt_y = self.Basepoint.get_basepoint(
                        hor_sel, ver_sel, br.left, br.bottom)

                    text = _("Skew")
                    ax, ay = tan(axisX), tan(axisY)
                    sx = 1.0
                    sy = 1.0 - (ax * ay)
                    tx = cnt_x * ax
                    ty = cnt_y * ax * ay - cnt_y * ay
                    # Move the selection in the coordinates x0 y0
                    trafo = Trafo(1, 0, 0, 1, -cnt_x, -cnt_y)
                    # Skew and Scaling
                    trafo = Trafo(sx, ay, -ax, sy, 0, 0)(trafo)
                    # Move the selection in the coordinates basepoint
                    trafo = Trafo(1, 0, 0, 1, cnt_x, cnt_y)(trafo)
                    self.document.TransformSelected(trafo, text)
                except:
                    self.document.abort_transaction()
            finally:
                self.document.end_transaction()

    def apply_skew(self, *arg):
        if self.button["state"] == DISABLED:
            return
        try:
            angleX = self.var_angleX.get() * degrees
            angleY = self.var_angleY.get() * degrees
            self.SkewSelected(angleX, angleY)
        except:
            return

    def apply_to_copy(self):
        if self.button["state"] == DISABLED:
            return
        self.document.begin_transaction(_("Skew&Copy"))
        try:
            try:
                self.document.ApplyToDuplicate()
                self.apply_skew()
            except:
                self.document.abort_transaction()
        finally:
            self.document.end_transaction()

    def is_selection(self):
        return (len(self.document.selection) > 0)
コード例 #49
0
class ConfigClass:
    def __init__(self,textbox,FOLDER,APPNAME):
        self.folder=os.path.join(FOLDER,'config')
        self.appname=APPNAME
        # Das Standard App Verzeichniss fuer das Betriebssystem abfragen
        self.make_settings_folder()

        # eine ConfigParser Instanz oeffnen und evt. vorhandenes Config File Laden        
        self.parser = ConfigParser.ConfigParser()
        self.cfg_file_name=self.appname+'_config.cfg'
        self.parser.read(os.path.join(self.folder,self.cfg_file_name))

        # Falls kein Config File vorhanden ist oder File leer ist neue File anlegen und neu laden
        if len(self.parser.sections())==0:
            self.make_new_Config_file()
            self.parser.read(os.path.join(self.folder,self.cfg_file_name))
            textbox.prt((_('\nNo config file found generated new on at: %s') \
                             %os.path.join(self.folder,self.cfg_file_name)))
        else:
            textbox.prt((_('\nLoading config file:%s') \
                             %os.path.join(self.folder,self.cfg_file_name)))

        #Tkinter Variablen erstellen zur späteren Verwendung in den Eingabefeldern        
        self.get_all_vars()

        #DEBUG INFORMATIONEN
        #Übergeben des geladenen Debug Level
        textbox.set_debuglevel(DEBUG=self.debug)
        textbox.prt(_('\nDebug Level: %i') %(self.debug),1)
        textbox.prt(str(self),1)

    def make_settings_folder(self): 
        # create settings folder if necessary 
        try: 
            os.mkdir(self.folder) 
        except OSError: 
            pass 

    def make_new_Config_file(self):
        
        #Generelle Einstellungen für Export
        self.parser.add_section('General')
        self.parser.set('General', 'write_to_stdout', 0)
        
        self.parser.add_section('Paths') 
        self.parser.set('Paths', 'load_path', 'C:\Users\Christian Kohloeffel\Documents\DXF2GCODE\trunk\dxf')
        self.parser.set('Paths', 'save_path', 'C:\Users\Christian Kohloeffel\Documents')

        self.parser.add_section('Import Parameters') 
        self.parser.set('Import Parameters', 'point_tolerance', 0.01)
        self.parser.set('Import Parameters', 'fitting_tolerance', 0.01)   
        self.parser.set('Import Parameters', 'spline_check',1)  
                   
        self.parser.add_section('Tool Parameters') 
        self.parser.set('Tool Parameters', 'diameter', 2.0)
        self.parser.set('Tool Parameters', 'start_radius', 0.2)

        self.parser.add_section('Plane Coordinates') 
        self.parser.set('Plane Coordinates', 'axis1_start_end', 0)
        self.parser.set('Plane Coordinates', 'axis2_start_end', 0)

        self.parser.add_section('Depth Coordinates') 
        self.parser.set('Depth Coordinates', 'axis3_retract', 15)
        self.parser.set('Depth Coordinates', 'axis3_safe_margin', 3.0)
        self.parser.set('Depth Coordinates', 'axis3_mill_depth', -3.0)
        self.parser.set('Depth Coordinates', 'axis3_slice_depth', -1.5)

        self.parser.add_section('Feed Rates')
        self.parser.set('Feed Rates', 'f_g1_depth', 150)
        self.parser.set('Feed Rates', 'f_g1_plane', 400)

        self.parser.add_section('Axis letters')
        self.parser.set('Axis letters', 'ax1_letter', 'X')
        self.parser.set('Axis letters', 'ax2_letter', 'Y')
        self.parser.set('Axis letters', 'ax3_letter', 'Z')                  

        self.parser.add_section('Route Optimisation')
        self.parser.set('Route Optimisation', 'Begin art','heurestic')
        self.parser.set('Route Optimisation', 'Max. population', 20)
        self.parser.set('Route Optimisation', 'Max. iterations', 300)  
        self.parser.set('Route Optimisation', 'Mutation Rate', 0.95)

        self.parser.add_section('Filters')
        self.parser.set('Filters', 'pstoedit_cmd','C:\Program Files (x86)\pstoedit\pstoedit')
        self.parser.set('Filters', 'pstoedit_opt', ['-f','dxf','-mm'])
                     
        self.parser.add_section('Debug')
        self.parser.set('Debug', 'global_debug_level', 0)         
                
        open_file = open(os.path.join(self.folder,self.cfg_file_name), "w") 
        self.parser.write(open_file) 
        open_file.close()
            
    def get_all_vars(self):
        #try:   
        self.write_to_stdout=int(self.parser.get('General', 'write_to_stdout'))
        

        self.tool_dia=DoubleVar()
        self.tool_dia.set(float(self.parser.get('Tool Parameters','diameter')))

        self.start_rad=DoubleVar()
        self.start_rad.set(float(self.parser.get('Tool Parameters','start_radius')))        
       
        self.axis1_st_en=DoubleVar()
        self.axis1_st_en.set(float(self.parser.get('Plane Coordinates','axis1_start_end')))

        self.axis2_st_en=DoubleVar()
        self.axis2_st_en.set(float(self.parser.get('Plane Coordinates','axis2_start_end')))        
        
        self.axis3_retract=DoubleVar()
        self.axis3_retract.set(float(self.parser.get('Depth Coordinates','axis3_retract')))
        
        self.axis3_safe_margin=DoubleVar()
        self.axis3_safe_margin.set(float(self.parser.get('Depth Coordinates','axis3_safe_margin')))

        self.axis3_slice_depth=DoubleVar()
        self.axis3_slice_depth.set(float(self.parser.get('Depth Coordinates','axis3_slice_depth')))        

        self.axis3_mill_depth=DoubleVar()
        self.axis3_mill_depth.set(float(self.parser.get('Depth Coordinates','axis3_mill_depth')))        
        
        self.F_G1_Depth=DoubleVar()
        self.F_G1_Depth.set(float(self.parser.get('Feed Rates','f_g1_depth')))

        self.F_G1_Plane=DoubleVar()
        self.F_G1_Plane.set(float(self.parser.get('Feed Rates','f_g1_plane')))

        self.points_tolerance=DoubleVar()
        self.points_tolerance.set(float(self.parser.get('Import Parameters','point_tolerance')))

        self.fitting_tolerance=DoubleVar()
        self.fitting_tolerance.set(float(self.parser.get('Import Parameters','fitting_tolerance')))
        self.spline_check=int(self.parser.get('Import Parameters', 'spline_check')  )

        #Zuweisen der Werte fuer die TSP Optimierung
        self.begin_art=self.parser.get('Route Optimisation', 'Begin art')
        self.max_population=int((int(self.parser.get('Route Optimisation', 'Max. population'))/4)*4)
        self.max_iterations=int(self.parser.get('Route Optimisation', 'Max. iterations'))  
        self.mutate_rate=float(self.parser.get('Route Optimisation', 'Mutation Rate', 0.95))

        #Zuweisen der Axis Letters
        self.ax1_letter=self.parser.get('Axis letters', 'ax1_letter')
        self.ax2_letter=self.parser.get('Axis letters', 'ax2_letter')
        self.ax3_letter=self.parser.get('Axis letters', 'ax3_letter')

        #Holen der restlichen Variablen
        #Verzeichnisse
        self.load_path=self.parser.get('Paths','load_path')
        self.save_path=self.parser.get('Paths','save_path')

        #Holen der Commandos fuer pstoedit
        self.pstoedit_cmd=self.parser.get('Filters','pstoedit_cmd')
        self.pstoedit_opt=self.parser.get('Filters','pstoedit_opt')

        #Setzen des Globalen Debug Levels
        self.debug=int(self.parser.get('Debug', 'global_debug_level'))
        
        
#        except:
#            showerror(_("Error during reading config file"), _("Please delete or correct\n %s")\
#                      %(os.path.join(self.folder,self.cfg_file_name)))
#            raise Exception, _("Problem during import from INI File") 
#            
    def __str__(self):

        str=''
        for section in self.parser.sections(): 
            str= str +"\nSection: "+section 
            for option in self.parser.options(section): 
                str= str+ "\n   -> %s=%s" % (option, self.parser.get(section, option))
        return str
コード例 #50
0
class ScalePanel(PluginPanel):
    name = 'ScaleAndMirror'
    title = _("Scale and Mirror")

    def init(self, master):
        PluginPanel.init(self, master)

        self.width_priority = 1

        root = self.mw.root
        self.var_width = DoubleVar(root)
        self.var_height = DoubleVar(root)

        unit = '%'
        jump = 5

        self.var_proportional = IntVar(root)
        self.var_proportional.set(0)

        self.var_basepoint = StringVar(root)
        self.var_basepoint.set('C')

        #---------------------------------------------------------
        top = TFrame(self.panel, style='FlatFrame')
        top.pack(side=TOP, fill=BOTH)
        #---------------------------------------------------------
        # Horisontal size
        size_frameH = TFrame(top, style='FlatFrame', borderwidth=3)
        size_frameH.pack(side=TOP, fill=BOTH)

        label = TLabel(size_frameH, style='FlatLabel', image='size_h')
        label.pack(side=LEFT, padx=5)
        self.entry_width = TSpinbox(size_frameH,
                                    var=100,
                                    vartype=1,
                                    textvariable=self.var_width,
                                    min=-30000,
                                    max=30000,
                                    step=jump,
                                    width=6,
                                    command=self.apply_scale)
        self.entry_width.pack(side=LEFT)

        self.entry_width.down_button.bind('<ButtonRelease>',
                                          self.entry_width_chang)
        self.entry_width.down_button.bind('<KeyRelease>',
                                          self.entry_width_chang)
        self.entry_width.up_button.bind('<ButtonRelease>',
                                        self.entry_width_chang)
        self.entry_width.up_button.bind('<KeyRelease>', self.entry_width_chang)
        self.entry_width.entry.bind('<ButtonRelease>', self.entry_width_chang)
        self.entry_width.entry.bind('<KeyRelease>', self.entry_width_chang)
        self.entry_width.entry.bind('<FocusOut>', self.entry_width_chang)
        self.entry_width.entry.bind('<FocusIn>', self.entry_width_FocusIn)

        self.labelwunit = TLabel(size_frameH, style='FlatLabel', text=unit)
        self.labelwunit.pack(side=LEFT, padx=5)

        self.hflip = BooleanVar(root)
        self.hflip.set(0)
        button = TCheckbutton(size_frameH,
                              image="pstrip_hflip",
                              style='ToolBarCheckButton',
                              variable=self.hflip,
                              command=None)
        button.pack(side=LEFT)

        #---------------------------------------------------------
        # Vertical

        size_frameV = TFrame(top, style='FlatFrame', borderwidth=3)
        size_frameV.pack(side=TOP, fill=BOTH)
        label = TLabel(size_frameV, style='FlatLabel', image='size_v')
        label.pack(side=LEFT, padx=5)

        self.entry_height = TSpinbox(size_frameV,
                                     var=100,
                                     vartype=1,
                                     textvariable=self.var_height,
                                     min=-30000,
                                     max=30000,
                                     step=jump,
                                     width=6,
                                     command=self.apply_scale)
        self.entry_height.pack(side=LEFT)

        self.entry_height.down_button.bind('<ButtonRelease>',
                                           self.entry_height_chang)
        self.entry_height.down_button.bind('<KeyRelease>',
                                           self.entry_height_chang)
        self.entry_height.up_button.bind('<ButtonRelease>',
                                         self.entry_height_chang)
        self.entry_height.up_button.bind('<KeyRelease>',
                                         self.entry_height_chang)
        self.entry_height.entry.bind('<ButtonRelease>',
                                     self.entry_height_chang)
        self.entry_height.entry.bind('<KeyRelease>', self.entry_height_chang)
        self.entry_height.entry.bind('<FocusOut>', self.entry_height_chang)
        self.entry_height.entry.bind('<FocusIn>', self.entry_height_FocusIn)

        self.labelhunit = TLabel(size_frameV, style='FlatLabel', text=unit)
        self.labelhunit.pack(side=LEFT, padx=5)

        self.vflip = BooleanVar(root)
        self.vflip.set(0)
        button = TCheckbutton(size_frameV,
                              image="pstrip_vflip",
                              style='ToolBarCheckButton',
                              variable=self.vflip,
                              command=None)
        button.pack(side=LEFT)

        #---------------------------------------------------------
        # Proportional chek

        self.proportional_check = TCheckbutton(top,
                                               text=_("Proportional"),
                                               variable=self.var_proportional,
                                               command=self.proportional)
        self.proportional_check.pack(side=TOP, anchor=W, padx=5, pady=5)

        #---------------------------------------------------------
        # Basepoint check
        label = TLabel(top, style='FlatLabel', text=_("Basepoint:"))
        label.pack(side=TOP, fill=BOTH, padx=5)
        basepoint_frame = TLabelframe(top,
                                      labelwidget=label,
                                      style='Labelframe',
                                      borderwidth=4)
        basepoint_frame.pack(side=TOP, fill=X, padx=5, pady=2)

        self.Basepoint = BasePointSelector(basepoint_frame,
                                           anchor=self.var_basepoint)
        self.Basepoint.pack(side=LEFT, fill=BOTH, padx=5)

        label = TLabel(basepoint_frame,
                       style='FlatLabel',
                       image='coordinate_sys')
        label.pack(side=LEFT, fill=BOTH, padx=10)

        #---------------------------------------------------------
        # Button frame

        button_frame = TFrame(top, style='FlatFrame', borderwidth=5)
        button_frame.pack(side=BOTTOM, fill=BOTH)

        self.update_buttons = []
        self.button = UpdatedButton(top,
                                    text=_("Apply"),
                                    command=self.apply_scale)
        self.button.pack(in_=button_frame,
                         side=BOTTOM,
                         expand=1,
                         fill=X,
                         pady=3)

        self.button_copy = UpdatedButton(top,
                                         text=_("Apply to Copy"),
                                         command=self.apply_to_copy)
        self.button_copy.pack(in_=button_frame, side=BOTTOM, expand=1, fill=X)

        self.init_from_doc()
        self.subscribe_receivers()


###############################################################################

    def subscribe_receivers(self):
        self.document.Subscribe(SELECTION, self.Update)

    def unsubscribe_receivers(self):
        self.document.Unsubscribe(SELECTION, self.Update)

    def init_from_doc(self, *arg):
        self.Update()

    def Update(self, *arg):
        if self.is_selection():
            self.entry_width.set_state(NORMAL)
            self.entry_height.set_state(NORMAL)
            self.proportional_check['state'] = NORMAL
            self.button['state'] = NORMAL
            self.button_copy['state'] = NORMAL
        else:
            self.entry_width.set_state(DISABLED)
            self.entry_height.set_state(DISABLED)
            self.proportional_check['state'] = DISABLED
            self.button['state'] = DISABLED
            self.button_copy['state'] = DISABLED

    def entry_width_FocusIn(self, *arg):
        self.width_priority = 1

    def entry_height_FocusIn(self, *arg):
        self.width_priority = 0

    def ScaleSelected(self, h, v, cnt_x=None, cnt_y=None):
        text = _("Scale")
        if self.document.selection:
            self.document.begin_transaction(text)
            try:
                try:
                    br = self.document.selection.coord_rect
                    hor_sel = br.right - br.left
                    ver_sel = br.top - br.bottom
                    if cnt_x is None:
                        cnt_x = hor_sel / 2 + br.left
                    if cnt_y is None:
                        cnt_y = ver_sel / 2 + br.bottom
                    trafo = Trafo(h, 0, 0, v, cnt_x - cnt_x * h,
                                  cnt_y - cnt_y * v)
                    self.document.TransformSelected(trafo, text)
                except:
                    self.document.abort_transaction()
            finally:
                self.document.end_transaction()

    def ScaleAndCopy(self, h, v, cnt_x=None, cnt_y=None):
        text = _("Scale&Copy")
        if self.document.selection:
            self.document.begin_transaction(text)
            try:
                try:
                    br = self.document.selection.coord_rect
                    hor_sel = br.right - br.left
                    ver_sel = br.top - br.bottom
                    if cnt_x is None:
                        cnt_x = hor_sel / 2 + br.left
                    if cnt_y is None:
                        cnt_y = ver_sel / 2 + br.bottom
                    trafo = Trafo(h, 0, 0, v, cnt_x - cnt_x * h,
                                  cnt_y - cnt_y * v)
                    self.document.ApplyToDuplicate()
                    self.document.TransformSelected(trafo, text)
                except:
                    self.document.abort_transaction()
            finally:
                self.document.end_transaction()

    def entry_height_chang(self, *arg):
        if self.var_proportional.get():
            self.var_width.set(self.var_height.get())

    def entry_width_chang(self, *arg):
        if self.var_proportional.get():
            self.var_height.set(self.var_width.get())

    def proportional(self):
        if self.width_priority:
            self.entry_width_chang()
        else:
            self.entry_height_chang()

    def apply_scale(self, *arg):
        if self.button["state"] == DISABLED:
            return
        self.proportional()
        width = self.var_width.get()
        height = self.var_height.get()
        br = self.document.selection.coord_rect
        hor_sel = br.right - br.left
        ver_sel = br.top - br.bottom
        cnt_x, cnt_y = self.Basepoint.get_basepoint(hor_sel, ver_sel, br.left,
                                                    br.bottom)

        h = width / 100
        if h == 0:
            h = 1
        if self.hflip.get():
            h = -1 * h

        v = height / 100
        if v == 0:
            v = 1
        if self.vflip.get():
            v = -1 * v

        if arg and arg[0] == 'Duplicate':
            self.ScaleAndCopy(h, v, cnt_x, cnt_y)
        else:
            self.ScaleSelected(h, v, cnt_x, cnt_y)

    def apply_to_copy(self):
        self.apply_scale('Duplicate')

    def is_selection(self):
        return (len(self.document.selection) > 0)
コード例 #51
0
class GUI:
    def _run(self):
        self.saveBtn.config(state=DISABLED)
        self.progressTxt.config(state=NORMAL)
        self.progressTxt.delete('1.0', END)
        self.progressTxt.update()
        self.progressTxt.config(state=DISABLED)
        inputId = self.txEntry.get()
        item = map(int, self.dbLbox.curselection())
        db = self.dbids[item[0]]
        self.runBtn.config(state=DISABLED)
        self.inputId, self.inputName, self.hprobes = main(inputId,
                                                          db,
                                                          debug=self.debug,
                                                          txt=self.progressTxt)
        self.runBtn.config(state=NORMAL)
        if self.hprobes is not None:
            self.saveBtn.config(state=NORMAL)

    def _quitGUI(self):
        #rpath = self.progressTxt.get('8.0','end-1c')
        #if rpath.startswith('Your results'):
        #  tkMessageBox.showinfo("Quit", self.progressTxt.get('8.0','end-1c'))
        self.master.destroy()

    def _save(self):
        hps = filter_probes(self.hprobes, self.spec.get(), self.mingc.get(),
                            self.multiexon.get(), self.mintm.get(),
                            self.maxtm.get(), self.mindimer.get(),
                            self.minfold.get(), self.maxduplex.get())
        result_csv = write_probesCSV(self.inputId, self.inputName, hps,
                                     self.progressTxt)
        result_fna = write_probesFNA(self.inputId, self.inputName, hps,
                                     self.progressTxt)
        tkMessageBox.showinfo('Result file',
                              'Details on ' + str(len(hps)) + \
                              ' hybridization probe(s) were exported to ' + \
                              result_csv + "\n\n" + \
                              'Sequences of '+ str(len(hps)) + \
                              ' hybridization probe(s) were exported to ' + \
                              result_fna)

    def __init__(self, master, dbnames, dbids, debug, version):
        self.dbids = dbids
        self.debug = debug

        self.master = master
        master.title('Plish Probe Designer')

        self.logoImg = PhotoImage(file=get_script_path() +
                                  '/img/plishLogo.gif')
        self.logoLbl = Label(master, image=self.logoImg)
        self.logoLbl.grid(row=0, columnspan=3)
        self.logoLbl.img = self.logoImg

        self.dbLbl = Label(master, text='Database')
        self.dbLbl.grid(row=1, sticky=W + N)
        self.dbLbox = Listbox(master, width=63, height=4)
        self.dbLbox.configure(exportselection=False)

        for i in range(len(dbnames)):
            self.dbLbox.insert(i, dbnames[i])
        self.dbLbox.select_set(0)
        self.dbLbox.grid(row=1, column=1, columnspan=2, sticky=N)

        self.txLbl = Label(master, text='Transcript ID')
        self.txLbl.grid(row=2, sticky=W + N)
        self.txEntry = Entry(master, width=39)
        self.txEntry.grid(row=2, column=1, sticky=W + N)

        self.runBtn = Button(master, text='Run', command=self._run, width=15)
        self.runBtn.grid(row=2, column=2)

        self.progressLbl = Label(master, text='Progress')
        self.progressLbl.grid(row=4, sticky=W + N)
        self.progressTxt = Text(bg="#263238",
                                fg="#ffffff",
                                state=DISABLED,
                                width=51,
                                height=16)
        self.progressTxt.grid(row=4, column=1)

        self.saveBtn = Button(master,
                              text='Save',
                              command=self._save,
                              state=DISABLED,
                              width=15)
        self.saveBtn.grid(row=5, column=2, sticky=N)

        self.quitBtn = Button(master,
                              text='Quit',
                              command=self._quitGUI,
                              width=15)
        self.quitBtn.grid(row=6, column=2, sticky=N)

        self.aboutLF = LabelFrame(master, text='About', width=300)
        self.aboutLF.grid(row=5,
                          column=0,
                          rowspan=2,
                          columnspan=2,
                          sticky=N + W)
        self.versionLbl = Label(self.aboutLF, text='PLISH Probe Designer, Version ' + version + '\n' + \
                          '(c) Heller lab, Stanford University School of Medicine\n' + \
                          '     Daniel C. Ellwanger <*****@*****.**>                       ',
                          justify=LEFT)
        self.versionLbl.grid(row=0, column=0, sticky=N)

        # Filter
        self.filterLF = LabelFrame(master, text='Filter')
        self.filterLF.grid(row=4, column=2, rowspan=2, sticky=N + W)

        self.mingc = DoubleVar()
        self.mingc.set(_defaultGC)
        self.mingcLbl = Label(self.filterLF, text='Min. GC')
        self.mingcLbl.grid(row=0, column=0, sticky=N + W)
        self.mingcEntry = Entry(self.filterLF, width=5, text=self.mingc)
        self.mingcEntry.grid(row=0, column=1, sticky=N + W)
        self.mingcLbl2 = Label(self.filterLF, text='%')
        self.mingcLbl2.grid(row=0, column=2, sticky=N + W)

        self.spec = StringVar(master)
        self.spec.set("isoform")
        self.specLbl = Label(self.filterLF, text='Specificity')
        self.specLbl.grid(row=1, column=0, sticky=N + W)
        self.specOm = OptionMenu(self.filterLF, self.spec, "isoform", "gene",
                                 "none")
        self.specOm.grid(row=1, column=1, sticky=N + W, columnspan=2)

        self.mintm = DoubleVar()
        self.mintm.set(_defaultMinTm)
        self.mintmLbl = Label(self.filterLF, text='Min. Tm')
        self.mintmLbl.grid(row=2, column=0, sticky=N + W)
        self.mintmEntry = Entry(self.filterLF, width=5, text=self.mintm)
        self.mintmEntry.grid(row=2, column=1, sticky=N + W)
        self.mintmLbl2 = Label(self.filterLF, text=u'\N{DEGREE SIGN}' + 'C')
        self.mintmLbl2.grid(row=2, column=2, sticky=N + W)

        self.maxtm = DoubleVar()
        self.maxtm.set(_defaultMaxTm)
        self.maxtmLbl = Label(self.filterLF, text='Max. Tm')
        self.maxtmLbl.grid(row=3, column=0, sticky=N + W)
        self.maxtmEntry = Entry(self.filterLF, width=5, text=self.maxtm)
        self.maxtmEntry.grid(row=3, column=1, sticky=N + W)
        self.maxtmLbl2 = Label(self.filterLF, text=u'\N{DEGREE SIGN}' + 'C')
        self.maxtmLbl2.grid(row=3, column=2, sticky=N + W)

        self.minfold = DoubleVar()
        self.minfold.set(_defaultMinFold)
        self.minfoldLbl = Label(self.filterLF, text='Min. Fold')
        self.minfoldLbl.grid(row=4, column=0, sticky=N + W)
        self.minfoldEntry = Entry(self.filterLF, width=5, text=self.minfold)
        self.minfoldEntry.grid(row=4, column=1, sticky=N + W)
        self.minfoldLbl2 = Label(self.filterLF, text='kcal/mol')
        self.minfoldLbl2.grid(row=4, column=2, sticky=N + W)

        self.mindimer = DoubleVar()
        self.mindimer.set(_defaultMinDimer)
        self.mindimerLbl = Label(self.filterLF, text='Min. Dimer')
        self.mindimerLbl.grid(row=5, column=0, sticky=N + W)
        self.mindimerEntry = Entry(self.filterLF, width=5, text=self.mindimer)
        self.mindimerEntry.grid(row=5, column=1, sticky=N + W)
        self.mindimerLbl2 = Label(self.filterLF, text='kcal/mol')
        self.mindimerLbl2.grid(row=5, column=2, sticky=N + W)

        self.maxduplex = DoubleVar()
        self.maxduplex.set(_defaultMaxDuplex)
        self.maxduplexLbl = Label(self.filterLF, text='Max. Duplex')
        self.maxduplexLbl.grid(row=6, column=0, sticky=N + W)
        self.maxduplexEntry = Entry(self.filterLF,
                                    width=5,
                                    text=self.maxduplex)
        self.maxduplexEntry.grid(row=6, column=1, sticky=N + W)
        self.maxduplexLbl2 = Label(self.filterLF, text='kcal/mol')
        self.maxduplexLbl2.grid(row=6, column=2, sticky=N + W)

        self.multiexon = BooleanVar()
        self.multiexon.set(_defaultMultiExon)
        self.multiexonCb = Checkbutton(self.filterLF,
                                       text='Multi-exon',
                                       variable=self.multiexon,
                                       onvalue=True,
                                       offvalue=False)
        self.multiexonCb.grid(row=7, column=0, sticky=N + W)
コード例 #52
0
class gui(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.initUI()
        self.centerUI(w=600, h=250)

    def initUI(self):

        self.parent.title("FIND BLR COEFFICIENT VALUE")
        self.pack(fill=BOTH, expand=True)

        self.columnconfigure(0, weight=1)
        # self.rowconfigure(0, weight=1)
        # weight attibute is used to make them growable

        ###### GUI Control Variables ######
        self.LIMIT_L = IntVar()
        self.LIMIT_H = IntVar()
        self.PULSE_R = IntVar()
        self.PULSE_L = IntVar()
        self.pulse_height = DoubleVar()
        self.hdf5_file = StringVar()
        self.PMT = IntVar()
        self.EVENT = IntVar()
        self.amplitude_range = DoubleVar()
        self.delta = DoubleVar()
        self.noise_sigma = DoubleVar()
        self.coeff = DoubleVar()
        #self.DRAW               = BooleanVar()

        search = Image.open("next_logo.jpg")
        search_temp = search.resize((170, 200), Image.ANTIALIAS)
        search_aux = ImageTk.PhotoImage(search_temp)
        label1 = Label(self, image=search_aux)
        label1.image = search_aux
        label1.grid(row=0,
                    column=0,
                    columnspan=10,
                    rowspan=10,
                    sticky=E + W + S + N)

        self.hdf5_file.set("2052.h5.z")
        e1 = Entry(self, textvariable=self.hdf5_file, width=30)
        e1.grid(row=1, column=1, sticky=W, columnspan=5, pady=5)
        e1_label = Label(self, text="HDF5 file")
        e1_label.grid(row=0, column=1, sticky=W, columnspan=5, pady=5)

        self.PMT.set("0")
        sb1 = Spinbox(self, from_=0, to=12, width=3, textvariable=self.PMT)
        sb1.grid(row=3, column=2, sticky=W)
        sb1_label = Label(self, text="PMT")
        sb1_label.grid(row=2, column=2, padx=0, sticky=W)

        self.EVENT.set("0")
        sb1 = Spinbox(self, from_=0, to=1000, width=5, textvariable=self.EVENT)
        sb1.grid(row=3, column=3, sticky=W)
        sb1_label = Label(self, text="EVENT")
        sb1_label.grid(row=2, column=3, padx=0, sticky=W)

        self.LIMIT_L.set("19000")
        sb1 = Spinbox(self,
                      from_=0,
                      to=100000,
                      width=5,
                      textvariable=self.LIMIT_L)
        sb1.grid(row=5, column=2, sticky=W)
        sb1_label = Label(self, text="ROI Start ")
        sb1_label.grid(row=4, column=2, padx=0, sticky=W)

        self.LIMIT_H.set("22500")
        sb1 = Spinbox(self,
                      from_=0,
                      to=100000,
                      width=5,
                      textvariable=self.LIMIT_H)
        sb1.grid(row=5, column=3, sticky=W)
        sb1_label = Label(self, text="ROI End ")
        sb1_label.grid(row=4, column=3, padx=0, sticky=W)

        self.PULSE_R.set("20142")
        sb1 = Spinbox(self,
                      from_=0,
                      to=100000,
                      width=8,
                      textvariable=self.PULSE_R)
        sb1.grid(row=5, column=4, sticky=E)
        sb1_label = Label(self, text=" Pulse Rise")
        sb1_label.grid(row=4, column=4, padx=0, sticky=E)

        self.PULSE_L.set("1200")
        sb1 = Spinbox(self,
                      from_=0,
                      to=5000,
                      width=8,
                      textvariable=self.PULSE_L)
        sb1.grid(row=5, column=5, sticky=E)
        sb1_label = Label(self, text=" Pulse Length")
        sb1_label.grid(row=4, column=5, padx=0, sticky=E)

        sb1_label = Label(self, text="  ")
        sb1_label.grid(row=2, column=7, padx=0, sticky=W)
        sb1_label = Label(self, text="  ")
        sb1_label.grid(row=6, column=7, padx=0, sticky=W)

        self.pulse_height.set("545.5")
        sb1 = Entry(self, width=8, textvariable=self.pulse_height)
        sb1.grid(row=7, column=3, sticky=E)
        sb1_label = Label(self, text=" Amplitude")
        sb1_label.grid(row=6, column=3, padx=0, sticky=E)

        self.amplitude_range.set("2")
        sb1 = Entry(self, width=8, textvariable=self.amplitude_range)
        sb1.grid(row=7, column=4, sticky=E)
        sb1_label = Label(self, text=" Loop Range")
        sb1_label.grid(row=6, column=4, padx=0, sticky=E)

        self.delta.set("0.1")
        sb1 = Entry(self, width=8, textvariable=self.delta)
        sb1.grid(row=7, column=5, sticky=E)
        sb1_label = Label(self, text=" Loop Delta")
        sb1_label.grid(row=6, column=5, padx=0, sticky=E)

        self.noise_sigma.set("4")
        sb1 = Entry(self, width=3, textvariable=self.noise_sigma)
        sb1.grid(row=5, column=6, sticky=E)
        sb1_label = Label(self, text=" Noise Threshold")
        sb1_label.grid(row=4, column=6, padx=0, sticky=E)

        sb_coeff_label = Label(self, text="Coefficient ")
        sb_coeff_label.grid(row=0, column=6, padx=0, sticky=E)
        self.sb_coeff = Label(self)
        self.sb_coeff.grid(row=1, column=6, padx=0, sticky=E)

        # MAIN BUTTONS
        obtn = Button(self, text="GO!!", command=self.find_C)
        obtn.grid(row=14, column=4, sticky=E, pady=10)

        cbtn = Button(self, text="Quit", command=self.quit)
        cbtn.grid(row=14, column=5, sticky=E, pady=10)

        hbtn = Button(self, text="Help", command=self.help_f)
        hbtn.grid(row=14, column=0, sticky=W, pady=10)

    def help_f(self):
        top = Toplevel()
        top.title("HELP")
        msg = Message(top,
                      width=500,
                      text="COEFF Calibration Procedure: \n \
             Input Start Point and Length of the pulse \n \
             Input an initial guess of the pulse amplitude \n \
             Use a ROI with at least 1000 samples of baseline \n \
             and 1000 samples after pulse end \n \
             Adjust loop range and step until a graph error \n \
             with a minimum is observed \n \
             Refine the search to increase precision")
        msg.pack()

        button = Button(top, text="Close", command=top.destroy)
        button.pack()

    def find_C(self):

        draw = False
        LIMIT_L = self.LIMIT_L.get()  #19000
        LIMIT_H = self.LIMIT_H.get()  #22500
        PULSE_R = self.PULSE_R.get()  #20142
        PULSE_L = self.PULSE_L.get()  #1200
        pulse_height = self.pulse_height.get()  #545
        hdf5_file = self.hdf5_file.get()  #'2052.h5.z'
        PMT = self.PMT.get()
        event = self.EVENT.get()
        amplitude_range = self.amplitude_range.get()  #2
        delta = self.delta.get()  #0.1
        noise_sigma = self.noise_sigma.get()  #4

        coeff_aux = fc.find_coeff(LIMIT_L, LIMIT_H, PULSE_R, PULSE_L,
                                  pulse_height, hdf5_file, PMT, event,
                                  amplitude_range, delta, noise_sigma, draw)

        plt.show()
        self.sb_coeff.configure(text=str(coeff_aux))

    def centerUI(self, w, h):
        sw = self.parent.winfo_screenwidth()
        sh = self.parent.winfo_screenheight()
        x = (sw - w) / 2
        y = (sh - h) / 2
        self.parent.geometry('%dx%d+%d+%d' % (w, h, x, y))
コード例 #53
0
ファイル: rcomplete.py プロジェクト: JLuebben/R_complete
def gui():
    from Tkinter import Tk, Label, Entry, Button, Scale, Checkbutton, W, HORIZONTAL, Frame, StringVar, IntVar, DoubleVar, Radiobutton, BooleanVar, E

    global root
    root = Tk()
    root.wm_title("Compute R_complete")
    line = 0

    global insFile, hklFile, nHKL, nParams, nHKLLabel, fracFree, status, nParamsLabel, nCPU, rCompleteLabel, cycles, lsType, cleanup, nFree, nRunsLabel, mergeCheck, compileMap
    insFile = StringVar()
    hklFile = StringVar()
    nHKL = IntVar()
    nParams = IntVar()
    nFree = IntVar()
    fracFree = DoubleVar()
    fracFree.set(5.0)
    nCPU = IntVar()
    nCPU.set(maxCPU)
    cycles = IntVar()
    cycles.set(10)
    lsType = IntVar()
    lsType.set(1)
    cleanup = BooleanVar()
    cleanup.set(True)
    mergeCheck = BooleanVar()
    mergeCheck.set(True)
    compileMap = BooleanVar()
    compileMap.set(True)

    Label(root, text='Instruction File:').grid(row=line, column=0, sticky=E)
    Entry(root, textvariable=insFile).grid(row=line, column=1)
    Button(root, text='Browse', command=browseINS).grid(row=line, column=2)

    line += 1

    Label(root, text='Reflection File:').grid(row=line, column=0, sticky=E)
    Entry(root, textvariable=hklFile).grid(row=line, column=1)
    Button(root, text='Browse', command=browseHKL).grid(row=line, column=2)

    line += 1
    Checkbutton(root, var=mergeCheck, text='Merge Reflections').grid(row=line,
                                                                     column=1,
                                                                     sticky=W)
    line += 1
    Button(root, text='Load', command=load).grid(row=line, columnspan=3)
    line += 1

    Frame(root, height=20).grid(row=line)

    line += 1

    Label(root, text='# of reflections:').grid(row=line, sticky=E)
    nHKLLabel = Label(root, text='???')
    nHKLLabel.grid(row=line, column=1, sticky=W)

    line += 1

    Label(root, text='# of atoms:').grid(row=line, sticky=E)
    nParamsLabel = Label(root, text='???')
    nParamsLabel.grid(row=line, column=1, sticky=W)

    line += 1

    Frame(root, height=20).grid(row=line)

    line += 1

    Label(root, text='Select Parameters').grid(row=line, column=1)
    line += 1

    Frame(root, height=20).grid(row=line)
    line += 1

    Label(root, text='# of free reflections:').grid(row=line, sticky=E)
    nFreeEntry = Entry(root, width=5, textvariable=nFree)
    nFreeEntry.grid(row=line, column=1, sticky=W)
    nFreeEntry.bind('<Return>', setScale)
    nRunsLabel = Label(root, text='# runs')
    nRunsLabel.grid(row=line, column=2)

    line += 1

    Label(root, text='% of free reflections:').grid(row=line,
                                                    column=0,
                                                    sticky=E)
    w = Scale(root,
              from_=0.1,
              to=10.0,
              resolution=0.1,
              orient=HORIZONTAL,
              length=200,
              var=fracFree,
              command=percentScale)
    w.grid(row=line, column=1, columnspan=2, sticky=W)

    line += 1

    Label(root, text='stable <-------------------------------> fast').grid(
        row=line, column=1, columnspan=2, sticky=W)

    line += 1
    Frame(root, height=10).grid(row=line)

    line += 1

    Label(root, text='Refinement cycles:').grid(row=line, column=0, sticky=E)
    ls = Scale(root,
               from_=0,
               to=50,
               resolution=1,
               orient=HORIZONTAL,
               length=200,
               var=cycles)
    ls.grid(row=line, column=1, columnspan=2, sticky=W)

    line += 1

    Label(root, text='fast <--------------------> less model bias').grid(
        row=line, column=1, columnspan=2, sticky=W)

    line += 1
    Frame(root, height=10).grid(row=line)

    line += 1
    Label(root, text='# of CPUs:').grid(row=line, column=0, sticky=E)
    ww = Scale(root,
               from_=1,
               to=maxCPU,
               orient=HORIZONTAL,
               length=200,
               var=nCPU)
    ww.grid(row=line, column=1, columnspan=2, sticky=W)

    line += 1

    Label(root, text='Refinement Type:').grid(row=line, column=0, sticky=E)
    Radiobutton(root, text='CGLS', var=lsType, value=1).grid(row=line,
                                                             column=1,
                                                             sticky=W)
    Radiobutton(root, text='L.S.', var=lsType, value=2).grid(row=line,
                                                             column=2,
                                                             sticky=W)

    line += 1
    Frame(root, height=10).grid(row=line)
    line += 1

    Label(root, text='Compile map:').grid(row=line, column=0, sticky=E)
    Checkbutton(root, var=compileMap).grid(row=line, column=1, sticky=W)

    line += 1
    Label(root, text='Cleanup:').grid(row=line, column=0, sticky=E)
    Checkbutton(root, var=cleanup).grid(row=line, column=1, sticky=W)

    line += 1

    Button(root, text='RUN', command=run, width=25).grid(row=line,
                                                         columnspan=3)

    line += 1
    Frame(root, height=20).grid(row=line)
    line += 1

    Label(root, text='R_complete:').grid(row=line, column=0, sticky=E)
    rCompleteLabel = Label(root, text='???')
    rCompleteLabel.grid(row=line, column=1, sticky=W)

    line += 1

    Frame(root, height=20).grid(row=line)

    line += 1

    Label(root, text='Status:').grid(row=line, column=0, sticky=E)
    status = Label(root, text='Idle... Please load files.')
    status.grid(row=line, column=1, columnspan=2, sticky=W)
    global IDLE
    IDLE = True

    root.mainloop()
コード例 #54
0
class CMYKDigitizer(TFrame):
    def __init__(self, parent, callback, **kw):
        self.callback = callback
        TFrame.__init__(self, parent, style='FlatFrame', **kw)
        self.C_value = DoubleVar(0)
        self.M_value = DoubleVar(0)
        self.Y_value = DoubleVar(0)
        self.K_value = DoubleVar(0)
        self.A_value = DoubleVar(0)

        b = TLabel(self, style='HLine')
        b.pack(side=BOTTOM, fill=X)

        frame = TFrame(self, borderwidth=0, style='FlatFrame')
        frame.pack(side=BOTTOM)
        label = TLabel(frame, text=_("Opacity: "))
        label.pack(side=LEFT)
        self.A_spin = TSpinbox(frame,
                               min=0,
                               max=255,
                               step=1,
                               vartype=0,
                               width=7,
                               textvariable=self.A_value,
                               command=self.cmyk_component_changed)
        self.A_spin.pack(side=RIGHT)

        b = TLabel(self, style='HLine')
        b.pack(side=BOTTOM, fill=X)

        cmyk_frame = TFrame(self, borderwidth=2, style='FlatFrame')
        cmyk_frame.pack(side=LEFT, padx=10)

        frame = TFrame(cmyk_frame, borderwidth=2, style='FlatFrame')
        frame.pack(side=TOP)
        label = TLabel(frame, text="C: ")
        label.pack(side=LEFT)
        self.C_spin = TSpinbox(frame,
                               min=0,
                               max=100,
                               step=1,
                               vartype=1,
                               width=7,
                               textvariable=self.C_value,
                               command=self.cmyk_component_changed)
        self.C_spin.pack(side=RIGHT)

        frame = TFrame(cmyk_frame, borderwidth=2, style='FlatFrame')
        frame.pack(side=TOP)
        label = TLabel(frame, text="M: ")
        label.pack(side=LEFT)
        self.M_spin = TSpinbox(frame,
                               min=0,
                               max=100,
                               step=1,
                               vartype=1,
                               width=7,
                               textvariable=self.M_value,
                               command=self.cmyk_component_changed)
        self.M_spin.pack(side=RIGHT)

        frame = TFrame(cmyk_frame, borderwidth=2, style='FlatFrame')
        frame.pack(side=TOP)
        label = TLabel(frame, text="Y: ")
        label.pack(side=LEFT)
        self.Y_spin = TSpinbox(frame,
                               min=0,
                               max=100,
                               step=1,
                               vartype=1,
                               width=7,
                               textvariable=self.Y_value,
                               command=self.cmyk_component_changed)
        self.Y_spin.pack(side=RIGHT)

        frame = TFrame(cmyk_frame, borderwidth=2, style='FlatFrame')
        frame.pack(side=TOP)
        label = TLabel(frame, text="K: ")
        label.pack(side=LEFT)
        self.K_spin = TSpinbox(frame,
                               min=0,
                               max=100,
                               step=1,
                               vartype=1,
                               width=7,
                               textvariable=self.K_value,
                               command=self.cmyk_component_changed)
        self.K_spin.pack(side=RIGHT)

        rgb_frame = TFrame(self, borderwidth=2, style='FlatFrame')
        rgb_frame.pack(side=LEFT)

        self.RGB_label = TLabel(rgb_frame, text='R:\nG:\nB:', justify=LEFT)
        self.RGB_label.pack(side=LEFT)

    def set_color(self, color):
        self.color = color
        c, m, y, k = color.getCMYK()
        self.C_value.set(round(c * 100, 2))
        self.M_value.set(round(m * 100, 2))
        self.Y_value.set(round(y * 100, 2))
        self.K_value.set(round(k * 100, 2))
        self.A_value.set(int(round(color.alpha * 100)))
        r, g, b = color.getRGB()
        text = 'R: %d\nG: %d\nB: %d' % (round(r * 255, 2), round(
            g * 255, 2), round(b * 255, 2))
        int_color = (round(r * 255), round(g * 255), round(b * 255))
        text += '\n\n#%02X%02X%02X' % int_color
        self.RGB_label['text'] = text

    def cmyk_component_changed(self, *arg):
        c = self.C_value.get() / 100.0
        m = self.M_value.get() / 100.0
        y = self.Y_value.get() / 100.0
        k = self.K_value.get() / 100.0
        a = self.A_value.get() / 100.0
        self.callback(CreateCMYKAColor(c, m, y, k, a))
コード例 #55
0
ファイル: find_coeff_GUI.py プロジェクト: jjgomezcadenas/IC
class gui(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.initUI()
        self.centerUI(w=600,h=250)

    def initUI(self):
        
        self.parent.title("FIND BLR COEFFICIENT VALUE")
        self.pack(fill=BOTH, expand=True)
        
        self.columnconfigure(0, weight=1)
        # self.rowconfigure(0, weight=1)
        # weight attibute is used to make them growable
        
        ###### GUI Control Variables ######
        self.LIMIT_L            = IntVar()
        self.LIMIT_H            = IntVar()
        self.PULSE_R            = IntVar()
        self.PULSE_L            = IntVar()
        self.pulse_height       = DoubleVar()
        self.hdf5_file          = StringVar()
        self.PMT                = IntVar()
        self.EVENT              = IntVar()
        self.amplitude_range    = DoubleVar()
        self.delta              = DoubleVar()
        self.noise_sigma        = DoubleVar()
        self.coeff              = DoubleVar()
        #self.DRAW               = BooleanVar()
        
        
        search = Image.open("next_logo.jpg")
        search_temp = search.resize((170, 200), Image.ANTIALIAS)
        search_aux = ImageTk.PhotoImage(search_temp)
        label1 = Label(self, image=search_aux)
        label1.image = search_aux
        label1.grid(row=0, column=0,
				columnspan=10, rowspan=10, sticky=E+W+S+N)
        
        
        self.hdf5_file.set("2052.h5.z")
        e1 = Entry(self, textvariable=self.hdf5_file, width=30)
        e1.grid(row=1,column=1, sticky=W, columnspan=5, pady=5)
        e1_label = Label(self, text="HDF5 file")
        e1_label.grid(row=0,column=1,sticky=W, columnspan=5, pady=5)        
               
        self.PMT.set("0")
        sb1 = Spinbox(self, from_=0, to=12, 
                      width=3, textvariable=self.PMT)
        sb1.grid(row=3,column=2, sticky=W)
        sb1_label = Label(self, text="PMT")
        sb1_label.grid(row=2,column=2, padx=0, sticky=W)
        
        self.EVENT.set("0")
        sb1 = Spinbox(self, from_=0, to=1000, 
                      width=5, textvariable=self.EVENT)
        sb1.grid(row=3,column=3, sticky=W)
        sb1_label = Label(self, text="EVENT")
        sb1_label.grid(row=2,column=3, padx=0, sticky=W)
        
        
        
        self.LIMIT_L.set("19000")
        sb1 = Spinbox(self, from_=0, to=100000, 
                      width=5, textvariable=self.LIMIT_L)
        sb1.grid(row=5,column=2, sticky=W)
        sb1_label = Label(self, text="ROI Start ")
        sb1_label.grid(row=4,column=2, padx=0, sticky=W)
        
        self.LIMIT_H.set("22500")
        sb1 = Spinbox(self, from_=0, to=100000, 
                      width=5, textvariable=self.LIMIT_H)
        sb1.grid(row=5,column=3, sticky=W)
        sb1_label = Label(self, text="ROI End ")
        sb1_label.grid(row=4,column=3, padx=0, sticky=W)
        
        
        
           
        self.PULSE_R.set("20142")
        sb1 = Spinbox(self, from_=0, to=100000, 
                      width=8, textvariable=self.PULSE_R)
        sb1.grid(row=5,column=4, sticky=E)
        sb1_label = Label(self, text=" Pulse Rise")
        sb1_label.grid(row=4,column=4, padx=0, sticky=E)
        
        self.PULSE_L.set("1200")
        sb1 = Spinbox(self, from_=0, to=5000, 
                      width=8, textvariable=self.PULSE_L)
        sb1.grid(row=5,column=5, sticky=E)
        sb1_label = Label(self, text=" Pulse Length")
        sb1_label.grid(row=4,column=5, padx=0, sticky=E)
        
        
        
        sb1_label = Label(self, text="  ")
        sb1_label.grid(row=2,column=7, padx=0, sticky=W)        
        sb1_label = Label(self, text="  ")
        sb1_label.grid(row=6,column=7, padx=0, sticky=W)
        
        
        self.pulse_height.set("545.5")
        sb1 = Entry(self, width=8, textvariable=self.pulse_height)
        sb1.grid(row=7,column=3, sticky=E)
        sb1_label = Label(self, text=" Amplitude")
        sb1_label.grid(row=6,column=3, padx=0, sticky=E)
        
        self.amplitude_range.set("2")
        sb1 = Entry(self, width=8, textvariable=self.amplitude_range)
        sb1.grid(row=7,column=4, sticky=E)
        sb1_label = Label(self, text=" Loop Range")
        sb1_label.grid(row=6,column=4, padx=0, sticky=E)
        
        self.delta.set("0.1")
        sb1 = Entry(self, width=8, textvariable=self.delta)
        sb1.grid(row=7,column=5, sticky=E)
        sb1_label = Label(self, text=" Loop Delta")
        sb1_label.grid(row=6,column=5, padx=0, sticky=E)
        
        self.noise_sigma.set("4")
        sb1 = Entry(self, width=3, textvariable=self.noise_sigma)
        sb1.grid(row=5,column=6, sticky=E)
        sb1_label = Label(self, text=" Noise Threshold")
        sb1_label.grid(row=4,column=6, padx=0, sticky=E)
        
        sb_coeff_label = Label(self, text= "Coefficient ")
        sb_coeff_label.grid(row=0,column=6, padx=0, sticky=E)
        self.sb_coeff = Label(self)
        self.sb_coeff.grid(row=1,column=6, padx=0, sticky=E)


        
        # MAIN BUTTONS
        obtn = Button(self, text="GO!!", command=self.find_C)
        obtn.grid(row=14, column=4, sticky=E, pady=10)
        
        cbtn = Button(self, text="Quit", command=self.quit)
        cbtn.grid(row=14, column=5, sticky=E, pady=10)
        
        hbtn = Button(self, text="Help", command=self.help_f)
        hbtn.grid(row=14, column=0, sticky=W, pady=10)


    def help_f(self):
		top = Toplevel()
		top.title("HELP")
		msg = Message(top, width= 500,
             text="COEFF Calibration Procedure: \n \
             Input Start Point and Length of the pulse \n \
             Input an initial guess of the pulse amplitude \n \
             Use a ROI with at least 1000 samples of baseline \n \
             and 1000 samples after pulse end \n \
             Adjust loop range and step until a graph error \n \
             with a minimum is observed \n \
             Refine the search to increase precision")
		msg.pack()

		button = Button(top, text="Close", command=top.destroy)
		button.pack()

        
    
    def find_C(self):
        
        draw = False
        LIMIT_L       = self.LIMIT_L.get()      #19000
        LIMIT_H       = self.LIMIT_H.get()      #22500
        PULSE_R       = self.PULSE_R.get()      #20142
        PULSE_L       = self.PULSE_L.get()      #1200
        pulse_height  = self.pulse_height.get() #545
        hdf5_file 	 = self.hdf5_file.get()   #'2052.h5.z'
        PMT 		    = self.PMT.get()
        event         = self.EVENT.get()
        amplitude_range = self.amplitude_range.get() #2
        delta          = self.delta.get()       #0.1
        noise_sigma     = self.noise_sigma.get() #4
        
        
        coeff_aux = fc.find_coeff(LIMIT_L, LIMIT_H, PULSE_R, PULSE_L,
                             pulse_height,
                             hdf5_file, PMT, event,
                             amplitude_range, delta,
                             noise_sigma,
                             draw)
        
        plt.show() 
        self.sb_coeff.configure(text=str(coeff_aux))
																		
    
    
    def centerUI(self,w,h):
        sw = self.parent.winfo_screenwidth()
        sh = self.parent.winfo_screenheight()
        x  = (sw-w)/2
        y  = (sh-h)/2
        self.parent.geometry('%dx%d+%d+%d' % (w,h,x,y))
コード例 #56
0
ファイル: colordigitizer.py プロジェクト: kindlychung/sk1
class CMYKDigitizer(TFrame):
	
	def __init__(self, parent, callback, **kw):
		self.callback = callback
		TFrame.__init__(self, parent, style='FlatFrame', **kw)
		self.C_value = DoubleVar(0)
		self.M_value = DoubleVar(0)
		self.Y_value = DoubleVar(0)
		self.K_value = DoubleVar(0)
		self.A_value = DoubleVar(0)
		
		b = TLabel(self, style='HLine')
		b.pack(side=BOTTOM, fill=X)
		
		frame = TFrame(self, borderwidth=0, style='FlatFrame')
		frame.pack(side=BOTTOM)
		label = TLabel(frame, text=_("Opacity: "))
		label.pack(side=LEFT)
		self.A_spin = TSpinbox(frame, min=0, max=255, step=1, vartype=0, width=7,
							textvariable=self.A_value, command=self.cmyk_component_changed)
		self.A_spin.pack(side=RIGHT)
		
		b = TLabel(self, style='HLine')
		b.pack(side=BOTTOM, fill=X)
		
		cmyk_frame = TFrame(self, borderwidth=2, style='FlatFrame')
		cmyk_frame.pack(side=LEFT, padx=10)
		
		frame = TFrame(cmyk_frame, borderwidth=2, style='FlatFrame')
		frame.pack(side=TOP)
		label = TLabel(frame, text="C: ")
		label.pack(side=LEFT)
		self.C_spin = TSpinbox(frame, min=0, max=100, step=1, vartype=1, width=7,
							textvariable=self.C_value, command=self.cmyk_component_changed)
		self.C_spin.pack(side=RIGHT)

		frame = TFrame(cmyk_frame, borderwidth=2, style='FlatFrame')
		frame.pack(side=TOP)
		label = TLabel(frame, text="M: ")
		label.pack(side=LEFT)
		self.M_spin = TSpinbox(frame, min=0, max=100, step=1, vartype=1, width=7,
							textvariable=self.M_value, command=self.cmyk_component_changed)
		self.M_spin.pack(side=RIGHT)

		frame = TFrame(cmyk_frame, borderwidth=2, style='FlatFrame')
		frame.pack(side=TOP)
		label = TLabel(frame, text="Y: ")
		label.pack(side=LEFT)
		self.Y_spin = TSpinbox(frame, min=0, max=100, step=1, vartype=1, width=7,
							textvariable=self.Y_value, command=self.cmyk_component_changed)
		self.Y_spin.pack(side=RIGHT)		

		frame = TFrame(cmyk_frame, borderwidth=2, style='FlatFrame')
		frame.pack(side=TOP)
		label = TLabel(frame, text="K: ")
		label.pack(side=LEFT)
		self.K_spin = TSpinbox(frame, min=0, max=100, step=1, vartype=1, width=7,
							textvariable=self.K_value, command=self.cmyk_component_changed)
		self.K_spin.pack(side=RIGHT)
		
		rgb_frame = TFrame(self, borderwidth=2, style='FlatFrame')
		rgb_frame.pack(side=LEFT)
		
		self.RGB_label = TLabel(rgb_frame, text='R:\nG:\nB:', justify=LEFT)
		self.RGB_label.pack(side=LEFT)		
		
		
	def set_color(self, color):
		self.color = color
		c, m, y, k = color.getCMYK()
		self.C_value.set(round(c * 100, 2))
		self.M_value.set(round(m * 100, 2))
		self.Y_value.set(round(y * 100, 2))
		self.K_value.set(round(k * 100, 2))
		self.A_value.set(int(round(color.alpha * 100)))
		r, g, b = color.getRGB()
		text = 'R: %d\nG: %d\nB: %d' % (round(r * 255, 2), round(g * 255, 2), round(b * 255, 2))
		int_color = (round(r * 255), round(g * 255), round(b * 255))
		text += '\n\n#%02X%02X%02X' % int_color		
		self.RGB_label['text'] = text	
		
		
	def cmyk_component_changed(self, *arg):
		c = self.C_value.get() / 100.0
		m = self.M_value.get() / 100.0
		y = self.Y_value.get() / 100.0
		k = self.K_value.get() / 100.0		
		a = self.A_value.get() / 100.0
		self.callback(CreateCMYKAColor(c, m, y, k, a))
コード例 #57
0
class TopoConsole(tk.AppWindow,tk.TkParameterized):
    """
    Main window for the Tk-based GUI.
    """

    def _getmenubar(self):
        return self.master.menubar

    menubar = property(_getmenubar)

    def __getitem__(self,menu_name):
        """Allow dictionary-style access to the menu bar."""
        return self.menubar[menu_name]

    def __init__(self, root,exit_on_quit=True, **params):
        tk.AppWindow.__init__(self,root,status=True)
        tk.TkParameterized.__init__(self,root,**params)

        # Instead of displaying tracebacks on the commandline, try to display
        # them on the originating window.
        # CEBALERT: on destroy(), ought to revert this
        Tkinter.Misc._report_exception=_tkinter_report_exception

        self.exit_on_quit = exit_on_quit
        self.auto_refresh_panels = []
        self._init_widgets()
        self.title(topo.sim.name) # If -g passed *before* scripts on commandline, this is useless.
                                  # So topo.misc.commandline sets the title as its last action (if -g)



        # catch click on the 'x': offers choice to quit or not
        self.protocol("WM_DELETE_WINDOW",self.quit_topographica)


        ##########
        ### Make cascade menus open automatically on linux when the mouse
        ### is over the menu title.
        ### [Tkinter-discuss] Cascade menu issue
        ### http://mail.python.org/pipermail/tkinter-discuss/2006-August/000864.html
        if topo.tkgui.system_platform is 'linux':
            activate_cascade = """\
            if {[%W cget -type] != {menubar} && [%W type active] == {cascade}} {
                %W postcascade active
               }
            """
            self.bind_class("Menu", "<<MenuSelect>>", activate_cascade)
        ##########

        # Install warning and message handling
        from param.parameterized import Parameterized
        self.__orig_P_warning = Parameterized.warning
        #self.__orig_P_message = Parameterized.message
        type.__setattr__(Parameterized,'warning',self.gui_warning)
        #type.__setattr__(Parameterized,'message',self.gui_message)

    def gui_warning(self,msg,*args,**kw):
        stat = self.__get_status_bar()
        stat.warn(msg%args)
        self.__orig_P_warning(self,msg,*args,**kw)

    def gui_message(self,msg,*args,**kw):
        stat = self.__get_status_bar()
        stat.message(msg%args)
        self.__orig_P_message(self,*args)


    def title(self,t=None):
        newtitle = "Topographica"
        if t: newtitle+=": %s" % t
        tk.AppWindow.title(self,newtitle)


    def _init_widgets(self):

        ## CEBALERT: now we can have multiple operations at the same time,
        ## status bar could be improved to show all tasks?

        # CEBALERT
        self.messageBar = self.status

        self.some_area = DockManager(self)
        self.some_area.pack(fill="both", expand=1)

        ### Balloon, for pop-up help
        self.balloon = tk.Balloon(self.content)

        ### Top-level (native) menu bar
        #self.menubar = tk.ControllableMenu(self.content)
        self.configure(menu=self.menubar)

        #self.menu_balloon = Balloon(topo.tkgui.root)

        # no menubar in tile yet
        # http://news.hping.org/comp.lang.tcl.archive/4679.html

        self.__simulation_menu()
        self.__create_plots_menu()
        self.refresh_plots_menu()
        self.__help_menu()


        ### Running the simulation
        run_frame = Frame(self.content)
        run_frame.pack(side='top',fill='x',padx=4,pady=8)

        self.run_frame = run_frame

        Label(run_frame,text='Run for: ').pack(side=LEFT)

        self.run_for_var=DoubleVar()
        self.run_for_var.set(1.0)

        run_for = tk.TaggedSlider(run_frame,
                               variable=self.run_for_var,
                               tag_width=11,
                               slider_length=150,
                               bounds=(0,20000))
        self.balloon.bind(run_for,"Duration to run the simulation, e.g. 0.0500, 1.0, or 20000.")
        run_for.pack(side=LEFT,fill='x',expand=YES)
        run_for.tag.bind("<Return>",self.run_simulation)

        # When return is pressed, the TaggedSlider updates itself...but we also want to run
        # the simulation in this case.
        run_frame.optional_action=self.run_simulation

        go_button = Button(run_frame,text="Go",
                           command=self.run_simulation)
        go_button.pack(side=LEFT)

        self.balloon.bind(go_button,"Run the simulation for the specified duration.")

        self.step_button = Button(run_frame,text="Step",command=self.run_step)
        self.balloon.bind(self.step_button,"Run the simulation through the time at which the next events are processed.")
        self.step_button.pack(side=LEFT)

        self.sizeright()


    def __simulation_menu(self):
        """Add the simulation menu options to the menubar."""
        simulation_menu = ControllableMenu(self.menubar,tearoff=0)

        self.menubar.add_cascade(label='Simulation',menu=simulation_menu)

        simulation_menu.add_command(label='Run script',command=self.run_script)
        simulation_menu.add_command(label='Save script',command=self.save_script_repr)
        simulation_menu.add_command(label='Load snapshot',command=self.load_snapshot)
        simulation_menu.add_command(label='Save snapshot',command=self.save_snapshot)
        #simulation_menu.add_command(label='Reset',command=self.reset_network)
        simulation_menu.add_command(label='Test Pattern',command=self.open_test_pattern)

        simulation_menu.add_command(label='Model Editor',command=self.open_model_editor)
        simulation_menu.add_command(label='Quit',command=self.quit_topographica)



    def open_test_pattern(self):
        return open_plotgroup_panel(TestPattern)






    def __create_plots_menu(self):
        """
        Add the plot menu to the menubar, with Basic plots on the menu itself and
        others in cascades by category (the plots come from plotgroup_templates).
        """
        plots_menu = ControllableMenu(self.menubar,tearoff=0)
        self.menubar.add_cascade(label='Plots',menu=plots_menu)


    # CEBALERT: should split other menus in same way as plots (create/refresh)
    def refresh_plots_menu(self):
        plots_menu = self['Plots']
        plots_menu.delete(0,'end')

        # create menu entries, and get list of categories
        entries=OrderedDict() # keep the order of plotgroup_templates (which is also KL)
        categories = []
        for label,plotgroup in plotgroups.items():
            entries[label] = PlotsMenuEntry(plotgroup)
            categories.append(plotgroup.category)
        categories = sorted(set(categories))


        # The Basic category items appear on the menu itself.
        assert 'Basic' in categories, "'Basic' is the category for the standard Plots menu entries."
        for label,entry in entries.items():
            if entry.plotgroup.category=='Basic':
                    plots_menu.add_command(label=label,command=entry.__call__)

        categories.remove('Basic')

        plots_menu.add_separator()

        # Add the other categories to the menu as cascades, and the plots of each category to
        # their cascades.
        for category in categories:
            category_menu = ControllableMenu(plots_menu,tearoff=0)
            plots_menu.add_cascade(label=category,menu=category_menu)

            # could probably search more efficiently than this
            for label,entry in entries.items():
                if entry.plotgroup.category==category:
                    category_menu.add_command(label=label,command=entry.__call__)


        plots_menu.add_separator()

        plots_menu.add_command(label="Help",command=(lambda x=plotting_help_locations: self.open_location(x)))


    def __help_menu(self):
        """Add the help menu options."""

        help_menu = ControllableMenu(self.menubar,tearoff=0,name='help')
        self.menubar.add_cascade(label='Help',menu=help_menu)

        help_menu.add_command(label='About',command=self.new_about_window)
        help_menu.add_command(label="User Manual",
                              command=(lambda x=user_manual_locations: self.open_location(x)))

        help_menu.add_command(label="Tutorials",
                              command=(lambda x=tutorials_locations: self.open_location(x)))

        help_menu.add_command(label="Examples",
                              command=self.run_example_script)

        help_menu.add_command(label="Reference Manual",
                              command=(lambda x=reference_manual_locations: self.open_location(x)))

        help_menu.add_command(label="Topographica.org",
                              command=(lambda x=topo_www_locations: self.open_location(x)))

        help_menu.add_command(label="Python documentation",
                              command=(lambda x=python_doc_locations: self.open_location(x)))




    def quit_topographica(self,check=True,exit_status=0):
        """Quit topographica."""
        if not check or (check and tk.askyesno("Quit Topographica","Really quit?")):
            self.destroy()

            # matplotlib's tk backend starts its own Tk instances; we
            # need to close these ourselves (at least to avoid error
            # message about 'unusual termination' in Windows).
            try: # not that there should be an error, but just in case...
                import matplotlib._pylab_helpers
                for figman in matplotlib._pylab_helpers.Gcf.get_all_fig_managers():
                    figman.destroy()
            except:
                pass

            self.message("Quit selected%s" % ("; exiting" if  self.exit_on_quit else ""))

            # Workaround for obscure problem on some UNIX systems
            # as of 4/2007, probably including Fedora Core 5.
            # On these systems, if Topographica is started from a
            # bash prompt and then quit from the Tkinter GUI (as
            # opposed to using Ctrl-D in the terminal), the
            # terminal would suppress echoing of all future user
            # input.  stty sane restores the terminal to sanity,
            # but it is not clear why this is necessary.
            # For more info:
            # http://groups.google.com/group/comp.lang.python/browse_thread/thread/68d0f33c8eb2e02d
            if topo.tkgui.system_platform=="linux" and os.getenv('EMACS')!='t':
                try: os.system("stty sane")
                except: pass
            # CEBALERT: re. above. Shouldn't we be able to store the
            # output of "stty --save" before starting the gui, then
            # ensure that when the gui exits (however badly it
            # happens) run "stty saved_settings"?

            # CEBALERT: there was no call to self.master.destroy()
            if  self.exit_on_quit:
                sys.exit(exit_status)


    def run_script(self):
        """
        Dialog to run a user-selected script

        The script is exec'd in __main__.__dict__ (i.e. as if it were specified on the commandline.)
        """
        script = askopenfilename(initialdir=normalize_path(),filetypes=SCRIPT_FILETYPES)
        if script in ('',(),None): # (representing the various ways no script was selected in the dialog)
            self.messageBar.response('Run canceled')
        else:
            execfile(script,__main__.__dict__)
            self.messageBar.response('Ran ' + script)
            sim_name_from_filename(script)
            self.title(topo.sim.name)

    # CEBALERT: duplicates most of run_script()
    def run_example_script(self):


        script = askopenfilename(initialdir=topo.misc.genexamples.find_examples(),
                                 filetypes=SCRIPT_FILETYPES)

        if script in ('',(),None): # (representing the various ways no script was selected in the dialog)
            self.messageBar.response('No example opened')
        else:
            execfile(script,__main__.__dict__)
            self.messageBar.response('Ran ' + script)
            sim_name_from_filename(script)
            self.title(topo.sim.name)



    def save_script_repr(self):
        script_name = asksaveasfilename(filetypes=SCRIPT_FILETYPES,
                                        initialdir=normalize_path(),
                                        initialfile=topo.sim.basename()+"_script_repr.ty")

        if script_name:
            topo.command.save_script_repr(script_name)
            self.messageBar.response('Script saved to ' + script_name)


    def load_snapshot(self):
        """
        Dialog to load a user-selected snapshot (see topo.command.load_snapshot() ).
        """
        snapshot_name = askopenfilename(initialdir=normalize_path(),filetypes=SAVED_FILETYPES)

        if snapshot_name in ('',(),None):
            self.messageBar.response('No snapshot loaded.')
        else:
            self.messageBar.dynamicinfo('Loading snapshot (may take some time)...')
            self.update_idletasks()
            topo.command.load_snapshot(snapshot_name)
            self.messageBar.response('Loaded snapshot ' + snapshot_name)
            self.title(topo.sim.name)

        self.auto_refresh()


    def save_snapshot(self):
        """
        Dialog to save a snapshot (see topo.command.save_snapshot() ).

        Adds the file extension .typ if not already present.
        """
        snapshot_name = asksaveasfilename(filetypes=SAVED_FILETYPES,
                                          initialdir=normalize_path(),
                                          initialfile=topo.sim.basename()+".typ")

        if snapshot_name in ('',(),None):
            self.messageBar.response('No snapshot saved.')
        else:
            if not snapshot_name.endswith('.typ'):
                snapshot_name = snapshot_name + SAVED_FILE_EXTENSION

            self.messageBar.dynamicinfo('Saving snapshot (may take some time)...')
            self.update_idletasks()
            topo.command.save_snapshot(snapshot_name)
            self.messageBar.response('Snapshot saved to ' + snapshot_name)


    def auto_refresh(self, update=True):
        """
        Refresh all windows in auto_refresh_panels.

        Panels can add and remove themselves to the list; those in the list
        will have their refresh() method called whenever this console's
        autorefresh() is called.
        """
        for win in self.auto_refresh_panels:
            win.refresh(update)

        self.set_step_button_state()
        self.update_idletasks()


    ### CEBERRORALERT: why doesn't updatecommand("display=True") for an
    ### orientation preference map measurement work with the
    ### hierarchical example? I guess this is the reason I thought the
    ### updating never worked properly (or I really did break it
    ### recently - or I'm confused)...
    def refresh_activity_windows(self):
        """
        Update any windows with a plotgroup_key ending in 'Activity'.

        Used primarily for debugging long scripts that present a lot of activity patterns.
        """
        for win in self.auto_refresh_panels:
            if re.match('.*Activity$',win.plotgroup.name):
                win.refresh()
                self.update_idletasks()


    def open_model_editor(self):
        """Start the Model editor."""
        return ModelEditor(self)


    def new_about_window(self):
        win = tk.AppWindow(self)
        win.withdraw()
        win.title("About Topographica")
        text = Label(win,text=topo.about(display=False),justify=LEFT)
        text.pack(side=LEFT)
        win.deiconify()
        #self.messageBar.message('state', 'OK')

    def open_location(self, locations):
        """
        Try to open one of the specified locations in a new window of the default
        browser. See webbrowser module for more information.

        locations should be a tuple.
        """
        # CB: could have been a list. This is only here because if locations is set
        # to a string, it will loop over the characters of the string.
        assert isinstance(locations,tuple),"locations must be a tuple."

        for location in locations:
            try:
                existing_location = resolve_path(location)
                webbrowser.open(existing_location,new=2,autoraise=True)
                self.messageBar.response('Opened local file '+existing_location+' in browser.')
                return ###
            except:
                pass

        for location in locations:
            if location.startswith('http'):
                try:
                    webbrowser.open(location,new=2,autoraise=True)
                    self.messageBar.response('Opened remote location '+location+' in browser.')
                    return ###
                except:
                    pass

        self.messageBar.response("Could not open any of %s in a browser."%locations)


    # CEBALERT: need to take care of removing old messages automatically?
    # (Otherwise callers might always have to pass 'ok'.)
    def status_message(self,m):
        self.messageBar.response(m)


    def run_simulation(self,event=None): # event=None allows use as callback
        """
        Run the simulation for the duration specified in the
        'run for' taggedslider.
        """
        fduration = self.run_for_var.get()
        self.open_progress_window(timer=topo.sim.timer)
        topo.sim.run_and_time(fduration)
        self.auto_refresh()


    # CEBERRORALERT: Step button does strange things at time==0.
    # E.g. for lissom_oo_or, nothing appears to happen. For
    # hierarchical, runs to time==10.
    def run_step(self):

        if not topo.sim.events:
            # JP: step button should be disabled if there are no events,
            # but just in case...
            return

        # JPALERT: This should really use .run_and_time() but it doesn't support
        # run(until=...)
        topo.sim.run(until=topo.sim.events[0].time)
        self.auto_refresh()

    def set_step_button_state(self):
        if topo.sim.events:
            self.step_button.config(state=NORMAL)
        else:
            self.step_button.config(state=DISABLED)


    def __get_status_bar(self,i=2):
        # Hack to find appropriate status bar: Go back through frames
        # until a widget with a status bar is found, and return it.
        try:
            while True:
                f = sys._getframe(i)
                if hasattr(f,'f_locals'):
                    if 'self' in f.f_locals:
                        o = f.f_locals['self']
                        # (temporary hack til ScrolledFrame cleaned up)
                        if o.__class__.__name__!='ScrolledFrame':
                            if hasattr(o,'messageBar'):
                                return o.messageBar
                            elif hasattr(o,'status'):
                                return o.status
                    i+=1
        except:
            pass

        #print "GUI INTERNAL WARNING: failed to determine window on which to display message."
        return self.messageBar


    def open_progress_window(self,timer,title=None):
        """
        Provide a convenient link to progress bars.
        """
        stat = self.__get_status_bar()
        return stat.open_progress_window(timer=timer,sim=topo.sim)
コード例 #58
0
ファイル: tpg_gui.py プロジェクト: tkkuehn/skeleprint_ui
class Tool_Path_Generator:
    def __init__(self, top=None):
        '''This class configures and populates the toplevel window.
           top is the toplevel containing window.'''
        _bgcolor = '#e6e6e6'  # X11 color: 'gray85'
        _fgcolor = '#000000'  # X11 color: 'black'
        font11 = "-size 15 -weight normal -slant roman "  \
            "-underline 0 -overstrike 0"

        self.axial_length = DoubleVar()
        self.printbed_diameter = DoubleVar()
        self.final_diameter = DoubleVar()
        self.filament_width_og = DoubleVar()
        self.helix_angle = DoubleVar()
        self.smear_factor = DoubleVar()
        self.flow_rate = DoubleVar()
        self.uv_offset = DoubleVar()
        self.use_strong_pattern = BooleanVar()

        self.axial_length.set(200.0)
        self.printbed_diameter.set(10.0)
        self.final_diameter.set(15.0)
        self.filament_width_og.set(0.41)
        self.helix_angle.set(45.0)
        self.smear_factor.set(100.0)
        self.flow_rate.set(0.0015)
        self.uv_offset.set(32.5)
        self.use_strong_pattern.set(True)

        top.geometry("700x550")
        top.title("SkelePrint Tool Path Generator")
        top.configure(background="#e6e6e6")
        top.configure(highlightbackground="#e6e6e6")
        top.configure(highlightcolor="black")

        self.Label7 = Label(top)
        self.Label7.grid(row=0, column=0, sticky=W)
        self.Label7.configure(background="#e6e6e6")
        self.Label7.configure(font=font11)
        self.Label7.configure(foreground="#000000")
        self.Label7.configure(text='''SkelePrint Tool Path Generator''')

        self.Labelframe1 = LabelFrame(top)
        self.Labelframe1.grid(row=1, column=0, sticky=N+S)
        self.Labelframe1.configure(relief=GROOVE)
        self.Labelframe1.configure(foreground="black")
        self.Labelframe1.configure(text='''Dimensions''')
        self.Labelframe1.configure(background="#e6e6e6")
        self.Labelframe1.configure(highlightbackground="#e6e6e6")
        self.Labelframe1.configure(highlightcolor="black")

        self.axial_length_entry = Entry(self.Labelframe1)
        self.axial_length_entry.grid(row=0, column=1)
        self.axial_length_entry.configure(background="white")
        self.axial_length_entry.configure(font="TkFixedFont")
        self.axial_length_entry.configure(foreground="#000000")
        self.axial_length_entry.configure(highlightbackground="#e6e6e6")
        self.axial_length_entry.configure(highlightcolor="black")
        self.axial_length_entry.configure(insertbackground="black")
        self.axial_length_entry.configure(selectbackground="#c4c4c4")
        self.axial_length_entry.configure(selectforeground="black")
        self.axial_length_entry.configure(textvariable=self.axial_length)

        self.Label1 = Label(self.Labelframe1)
        self.Label1.grid(row=0, column=0, sticky=E)
        self.Label1.configure(activebackground="#e6e6e6")
        self.Label1.configure(activeforeground="black")
        self.Label1.configure(background="#e6e6e6")
        self.Label1.configure(foreground="#000000")
        self.Label1.configure(highlightbackground="#e6e6e6")
        self.Label1.configure(highlightcolor="black")
        self.Label1.configure(text='''Axial Length''')

        self.Label2 = Label(self.Labelframe1)
        self.Label2.grid(row=0, column=2, sticky=W)
        self.Label2.configure(activebackground="#e6e6e6")
        self.Label2.configure(activeforeground="black")
        self.Label2.configure(background="#e6e6e6")
        self.Label2.configure(disabledforeground="#e6e6e6")
        self.Label2.configure(foreground="#000000")
        self.Label2.configure(highlightbackground="#e6e6e6")
        self.Label2.configure(highlightcolor="black")
        self.Label2.configure(text='''mm''')

        self.Label3 = Label(self.Labelframe1)
        self.Label3.grid(row=1, column=0, sticky=E)
        self.Label3.configure(activebackground="#e6e6e6")
        self.Label3.configure(activeforeground="black")
        self.Label3.configure(background="#e6e6e6")
        self.Label3.configure(foreground="#000000")
        self.Label3.configure(highlightbackground="#e6e6e6")
        self.Label3.configure(highlightcolor="black")
        self.Label3.configure(text='''Printbed Diameter''')

        self.Entry2 = Entry(self.Labelframe1)
        self.Entry2.grid(row=1, column=1)
        self.Entry2.configure(background="white")
        self.Entry2.configure(font="TkFixedFont")
        self.Entry2.configure(foreground="#000000")
        self.Entry2.configure(highlightbackground="#e6e6e6")
        self.Entry2.configure(highlightcolor="black")
        self.Entry2.configure(insertbackground="black")
        self.Entry2.configure(selectbackground="#c4c4c4")
        self.Entry2.configure(selectforeground="black")
        self.Entry2.configure(textvariable=self.printbed_diameter)

        self.Label4 = Label(self.Labelframe1)
        self.Label4.grid(row=1, column=2, sticky=W)
        self.Label4.configure(activebackground="#e6e6e6")
        self.Label4.configure(activeforeground="black")
        self.Label4.configure(background="#e6e6e6")
        self.Label4.configure(foreground="#000000")
        self.Label4.configure(highlightbackground="#e6e6e6")
        self.Label4.configure(highlightcolor="black")
        self.Label4.configure(text='''mm''')

        self.Label5 = Label(self.Labelframe1)
        self.Label5.grid(row=2, column=0, sticky=E)
        self.Label5.configure(activebackground="#e6e6e6")
        self.Label5.configure(activeforeground="black")
        self.Label5.configure(background="#e6e6e6")
        self.Label5.configure(foreground="#000000")
        self.Label5.configure(highlightbackground="#e6e6e6")
        self.Label5.configure(highlightcolor="black")
        self.Label5.configure(text='''Final Print Diameter''')

        self.final_diameter_entry = Entry(self.Labelframe1)
        self.final_diameter_entry.grid(row=2, column=1)
        self.final_diameter_entry.configure(background="white")
        self.final_diameter_entry.configure(font="TkFixedFont")
        self.final_diameter_entry.configure(foreground="#000000")
        self.final_diameter_entry.configure(highlightbackground="#e6e6e6")
        self.final_diameter_entry.configure(highlightcolor="black")
        self.final_diameter_entry.configure(insertbackground="black")
        self.final_diameter_entry.configure(selectbackground="#c4c4c4")
        self.final_diameter_entry.configure(selectforeground="black")
        self.final_diameter_entry.configure(textvariable=self.final_diameter)

        self.Label6 = Label(self.Labelframe1)
        self.Label6.grid(row=2, column=2, sticky=W)
        self.Label6.configure(activebackground="#e6e6e6")
        self.Label6.configure(activeforeground="black")
        self.Label6.configure(background="#e6e6e6")
        self.Label6.configure(foreground="#000000")
        self.Label6.configure(highlightbackground="#e6e6e6")
        self.Label6.configure(highlightcolor="black")
        self.Label6.configure(text='''mm''')

        self.Entry4 = Entry(self.Labelframe1)
        self.Entry4.grid(row=3, column=1)
        self.Entry4.configure(background="white")
        self.Entry4.configure(font="TkFixedFont")
        self.Entry4.configure(foreground="#000000")
        self.Entry4.configure(highlightbackground="#e6e6e6")
        self.Entry4.configure(highlightcolor="black")
        self.Entry4.configure(insertbackground="black")
        self.Entry4.configure(selectbackground="#c4c4c4")
        self.Entry4.configure(selectforeground="black")
        self.Entry4.configure(textvariable=self.filament_width_og)

        self.Label7 = Label(self.Labelframe1)
        self.Label7.grid(row=3, column=2, sticky=W)
        self.Label7.configure(activebackground="#e6e6e6")
        self.Label7.configure(activeforeground="black")
        self.Label7.configure(background="#e6e6e6")
        self.Label7.configure(foreground="#000000")
        self.Label7.configure(highlightbackground="#e6e6e6")
        self.Label7.configure(highlightcolor="black")
        self.Label7.configure(text='''mm''')

        self.Label8 = Label(self.Labelframe1)
        self.Label8.grid(row=3, column=0, sticky=E)
        self.Label8.configure(activebackground="#e6e6e6")
        self.Label8.configure(activeforeground="black")
        self.Label8.configure(background="#e6e6e6")
        self.Label8.configure(foreground="#000000")
        self.Label8.configure(highlightbackground="#e6e6e6")
        self.Label8.configure(highlightcolor="black")
        self.Label8.configure(text='''Filament Width''')

        self.tip = Label(self.Labelframe1, width=300, height=300)

        __location__ = os.path.realpath(
            os.path.join(os.getcwd(), os.path.dirname(__file__)))

        img = Image.open(os.path.join(__location__, 'dimensions.png'))
        one = ImageTk.PhotoImage(img)

        self.tip = Label(self.Labelframe1, image=one)
        self.tip.image = one
        self.tip.configure(background="#e6e6e6")
        self.tip.grid(row=4, columnspan=3)

        self.Labelframe2 = LabelFrame(top)
        self.Labelframe2.grid(row=1, column=1, sticky=N+S)
        self.Labelframe2.configure(relief=GROOVE)
        self.Labelframe2.configure(foreground="black")
        self.Labelframe2.configure(text='''Print Properties''')
        self.Labelframe2.configure(background="#e6e6e6")
        self.Labelframe2.configure(highlightbackground="#e6e6e6")
        self.Labelframe2.configure(highlightcolor="black")

        self.Label9 = Label(self.Labelframe2)
        self.Label9.grid(row=0, column=0, sticky=E)
        self.Label9.configure(activebackground="#e6e6e6")
        self.Label9.configure(activeforeground="black")
        self.Label9.configure(background="#e6e6e6")
        self.Label9.configure(foreground="#000000")
        self.Label9.configure(highlightbackground="#e6e6e6")
        self.Label9.configure(highlightcolor="black")
        self.Label9.configure(text='''Helix Angle''')

        self.Entry5 = Entry(self.Labelframe2)
        self.Entry5.grid(row=0, column=1)
        self.Entry5.configure(background="white")
        self.Entry5.configure(font="TkFixedFont")
        self.Entry5.configure(foreground="#000000")
        self.Entry5.configure(highlightbackground="#e6e6e6")
        self.Entry5.configure(highlightcolor="black")
        self.Entry5.configure(insertbackground="black")
        self.Entry5.configure(selectbackground="#c4c4c4")
        self.Entry5.configure(selectforeground="black")
        self.Entry5.configure(textvariable=self.helix_angle)

        self.Label10 = Label(self.Labelframe2)
        self.Label10.grid(row=0, column=2, sticky=W)
        self.Label10.configure(activebackground="#e6e6e6")
        self.Label10.configure(activeforeground="black")
        self.Label10.configure(background="#e6e6e6")
        self.Label10.configure(foreground="#000000")
        self.Label10.configure(highlightbackground="#e6e6e6")
        self.Label10.configure(highlightcolor="black")
        self.Label10.configure(text='''degrees [0 - 90]''')

        self.strong_targeter_button = Radiobutton(self.Labelframe2)
        self.strong_targeter_button.grid(row=1, column=0, sticky=E)
        self.strong_targeter_button.configure(variable=self.use_strong_pattern)
        self.strong_targeter_button.configure(value=True)
        self.strong_targeter_button.configure(activebackground="#e6e6e6")
        self.strong_targeter_button.configure(activeforeground="black")
        self.strong_targeter_button.configure(background="#e6e6e6")
        self.strong_targeter_button.configure(foreground="#000000")
        self.strong_targeter_button.configure(highlightbackground="#e6e6e6")
        self.strong_targeter_button.configure(highlightcolor="black")

        self.strong_targeter_label = Label(self.Labelframe2)
        self.strong_targeter_label.grid(row=1, column=1, sticky=W)
        self.strong_targeter_label.configure(activebackground="#e6e6e6")
        self.strong_targeter_label.configure(activeforeground="black")
        self.strong_targeter_label.configure(background="#e6e6e6")
        self.strong_targeter_label.configure(foreground="#000000")
        self.strong_targeter_label.configure(highlightbackground="#e6e6e6")
        self.strong_targeter_label.configure(highlightcolor="black")
        self.strong_targeter_label.configure(text="Strong angle pattern")

        self.default_targeter_button = Radiobutton(self.Labelframe2)
        self.default_targeter_button.grid(row=2, column=0, sticky=E)
        self.default_targeter_button.configure(activebackground="#e6e6e6")
        self.default_targeter_button.configure(activeforeground="black")
        self.default_targeter_button.configure(background="#e6e6e6")
        self.default_targeter_button.configure(foreground="#000000")
        self.default_targeter_button.configure(highlightbackground="#e6e6e6")
        self.default_targeter_button.configure(highlightcolor="black")
        self.default_targeter_button.configure(
                variable=self.use_strong_pattern)
        self.default_targeter_button.configure(value=False)

        self.default_targeter_label = Label(self.Labelframe2)
        self.default_targeter_label.grid(row=2, column=1, sticky=W)
        self.default_targeter_label.configure(activebackground="#e6e6e6")
        self.default_targeter_label.configure(activeforeground="black")
        self.default_targeter_label.configure(background="#e6e6e6")
        self.default_targeter_label.configure(foreground="#000000")
        self.default_targeter_label.configure(highlightbackground="#e6e6e6")
        self.default_targeter_label.configure(highlightcolor="black")
        self.default_targeter_label.configure(text="Default angle pattern")

        self.Scale1 = Scale(self.Labelframe2)
        self.Scale1.grid(row=5, column=1, columnspan=2, sticky=S+W)
        self.Scale1.configure(activebackground="#e6e6e6")
        self.Scale1.configure(background="#e6e6e6")
        self.Scale1.configure(font="TkTextFont")
        self.Scale1.configure(foreground="#000000")
        self.Scale1.configure(from_="5.0")
        self.Scale1.configure(highlightbackground="#d9d9d9")
        self.Scale1.configure(highlightcolor="black")
        self.Scale1.configure(length="150")
        self.Scale1.configure(orient="horizontal")
        self.Scale1.configure(resolution="5.0")
        self.Scale1.configure(troughcolor="#d9d9d9")
        self.Scale1.configure(variable=self.smear_factor)

        self.Label8 = Label(self.Labelframe2)
        self.Label8.grid(row=3, column=0, sticky=E)
        self.Label8.configure(background="#e6e6e6")
        self.Label8.configure(foreground="#000000")
        self.Label8.configure(text='''Flow rate''')

        self.Entry6 = Entry(self.Labelframe2)
        self.Entry6.grid(row=3, column=1)
        self.Entry6.configure(background="white")
        self.Entry6.configure(font="TkFixedFont")
        self.Entry6.configure(foreground="#000000")
        self.Entry6.configure(highlightbackground="#e6e6e6")
        self.Entry6.configure(highlightcolor="black")
        self.Entry6.configure(insertbackground="black")
        self.Entry6.configure(selectbackground="#c4c4c4")
        self.Entry6.configure(selectforeground="black")
        self.Entry6.configure(textvariable=self.flow_rate)

        self.Label12 = Label(self.Labelframe2)
        self.Label12.grid(row=3, column=2, sticky=W)
        self.Label12.configure(activebackground="#e6e6e6")
        self.Label12.configure(activeforeground="black")
        self.Label12.configure(background="#e6e6e6")
        self.Label12.configure(foreground="#000000")
        self.Label12.configure(highlightbackground="#d9d9d9")
        self.Label12.configure(highlightcolor="black")
        self.Label12.configure(text='''cm^3 / s''')

        self.uv_label = Label(self.Labelframe2)
        self.uv_label.grid(row=4, column=0, sticky=E)
        self.uv_label.configure(activebackground="#e6e6e6")
        self.uv_label.configure(activeforeground="black")
        self.uv_label.configure(background="#e6e6e6")
        self.uv_label.configure(foreground="#000000")
        self.uv_label.configure(highlightbackground="#d9d9d9")
        self.uv_label.configure(highlightcolor="black")
        self.uv_label.configure(text="UV Distance")

        self.uv_entry = Entry(self.Labelframe2)
        self.uv_entry.grid(row=4, column=1)
        self.uv_entry.configure(background="white")
        self.uv_entry.configure(font="TkFixedFont")
        self.uv_entry.configure(foreground="#000000")
        self.uv_entry.configure(highlightbackground="#e6e6e6")
        self.uv_entry.configure(highlightcolor="black")
        self.uv_entry.configure(insertbackground="black")
        self.uv_entry.configure(selectbackground="#c4c4c4")
        self.uv_entry.configure(selectforeground="black")
        self.uv_entry.configure(textvariable=self.uv_offset)

        self.uv_label_2 = Label(self.Labelframe2)
        self.uv_label_2.grid(row=4, column=2, sticky=W)
        self.uv_label_2.configure(activebackground="#e6e6e6")
        self.uv_label_2.configure(activeforeground="black")
        self.uv_label_2.configure(background="#e6e6e6")
        self.uv_label_2.configure(foreground="#000000")
        self.uv_label_2.configure(highlightbackground="#d9d9d9")
        self.uv_label_2.configure(highlightcolor="black")
        self.uv_label_2.configure(text='''mm''')

        self.Label11 = Label(self.Labelframe2)
        self.Label11.grid(row=5, column=0, sticky=S+E)
        self.Label11.configure(activebackground="#e6e6e6")
        self.Label11.configure(activeforeground="black")
        self.Label11.configure(background="#e6e6e6")
        self.Label11.configure(foreground="#000000")
        self.Label11.configure(highlightbackground="#d9d9d9")
        self.Label11.configure(highlightcolor="black")
        self.Label11.configure(text='''Layer Height %''')

        self.Label13 = Label(self.Labelframe2)
        self.Label13.grid(row=6, columnspan=3)
        self.Label13.configure(activebackground="#f9f9f9")
        self.Label13.configure(activeforeground="black")
        self.Label13.configure(background="#e6e6e6")
        self.Label13.configure(foreground="#000000")
        self.Label13.configure(highlightbackground="#d9d9d9")
        self.Label13.configure(highlightcolor="black")
        self.Label13.configure(text='''caution: layer height % is experimental
default = 100% (ie. layer height = filament width)''')

        self.Message1 = Message(self.Labelframe2)
        self.Message1.grid(row=8, columnspan=3)
        self.Message1.configure(anchor=N)
        self.Message1.configure(background="#e6e6e6")
        self.Message1.configure(foreground="#000000")
        self.Message1.configure(highlightbackground="#e6e6e6")
        self.Message1.configure(highlightcolor="black")
        self.Message1.configure(text='''Helix Angle Conditions:
If the angle is > 90, it will be set to 90 degrees

If angle is < 0, it will be set to 0 degrees

If angle = 0, the layer will consist of a single helix printed as close \
together as possible

If angle = 90, the layer will consist of many straight lines''')

        self.tip2 = Label(self.Labelframe2, width=300, height=91)

        img2 = Image.open(os.path.join(__location__, 'theta.jpg'))
        two = ImageTk.PhotoImage(img2)

        self.tip2 = Label(self.Labelframe2, image=two)
        self.tip2.image = two
        self.tip2.configure(background="#e6e6e6")
        self.tip2.grid(row=7, columnspan=3)

        self.Label8 = Label(top)
        self.Label8.grid(row=5, columnspan=2)
        self.Label8.configure(background="#e6e6e6")
        self.Label8.configure(foreground="#000000")
        self.Label8.configure(text='''G Code file will be saved on your Desktop under:
"gcode/timestamp_skeleprint_gcode.gcode"''')

        self.Button1 = Button(top)
        self.Button1.grid(row=2, columnspan=2)
        self.Button1.configure(activebackground="#e6e6e6")
        self.Button1.configure(activeforeground="#e6e6e6")
        self.Button1.configure(background="#e6e6e6")
        self.Button1.configure(command=lambda: tpg_gui_support.tpg(
            self.axial_length.get(),
            self.filament_width_og.get(),
            self.printbed_diameter.get(),
            self.final_diameter.get(),
            self.helix_angle.get(),
            self.smear_factor.get(),
            self.flow_rate.get(),
            self.uv_offset.get(),
            self.use_strong_pattern.get()))
        self.Button1.configure(foreground="#000000")
        self.Button1.configure(highlightbackground="#e6e6e6")
        self.Button1.configure(highlightcolor="black")
        self.Button1.configure(relief=RAISED)
        self.Button1.configure(text='''Generate G Code''')

        self.menubar = Menu(top, font="TkMenuFont", bg=_bgcolor, fg=_fgcolor)
        top.configure(menu=self.menubar)