/usr/share/pyshared/openopt/kernel/GUI.py is in python-openopt 0.38+svn1589-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | # sometimes Tkinter is not installed
TkinterIsInstalled = True
import platform
if platform.python_version()[0] == '2':
# Python2
try:
from Tkinter import Tk, Toplevel, Button, Entry, Menubutton, Label, Frame, StringVar, DISABLED, ACTIVE
except:
TkinterIsInstalled = False
else:
# Python3
try:
from tkinter import Tk, Toplevel, Button, Entry, Menubutton, Label, Frame, StringVar, DISABLED, ACTIVE
except:
TkinterIsInstalled = False
from threading import Thread
from openopt import __version__ as ooversion
from setDefaultIterFuncs import BUTTON_ENOUGH_HAS_BEEN_PRESSED, USER_DEMAND_EXIT
from ooMisc import killThread
from runProbSolver import finalShow
def manage(p, *args, **kwargs):
p.isManagerUsed = True
if not TkinterIsInstalled: p.err('Tkinter is not installed. If you have Linux you could try using "apt-get install python-tk"')
# expected args are (solver, start) or (start, solver) or one of them
p._args = args
p._kwargs = kwargs
for arg in args:
if type(arg) == str or hasattr(arg, '__name__'): p.solver = arg
elif arg in (0, 1, True, False): start = arg
else: p.err('Incorrect argument for manage()')
start = kwargs.pop('start', True)
if 'solver' in kwargs.keys(): p.solver = kwargs['solver']
# root
root = Tk()
p.GUI_root = root
# Title
#root.wm_title('OpenOpt ' + ooversion)
p.GUI_buttons = {}
""" Buttons """
# OpenOpt label
Frame(root).pack(ipady=4)
Label(root, text=' OpenOpt ' + ooversion + ' ').pack()
Label(root, text=' Solver: ' + (p.solver if isinstance(p.solver, str) else p.solver.__name__) + ' ').pack()
Label(root, text=' Problem: ' + p.name + ' ').pack()
#TODO: use Menubutton
#Statistics
# stat = StringVar()
# stat.set('')
# Statistics = Button(root, textvariable = stat, command = lambda: invokeStatistics(p))
# cw = Entry(root)
#
#
# b = Button(root, text = 'Evaluate!', command = lambda: invokeCommand(cw))
# cw.pack(fill='x', side='left')
# b.pack(side='right')
# Run
t = StringVar()
t.set(" Run ")
RunPause = Button(root, textvariable = t, command = lambda: invokeRunPause(p))
Frame(root).pack(ipady=8)
RunPause.pack(ipady=15)
p.GUI_buttons['RunPause'] = RunPause
p.statusTextVariable = t
# Enough
def invokeEnough():
p.userStop = True
p.istop = BUTTON_ENOUGH_HAS_BEEN_PRESSED
if hasattr(p, 'stopdict'): p.stopdict[BUTTON_ENOUGH_HAS_BEEN_PRESSED] = True
p.msg = 'button Enough has been pressed'
if p.state == 'paused':
invokeRunPause(p, isEnough=True)
else:
RunPause.config(state=DISABLED)
Enough.config(state=DISABLED)
Frame(root).pack(ipady=8)
Enough = Button(root, text = ' Enough! ', command = invokeEnough)
Enough.config(state=DISABLED)
Enough.pack()
p.GUI_buttons['Enough'] = Enough
# Exit
def invokeExit():
p.userStop = True
p.istop = USER_DEMAND_EXIT
if hasattr(p, 'stopdict'): p.stopdict[USER_DEMAND_EXIT] = True
# however, the message is currently unused
# since openopt return r = None
p.msg = 'user pressed Exit button'
root.destroy()
Frame(root).pack(ipady=8)
Button(root, text=" Exit ", command = invokeExit).pack(ipady=15)
""" Start main loop """
state = 'paused'
if start:
Thread(target=invokeRunPause, args=(p, )).start()
root.mainloop()
#finalShow(p)
""" Handle result """
if hasattr(p, 'tmp_result'):
r = p.tmp_result
delattr(p, 'tmp_result')
else:
r = None
""" Return """
return r
def invokeRunPause(p, isEnough=False):
try:
import pylab
except:
if p.plot:
p.warn('to use graphics you should have matplotlib installed')
p.plot = False
if isEnough:
p.GUI_buttons['RunPause'].config(state=DISABLED)
if p.state == 'init':
p.probThread = Thread(target=doCalculations, args=(p, ))
p.state = 'running'
p.statusTextVariable.set(' Pause ')
p.GUI_buttons['Enough'].config(state=ACTIVE)
p.GUI_root.update_idletasks()
p.probThread.start()
elif p.state == 'running':
p.state = 'paused'
if p.plot: pylab.ioff()
p.statusTextVariable.set(' Run ')
p.GUI_root.update_idletasks()
elif p.state == 'paused':
p.state = 'running'
if p.plot:
pylab.ion()
p.statusTextVariable.set(' Pause ')
p.GUI_root.update_idletasks()
def doCalculations(p):
try:
p.tmp_result = p.solve(*p._args, **p._kwargs)
except killThread:
if p.plot:
if hasattr(p, 'figure'):
#p.figure.canvas.draw_drawable = lambda: None
pylab.ioff()
pylab.close('all')
#def invokeStatistics(p):
def invokeCommand(cw):
exec(cw.get())
|