[Tkinter-discuss] Entry Widget - Setting a Default Text - continuted

Godson Gera godson.g at gmail.com
Tue Jan 1 13:28:49 CET 2008


On Jan 1, 2008 3:34 PM, Amit Finkler <amit.finkler at gmail.com> wrote:

>  Hi,
>
>
>  I tried setting up the following GUI:
>
>
>  import Numeric, time
> from Tkinter import *
> import tkFileDialog
>
> def exec_2D():
>         """Main execution function. Takes input values from user, i.e.
> magnetic field and current values, filename, series resistance and runs a
> two-dimensional scan, line scans over the current for each magnetic field
> value."""
>     yoko_gpib = int(YokoAdr.get())
>     mag_gpib = int(MagAdr.get())
>     dmm_gpib = int(DMMAdr.get())
>     Rs       = float(Rs_entr.get())
>     mag_coef = float(Mg_entr.get())
>     StartI   = float(Ie_init.get())*Rs
>     EndI     = float(Ie_finl.get())*Rs
>     StepI    = float(Ie_step.get())*Rs
>     StartH   = float(He_init.get())/mag_coef
>     EndH     = float(He_finl.get())/mag_coef
>     StepH    = float(He_step.get())/mag_coef
>
> def RampYoko():
>
>     yoko_gpib = int(YokoAdr.get())
>     Rs        = float(Rs_entr.get())
>     StartI    = float(Ie_init.get())*Rs
>     StepI     = float(Ie_step.get())*Rs
>
> def RampMag():
>
>     mag_gpib  = int(MagAdr.get())
>     mag_coef = float(Mg_entr.get())
>     StartH    = float(He_init.get())/mag_coef
>     StepH     = float(He_step.get())/mag_coef
>
> def cooldown():
>     """Cooldown procedure. Call subprocedure of miniprograms to run a
> cooldown measurement"""
>
>     lake_gpib = int(LakeAdr.get())
>     dmm_gpib       = int(DMMAdr.get())
>     yoko_gpib      = int(YokoAdr.get())
>     Rs             = float(Rs_entr.get())
>     StartI         = float(Ie_init.get())*Rs
>
> def IV_DC():
>     """DC IV procedure. Call subprocedure dc_scan of miniprograms module
> to run an IV measurement"""
>     yoko_gpib = int(YokoAdr.get())
>     dmm_gpib = int(DMMAdr.get())
>     Rs       = float(Rs_entr.get())
>     StartI   = float(Ie_init.get())*Rs
>     EndI     = float(Ie_finl.get())*Rs
>     StepI    = float(Ie_step.get())*Rs
>
> def Mag_DC():
>     """DC Magnetic field dependence procedure. CAll subprocedure
> mag_field_dc_scan of miniprograms module to run a mag measurement"""
>
>     mag_gpib = int(MagAdr.get())
>     dmm_gpib = int(DMMAdr.get())
>     mag_coef = float(Mg_entr.get())
>     StartH   = float(He_init.get())/mag_coef
>     EndH     = float(He_finl.get())/mag_coef
>     StepH    = float(He_step.get())/mag_coef
>
> def file_save_as(event):
>     """ Simple function, whose input is a mouse button click and its,
> opening a dialog box asking the user for a filename to save the data in and
> in return prints out a label to the right of the button which states the
> path of this file. Mostly serves as an indicator to the user where he/she
> saves his/her file."""
>     global fn
>     fn  = tkFileDialog.asksaveasfilename()
>     FilenameDisplay.configure(text = fn,fg = 'white', bg = 'black')
>
> def query_Yokogawa(event):
>     """ Real-time query of the current output of the Yokogawa power
> supply. Input is a mouse button click. Output is the current output
> displayed to the right of that button. Standard procedure really. """
>
>     yokogawa_gpib = int(YokoAdr.get())
>
> def query_temperature(event):
>     """ Real-time query of the current temperature in the bottom of the
> rod as measured by the Lakeshore 330 temperature controller. Input is a
> mouse button click. Output is the current temperature displayed to the right
> of that button. Standard procedure really. """
>
>
>
> def query_temperature2(event):
>     """ Real-time query of the current temperature in the bottom of the
> rod as measured by the Lakeshore 330 temperature controller. Input is a
> mouse button click. Output is the current temperature displayed to the right
> of that button. Standard procedure really. """
>
>
>
> def query_field(event):
>     """ Real-time query of the current external magnetic field in play.
> Input is a mouse button click. Output is the current magnetic field
> displayed to the right of that button. Standard procedure really. """
>
>
> ##########################################################
> #                                                        #
> #                     END OF FUNCTIONS                   #
> #                                                        #
> ##########################################################
>
> # Define main windows and their subframes
> win              = Tk()
> win.title('Control Panel')
>
> win2             = Tk()
> win2.title('Configuration and Monitoring')
>
> f                = Frame(win, bd = 2, relief = 'groove')
> f2               = Frame(win2, bd = 2, relief = 'groove')
>
> # Define variables and their default values
>
> I_init       = StringVar()
> H_init       = StringVar()
> I_finl       = StringVar()
> H_finl       = StringVar()
> I_step       = StringVar()
> H_step       = StringVar()
> R_ser        = IntVar()
> M_coef       = IntVar()
> R_ser.set('10000')
> M_coef.set('1119')
>
> Yoko_adr     = IntVar()
> Mag_adr      = IntVar()
> DMM_adr      = IntVar()
> Lake_adr     = IntVar()
> Lake2_adr    = IntVar()
> Yoko_adr.set('2')
> Mag_adr.set('10')
> DMM_adr.set('22')
> Lake_adr.set('12')
> Lake2_adr.set('1')
>
>
>
> # Variable input boxes from user
>
> Ie_init       = Entry(f, textvariable = I_init)
> He_init       = Entry(f, textvariable = H_init)
> Ie_finl       = Entry(f, textvariable = I_finl)
> He_finl       = Entry(f, textvariable = H_finl)
> Ie_step       = Entry(f, textvariable = I_step)
> He_step       = Entry(f, textvariable = H_step)
> Rs_entr       = Entry(f, textvariable = R_ser)
> Mg_entr       = Entry(f, textvariable = M_coef)
> Il_init       = Label(f, text         = 'Initial/set current [A]')
> Hl_init       = Label(f, text         = 'Initial field [Gauss]')
> Il_finl       = Label(f, text         = 'Final current [A]')
> Hl_finl       = Label(f, text         = 'Final field [Gauss]')
> Il_step       = Label(f, text         = 'Current step[A]')
> Hl_step       = Label(f, text         = 'Field step[Gauss]')
> Rs_labl       = Label(f, text         = 'Series Resistance [Ohm]', fg =
> 'magenta')
> Mg_labl       = Label(f, text         = 'Magnet Coef. [Gauss/A]', fg =
> 'magenta')
>
> YokoAdr       = Entry(f2, textvariable = Yoko_adr)
> MagAdr        = Entry(f2, textvariable = Mag_adr)
> DMMAdr        = Entry(f2, textvariable = DMM_adr)
> LakeAdr       = Entry(f2, textvariable = Lake_adr)
> Lake2Adr      = Entry(f2, textvariable = Lake2_adr)
> YokoAdrLabel  = Label(f2, text = 'Yoko address')
> MagAdrLabel   = Label(f2, text = 'Magnet address')
> DMMAdrLabel   = Label(f2, text = 'DMM address')
> LakeAdrLabel  = Label(f2, text = 'Lakeshore (Rod) address')
> Lake2AdrLabel = Label(f2, text = 'Lakeshore (Cryostat) address')
>
>
>
> # Place input boxes and labels on a grid
>
> YokoAdrLabel.grid(row  = 0, column = 0)
> YokoAdr.grid(row       = 1, column = 0)
> MagAdrLabel.grid(row   = 2, column = 0)
> MagAdr.grid(row        = 3, column = 0)
> DMMAdrLabel.grid(row   = 4, column = 0)
> DMMAdr.grid(row        = 5, column = 0)
> LakeAdrLabel.grid(row  = 6, column = 0)
> LakeAdr.grid(row       = 7, column = 0)
> Lake2AdrLabel.grid(row = 8, column = 0)
> Lake2Adr.grid(row      = 9, column = 0)
>
> Il_init.grid(row = 0, column = 0)
> Hl_init.grid(row = 0, column = 2)
> Ie_init.grid(row = 1, column = 0)
> He_init.grid(row = 1, column = 2)
> Il_finl.grid(row = 2, column = 0)
> Hl_finl.grid(row = 2, column = 2)
> Ie_finl.grid(row = 3, column = 0)
> He_finl.grid(row = 3, column = 2)
> Il_step.grid(row = 4, column = 0)
> Hl_step.grid(row = 4, column = 2)
> Ie_step.grid(row = 5, column = 0)
> He_step.grid(row = 5, column = 2)
> Rs_labl.grid(row = 6, column = 0)
> Rs_entr.grid(row = 7, column = 0)
> Mg_labl.grid(row = 6, column = 2)
> Mg_entr.grid(row = 7, column = 2)
>
> # Define execute 2D scan button
> RunFrame             = Frame(win, bd = 2, relief = 'groove')
> Run2D                = Button(RunFrame)
> Run2D.configure(text = 'Run 2D', fg = 'blue', command = exec_2D)
> Run2D.pack(side = LEFT)
> RunCooldown          = Button(RunFrame)
> RunCooldown.configure(text = 'Cooldown', fg = 'blue', command = cooldown)
> RunCooldown.pack(side = LEFT)
> RunIVDC              = Button(RunFrame)
> RunIVDC.configure(text = 'IV-DC', fg = 'blue', command = IV_DC)
> RunIVDC.pack(side = LEFT)
> RunRampYoko          = Button(RunFrame)
> RunRampYoko.configure(text = 'Ramp Yoko', fg = 'blue', command = RampYoko)
> RunRampYoko.pack(side = LEFT)
> RunRampMag           = Button(RunFrame)
> RunRampMag.configure(text = 'Ramp Field', fg = 'blue', command = RampMag)
> RunRampMag.pack(side = LEFT)
> RunMagDC              = Button(RunFrame)
> RunMagDC.configure(text = 'Mag-DC', fg = 'blue', command = Mag_DC)
> RunMagDC.pack(side = LEFT)
>
> # Define Filename Frame
>
> FilenameFrame = Frame(win, bd = 2, relief = 'groove')
> Filename      = Button(FilenameFrame)
> Filename.configure(text = 'Save in file...')
> Filename.bind("<Button-1>",file_save_as)
> Filename.pack(side=LEFT)
> FilenameDisplay = Label(FilenameFrame)
> FilenameDisplay.pack(side = LEFT)
>
> # Monitor Frame
>
> MonitorFrame  = Frame(win2, bd = 2, relief = 'groove')
>
> QueryField    = Button(MonitorFrame)
> QueryField.configure(text = 'Query Field', fg = 'DarkViolet')
> QueryField.bind("<Button-1>", query_field)
> PrintField    = Label(MonitorFrame)
> QueryField.grid(row = 0, column = 0)
> PrintField.grid(row = 0, column = 1)
>
> QueryLake     = Button(MonitorFrame)
> QueryLake.configure(text = 'Query Rod Temperature', fg = 'DarkViolet')
> QueryLake.bind("<Button-1>", query_temperature)
> PrintLake     = Label(MonitorFrame)
> QueryLake.grid(row = 1, column = 0)
> PrintLake.grid(row = 1, column = 1)
>
> QueryLake2    = Button(MonitorFrame)
> QueryLake2.configure(text = 'Query Cryostat Temperature', fg =
> 'DarkViolet')
> QueryLake2.bind("<Button-1>", query_temperature2)
> PrintLake2    = Label(MonitorFrame)
> QueryLake2.grid(row = 2, column = 0)
> PrintLake2.grid(row = 2, column = 1)
>
> QueryYoko     = Button(MonitorFrame)
> QueryYoko.configure(text = 'Query Yokogawa', fg = 'DarkViolet')
> QueryYoko.bind("<Button-1>", query_Yokogawa)
> PrintYoko     = Label(MonitorFrame)
> QueryYoko.grid(row = 3, column = 0)
> PrintYoko.grid(row = 3, column = 1)
>
> # Pack everything together
>
> # Window 1 packing
> f.pack()
> FilenameFrame.pack()
> RunFrame.pack()
>
> # Window 2 packing
> f2.pack()
> MonitorFrame.pack()
>
> Two windows open, but in window number 2 I set some default text into 5
> fields and they don't appear. If, however, I change "win2" to "win" in line
> 100 (the definition of "f2" frame), all the 5 default numbers appear in
> their respective widgets.
>
>
>  Any idea why this happens?
>
>
>  Thanks,
>
>
>  Amit.
>
>
> Hi Amit,

There should be only one instance of Tk() in your Tkinter Application (if I
remember correctly).

So if you want to create additional windows use Toplevel() instead of Tk().
Toplevel takes optional parent window argument like Toplevel(win)

so line 96 should look like

win2=Toplevel()

HTH

-- 
Godson Gera,
http://godson.in
Python Tkinter India.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tkinter-discuss/attachments/20080101/ddd2cc15/attachment-0001.htm 


More information about the Tkinter-discuss mailing list