def create_parameters_frame(self, parent):
parameters = ttk.Frame(master=parent, padding=STANDARD_MARGIN)
parameters.grid(sticky='nsew')
heading = ttk.Label(parameters, text='Analysis parameters')
heading.grid(column=0, row=0, sticky='n')
for i, param in enumerate(self.parameters, start=1):
param_label = ttk.Label(parameters, text=param._name)
param_label.grid(row=i, column=0, sticky='nsew')
if type(param) == tk.BooleanVar:
param_entry = ttk.Checkbutton(parameters, variable=param)
elif hasattr(param, '_choices'):
param_entry = ttk.OptionMenu(parameters, param, param.get(),
*param._choices.keys())
else:
param_entry = ttk.Entry(parameters, textvariable=param)
param_entry.grid(row=i, column=1, sticky='nsew')
python类OptionMenu()的实例源码
def de_widget(self):
# Initialize widget
self.init_widget()
# strategy
self.strategy_label = ttk.Label(self.frame1.sliders, text = "Strategy")
self.strategy_option_menu = ttk.OptionMenu(self.frame1.sliders, self.strategy, self.strategy.get(),
*sorted(self.STRATOPT))
self.strategy_label.place(relx = 0, x = 0, y = 5, anchor = "nw")
self.strategy_option_menu.place(relx = 0, x = 70, y = 3, anchor = "nw")
# CR
self._label("Crossover probability", 2)
self._scale(0., 1., 0.01, self.CR, 2)
self._entry(self.CR, 2)
# F
self._label("Differential weight", 4)
self._scale(0., 2., 0.01, self.F, 4)
self._entry(self.F, 4)
def SetPlotWidgets(self, rowControl):
## ??? ???????
def SetPlotType(val):
self.shSpectr.PlotType = val
self.shSpectr.ShowSp()
def ChkShowTitle():
self.shSpectr.ShowTitle = showTitle.get()
self.shSpectr.ShowSp()
rowControl = self.AddLabel(rowControl, "Plot type", colsp=1)
vPlotTypes = SD.ShowSpectr.PlotTypes() #self.shSpectr
sPlotType = tk.StringVar(self.frControl)
self.omPlotType = ttk.OptionMenu(self.frControl, sPlotType, self.shSpectr.PlotType, *vPlotTypes, command=SetPlotType)
PlaceWidget(self.omPlotType, rowControl, col=1, stick='wn')
showTitle = tk.BooleanVar()
showTitle.set(self.shSpectr.ShowTitle)
self.chkshowTitle = ttk.Checkbutton(self.frControl, variable=showTitle, text="Titles", onvalue=True, offvalue=False, command=ChkShowTitle)
rowControl = PlaceWidget(self.chkshowTitle, rowControl, col=1, stick='e')
return rowControl
#return self.AddLabel(rowControl)
def test_widget_destroy(self):
var = tkinter.StringVar(self.root)
optmenu = ttk.OptionMenu(self.root, var)
name = var._name
optmenu.update_idletasks()
optmenu.destroy()
self.assertEqual(optmenu.tk.globalgetvar(name), var.get())
del var
self.assertRaises(tkinter.TclError, optmenu.tk.globalgetvar, name)
def test_initialization(self):
self.assertRaises(tkinter.TclError,
ttk.OptionMenu, self.root, self.textvar, invalid='thing')
optmenu = ttk.OptionMenu(self.root, self.textvar, 'b', 'a', 'b')
self.assertEqual(optmenu._variable.get(), 'b')
self.assertTrue(optmenu['menu'])
self.assertTrue(optmenu['textvariable'])
optmenu.destroy()
def test_widget_destroy(self):
var = tkinter.StringVar(self.root)
optmenu = ttk.OptionMenu(self.root, var)
name = var._name
optmenu.update_idletasks()
optmenu.destroy()
self.assertEqual(optmenu.tk.globalgetvar(name), var.get())
del var
self.assertRaises(tkinter.TclError, optmenu.tk.globalgetvar, name)
def test_initialization(self):
self.assertRaises(tkinter.TclError,
ttk.OptionMenu, self.root, self.textvar, invalid='thing')
optmenu = ttk.OptionMenu(self.root, self.textvar, 'b', 'a', 'b')
self.assertEqual(optmenu._variable.get(), 'b')
self.assertTrue(optmenu['menu'])
self.assertTrue(optmenu['textvariable'])
optmenu.destroy()
def test_widget_destroy(self):
var = tkinter.StringVar()
optmenu = ttk.OptionMenu(None, var)
name = var._name
optmenu.update_idletasks()
optmenu.destroy()
self.assertEqual(optmenu.tk.globalgetvar(name), var.get())
del var
self.assertRaises(tkinter.TclError, optmenu.tk.globalgetvar, name)
def test_initialization(self):
self.assertRaises(tkinter.TclError,
ttk.OptionMenu, None, self.textvar, invalid='thing')
optmenu = ttk.OptionMenu(None, self.textvar, 'b', 'a', 'b')
self.assertEqual(optmenu._variable.get(), 'b')
self.assertTrue(optmenu['menu'])
self.assertTrue(optmenu['textvariable'])
optmenu.destroy()
def test_widget_destroy(self):
var = tkinter.StringVar(self.root)
optmenu = ttk.OptionMenu(self.root, var)
name = var._name
optmenu.update_idletasks()
optmenu.destroy()
self.assertEqual(optmenu.tk.globalgetvar(name), var.get())
del var
self.assertRaises(tkinter.TclError, optmenu.tk.globalgetvar, name)
def test_initialization(self):
self.assertRaises(tkinter.TclError,
ttk.OptionMenu, self.root, self.textvar, invalid='thing')
optmenu = ttk.OptionMenu(self.root, self.textvar, 'b', 'a', 'b')
self.assertEqual(optmenu._variable.get(), 'b')
self.assertTrue(optmenu['menu'])
self.assertTrue(optmenu['textvariable'])
optmenu.destroy()
def test_widget_destroy(self):
var = tkinter.StringVar(self.root)
optmenu = ttk.OptionMenu(self.root, var)
name = var._name
optmenu.update_idletasks()
optmenu.destroy()
self.assertEqual(optmenu.tk.globalgetvar(name), var.get())
del var
self.assertRaises(tkinter.TclError, optmenu.tk.globalgetvar, name)
def test_initialization(self):
self.assertRaises(tkinter.TclError,
ttk.OptionMenu, self.root, self.textvar, invalid='thing')
optmenu = ttk.OptionMenu(self.root, self.textvar, 'b', 'a', 'b')
self.assertEqual(optmenu._variable.get(), 'b')
self.assertTrue(optmenu['menu'])
self.assertTrue(optmenu['textvariable'])
optmenu.destroy()
def test_menu(self):
items = ('a', 'b', 'c')
default = 'a'
optmenu = ttk.OptionMenu(self.root, self.textvar, default, *items)
found_default = False
for i in range(len(items)):
value = optmenu['menu'].entrycget(i, 'value')
self.assertEqual(value, items[i])
if value == default:
found_default = True
self.assertTrue(found_default)
optmenu.destroy()
# default shouldn't be in menu if it is not part of values
default = 'd'
optmenu = ttk.OptionMenu(self.root, self.textvar, default, *items)
curr = None
i = 0
while True:
last, curr = curr, optmenu['menu'].entryconfigure(i, 'value')
if last == curr:
# no more menu entries
break
self.assertNotEqual(curr, default)
i += 1
self.assertEqual(i, len(items))
# check that variable is updated correctly
optmenu.pack()
optmenu.wait_visibility()
optmenu['menu'].invoke(0)
self.assertEqual(optmenu._variable.get(), items[0])
# changing to an invalid index shouldn't change the variable
self.assertRaises(tkinter.TclError, optmenu['menu'].invoke, -1)
self.assertEqual(optmenu._variable.get(), items[0])
optmenu.destroy()
# specifying a callback
success = []
def cb_test(item):
self.assertEqual(item, items[1])
success.append(True)
optmenu = ttk.OptionMenu(self.root, self.textvar, 'a', command=cb_test,
*items)
optmenu['menu'].invoke(1)
if not success:
self.fail("Menu callback not invoked")
optmenu.destroy()
def test_menu(self):
items = ('a', 'b', 'c')
default = 'a'
optmenu = ttk.OptionMenu(None, self.textvar, default, *items)
found_default = False
for i in range(len(items)):
value = optmenu['menu'].entrycget(i, 'value')
self.assertEqual(value, items[i])
if value == default:
found_default = True
self.assertTrue(found_default)
optmenu.destroy()
# default shouldn't be in menu if it is not part of values
default = 'd'
optmenu = ttk.OptionMenu(None, self.textvar, default, *items)
curr = None
i = 0
while True:
last, curr = curr, optmenu['menu'].entryconfigure(i, 'value')
if last == curr:
# no more menu entries
break
self.assertFalse(curr == default)
i += 1
self.assertEqual(i, len(items))
# check that variable is updated correctly
optmenu.pack()
optmenu.wait_visibility()
optmenu['menu'].invoke(0)
self.assertEqual(optmenu._variable.get(), items[0])
# changing to an invalid index shouldn't change the variable
self.assertRaises(tkinter.TclError, optmenu['menu'].invoke, -1)
self.assertEqual(optmenu._variable.get(), items[0])
optmenu.destroy()
# specifying a callback
success = []
def cb_test(item):
self.assertEqual(item, items[1])
success.append(True)
optmenu = ttk.OptionMenu(None, self.textvar, 'a', command=cb_test,
*items)
optmenu['menu'].invoke(1)
if not success:
self.fail("Menu callback not invoked")
optmenu.destroy()
def frame1(self):
self.frame1 = ttk.LabelFrame(self.master, text = "Parameters", borderwidth = 2, relief = "groove")
self.frame1.place(bordermode = "outside", relwidth = 0.99, relheight = 0.21, relx = 0, x = 5, y = 5, anchor = "nw")
self.frame1.first_run = True
# function
function_label = ttk.Label(self.frame1, text = "Function")
function_option_menu = ttk.OptionMenu(self.frame1, self.function, self.function.get(),
*sorted(self.FUNCOPT))
# max_iter
max_iter_label = ttk.Label(self.frame1, text = "Maximum number of iterations")
max_iter_spinbox = Spinbox(self.frame1, from_ = 2, to_ = 9999,
increment = 1, textvariable = self.max_iter,
width = 6, justify = "right", takefocus = True)
# fps
fps_label = ttk.Label(self.frame1, text = "Delay between frames (ms)")
fps_spinbox = Spinbox(self.frame1, from_ = 1, to_ = 1000,
increment = 1, textvariable = self.interval,
width = 6, justify = "right", takefocus = True)
# seed
seed_button = ttk.Checkbutton(self.frame1, text = "Fix seed",
variable = self.fix_seed, takefocus = False)
seed_spinbox = Spinbox(self.frame1, from_ = 0, to_ = self.MAX_SEED,
increment = 1, textvariable = self.seed,
width = 6, justify = "right", takefocus = True)
# solver
solver_label = ttk.Label(self.frame1, text = "Solver")
solver_option_menu = ttk.OptionMenu(self.frame1, self.solver_name, self.solver_name.get(),
*(self.EAOPT + self.MCOPT), command = self.select_widget)
# constrain
constrain_button = ttk.Checkbutton(self.frame1, text = "Constrain",
variable = self.constrain, takefocus = False)
# Layout
function_label.place(relx = 0., x = 5, y = 5, anchor = "nw")
function_option_menu.place(relx = 0., x = 75, y = 3, anchor = "nw")
max_iter_label.place(relx = 0., x = 5, y = 30, anchor = "nw")
max_iter_spinbox.place(width = 80, relx = 0., x = 220, y = 30, anchor = "nw")
fps_label.place(relx = 0., x = 5, y = 55, anchor = "nw")
fps_spinbox.place(width = 80, relx = 0., x = 220, y = 55, anchor = "nw")
seed_button.place(relx = 0., x = 5, y = 80, anchor = "nw")
seed_spinbox.place(width = 80, relx = 0., x = 220, y = 80, anchor = "nw")
solver_label.place(relx = 0.35, x = 0, y = 5, anchor = "nw")
solver_option_menu.place(relx = 0.35, x = 50, y = 3, anchor = "nw")
constrain_button.place(relx = 0.35, x = 0, y = 80, anchor = "nw")
def test_menu(self):
items = ('a', 'b', 'c')
default = 'a'
optmenu = ttk.OptionMenu(self.root, self.textvar, default, *items)
found_default = False
for i in range(len(items)):
value = optmenu['menu'].entrycget(i, 'value')
self.assertEqual(value, items[i])
if value == default:
found_default = True
self.assertTrue(found_default)
optmenu.destroy()
# default shouldn't be in menu if it is not part of values
default = 'd'
optmenu = ttk.OptionMenu(self.root, self.textvar, default, *items)
curr = None
i = 0
while True:
last, curr = curr, optmenu['menu'].entryconfigure(i, 'value')
if last == curr:
# no more menu entries
break
self.assertNotEqual(curr, default)
i += 1
self.assertEqual(i, len(items))
# check that variable is updated correctly
optmenu.pack()
optmenu.wait_visibility()
optmenu['menu'].invoke(0)
self.assertEqual(optmenu._variable.get(), items[0])
# changing to an invalid index shouldn't change the variable
self.assertRaises(tkinter.TclError, optmenu['menu'].invoke, -1)
self.assertEqual(optmenu._variable.get(), items[0])
optmenu.destroy()
# specifying a callback
success = []
def cb_test(item):
self.assertEqual(item, items[1])
success.append(True)
optmenu = ttk.OptionMenu(self.root, self.textvar, 'a', command=cb_test,
*items)
optmenu['menu'].invoke(1)
if not success:
self.fail("Menu callback not invoked")
optmenu.destroy()
def test_menu(self):
items = ('a', 'b', 'c')
default = 'a'
optmenu = ttk.OptionMenu(self.root, self.textvar, default, *items)
found_default = False
for i in range(len(items)):
value = optmenu['menu'].entrycget(i, 'value')
self.assertEqual(value, items[i])
if value == default:
found_default = True
self.assertTrue(found_default)
optmenu.destroy()
# default shouldn't be in menu if it is not part of values
default = 'd'
optmenu = ttk.OptionMenu(self.root, self.textvar, default, *items)
curr = None
i = 0
while True:
last, curr = curr, optmenu['menu'].entryconfigure(i, 'value')
if last == curr:
# no more menu entries
break
self.assertNotEqual(curr, default)
i += 1
self.assertEqual(i, len(items))
# check that variable is updated correctly
optmenu.pack()
optmenu.wait_visibility()
optmenu['menu'].invoke(0)
self.assertEqual(optmenu._variable.get(), items[0])
# changing to an invalid index shouldn't change the variable
self.assertRaises(tkinter.TclError, optmenu['menu'].invoke, -1)
self.assertEqual(optmenu._variable.get(), items[0])
optmenu.destroy()
# specifying a callback
success = []
def cb_test(item):
self.assertEqual(item, items[1])
success.append(True)
optmenu = ttk.OptionMenu(self.root, self.textvar, 'a', command=cb_test,
*items)
optmenu['menu'].invoke(1)
if not success:
self.fail("Menu callback not invoked")
optmenu.destroy()
def SetBaseWidgets(self, rowControl):
## ???? (?????, ??????, ??????) ???????
def SetSizeLabel():
self.lblSpSize.configure(text="Order: " + str(self.shSpectr.Size) + ', ' + str(self.shSpectr.numSp + 1))
def SetSpParameters():
self.scIndex.configure(to=self.toIndex())
self.scVMarksize.configure(to=int(self.shSpectr.numZ-1))
self.scColor.configure(to=int(self.shSpectr.numZ-1))
SetSizeLabel()
self.SetIndexLabel()
self.SetLabelVectorColor()
self.SetLabelMSVector()
def ChangeBaseSet(val):
self.shSpectr.BaseSet=val
self.shSpectr.SetForm()
SetSpParameters()
def ChangeSize(val):
iSize = int(float(val))
self.shSpectr.Size= iSize
self.shSpectr.SetForm()
SetSpParameters()
def ChangeIndex(val):
ind = int(float(val))
self.shSpectr.ChangeIndex(ind*self.shSpectr.NumPlots)
self.SetIndexLabel()
rowControl = self.AddLabel(rowControl, "Base", colsp=1)
vBaseSet = self.shSpectr.BaseSets()
sBaseSet = tk.StringVar(self.frControl)
self.omBaseSet = ttk.OptionMenu(self.frControl, sBaseSet, self.shSpectr.BaseSet, *vBaseSet, command=ChangeBaseSet)
rowControl = PlaceWidget(self.omBaseSet, rowControl, col=1, stick='wn')
self.lblSpSize = ttk.Label(self.frControl)
PlaceWidget(self.lblSpSize, rowControl)
SetSizeLabel()
self.scSize = ttk.Scale(self.frControl, orient='horizontal', length=self.colWidth2, from_=self.shSpectr.SizeRange[0], to=self.shSpectr.SizeRange[1], value=self.shSpectr.Size, command=ChangeSize)
rowControl = PlaceWidget(self.scSize, rowControl, col=1, colspan=1, stick='ne')
rowControl = self.AddLabel(rowControl, "Index", self.lblColor, 1)
self.lblSpIndex = ttk.Label(self.frControl) #, text="Index: 0"
PlaceWidget(self.lblSpIndex, rowControl)
self.scIndex = ttk.Scale(self.frControl, orient='horizontal', length=self.colWidth2, from_=0, to=self.toIndex(), value=int(self.shSpectr.iSpectr/self.shSpectr.NumPlots), command=ChangeIndex)
rowControl = PlaceWidget(self.scIndex, rowControl, col=0, colspan=2, stick='ne')
self.SetIndexLabel()
return self.AddLabel(rowControl)
def SetMarkerWidgets(self, rowControl):
## Markers
def ChangeMarkform(val='o'):
self.shSpectr.mark_style = SD.dicMarkStyle()[val]
self.shSpectr.ShowSp()
def SetMarksizeLabel():
self.lblMarksize.configure(text="Size: " + str(round(self.scMarksize.get(), 1)))
def SetMarksize(val=0):
self.shSpectr.ChangeMarksize(float(val))
SetMarksizeLabel()
def SetVmarksize(val=0, ini=False):
ms = float(val)
if not ini:
self.shSpectr.SetMarksize(ms)
self.SetLabelMSVector()
def CheckMarksize():
self.shSpectr.useVectorMarksize = useVectorMarksize.get()
self.shSpectr.ShowSp()
def InverseMarksize():
self.shSpectr.inverseMarksize = inverseMarksize.get()
self.shSpectr.ShowSp()
rowControl = self.AddLabel(rowControl, "Markers", colsp=1)
vsMarkStyle = list(SD.dicMarkStyle().keys())
sMarkStyle = tk.StringVar(self.frControl)
self.omMarkStyle = ttk.OptionMenu(self.frControl, sMarkStyle, "circle", *vsMarkStyle, command=ChangeMarkform)
rowControl = PlaceWidget(self.omMarkStyle, rowControl, col=1, stick='wn')
self.lblMarksize = ttk.Label(self.frControl)
PlaceWidget(self.lblMarksize, rowControl)
self.scMarksize = ttk.Scale(self.frControl, orient='horizontal', length=self.colWidth2,
from_=self.shSpectr.MarksizeRange[0], to=self.shSpectr.MarksizeRange[1], value=self.shSpectr.msize, command=SetMarksize)
rowControl = PlaceWidget(self.scMarksize, rowControl, col=1, stick='ne')
SetMarksizeLabel()
useVectorMarksize = tk.BooleanVar()
useVectorMarksize.set(self.shSpectr.useVectorMarksize)
self.chkMarkSize = ttk.Checkbutton(self.frControl, variable=useVectorMarksize, onvalue=True, offvalue=False, command=CheckMarksize)
rowControl = PlaceWidget(self.chkMarkSize, rowControl, col=0, stick='ew')
self.scVMarksize = ttk.Scale(self.frControl, orient='horizontal', length=100, from_=0, to=self.shSpectr.numZ-1, value=self.shSpectr.iMarksize, command=SetVmarksize)
rowControl = PlaceWidget(self.scVMarksize, rowControl, col=1, stick='ew')
SetVmarksize(val=self.shSpectr.iMarksize, ini=True)
inverseMarksize = tk.BooleanVar()
inverseMarksize.set(self.shSpectr.inverseMarksize)
self.chkInverseMarksize = ttk.Checkbutton(self.frControl, text="Reverse", variable=inverseMarksize, onvalue=True, offvalue=False, command=InverseMarksize)
rowControl = PlaceWidget(self.chkInverseMarksize, rowControl, col=0, colspan=2, stick='wn')
return self.AddLabel(rowControl)
def SetMaskWidgets(self, rowControl):
## ????? ????????????
def ChangeMaskFunction(val='Var'):
self.shSpectr.MaskFunction = val
self.shSpectr.ShowSp()
def SetMask(val=0, ini=False):
self.shSpectr.iMask = int(float(val))
self.chkMask.configure(text='Use: ' + str(int(self.shSpectr.iMask)))
if not ini:
self.shSpectr.ShowSp()
def CheckMask():
self.shSpectr.useMask = useMask.get()
self.shSpectr.ShowSp()
def InverseMask():
self.shSpectr.inverseMask = inverseMask.get()
self.shSpectr.ShowSp()
rowControl = self.AddLabel(rowControl, "Mask", colsp=1)
vsMaskFunction = ['Std', 'Mean']
sMaskFunction = tk.StringVar(self.frControl)
self.omMaskFunction = ttk.OptionMenu(self.frControl, sMaskFunction, "Std", *vsMaskFunction, command=ChangeMaskFunction)
rowControl = PlaceWidget(self.omMaskFunction, rowControl, col=1, stick='w')
useMask = tk.IntVar()
useMask.set(False)
self.chkMask = ttk.Checkbutton(self.frControl, variable=useMask, onvalue=True, offvalue=False, command=CheckMask)
rowControl = PlaceWidget(self.chkMask, rowControl, col=0, stick='ew')
self.scMask = ttk.Scale(self.frControl, orient='horizontal', length=self.colWidth2, from_=0, to=100, value=self.shSpectr.iMask, command=SetMask)
rowControl = PlaceWidget(self.scMask, rowControl, col=1, stick='ne')
SetMask(val=self.shSpectr.iMask, ini=True)
inverseMask = tk.BooleanVar()
inverseMask.set(self.shSpectr.inverseMask)
self.chkInverseMask = ttk.Checkbutton(self.frControl, text="Inverse", variable=inverseMask, onvalue=True, offvalue=False, command=InverseMask)
rowControl = PlaceWidget(self.chkInverseMask, rowControl, col=0, colspan=2, stick='wn')
return self.AddLabel(rowControl)