How to make a gui software¶
Toolkit¶
The simplest way of wrapping CUI is using Tkinter. It is a powerful GUI toolkit, and the advantage is the needs of no installation.
I also tried easygui, but slightly too less for me. It is easy, but multiple window is not supported well. Also, it was announced in 2013 that the support is finished.
Template¶
Single GUI software¶
You may refer to irfpy.util.tk.PyanaFrame
as a start point.
Sample GUI program is
### Need for the template
import Tkinter as Tk
import irfpy.util.tk
### These imports are needed for "go" function.
import dateutil.parser
import matplotlib.pyplot as plt
import vima_extra_emae
class Frame(irfpy.util.tk.PyanaFrame):
def __init__(self, master=None):
irfpy.util.tk.PyanaFrame.__init__(self, master)
def framename(self):
return 'vima_extra_emae'
def create_widget(self):
frm = self.mf # All the widget is made under self.mf
timefield_lf = Tk.LabelFrame(frm, text='Time to plot')
timefield_lf.grid(row=1, column=1, columnspan=2, padx=5, pady=5)
self.timefield = Tk.Entry(timefield_lf)
self.timefield.delete(0, Tk.END)
self.timefield.insert(0, '2007-10-09T23:30:00')
self.timefield.pack()
frm.pack() # Widget is shown now.
def go(self):
t0 = dateutil.parser.parse(self.timefield.get())
print t0
plt.ion()
vima_extra_emae.execplot(t0)
def main():
root = Tk.Tk()
f = Frame(root)
f.pack()
root.mainloop()
if __name__ == '__main__':
main()
This sample is a short version in vima_extra_emae_ui
script in irfpy.aspera project.
If you run the script from python, you can see a single window that can specify the time of the plot.
Collection of GUI¶
GUI for collection of single GUI programs can also be made.
A template follows. It comes from apps_130622_veximaextra.vima_extra_ui.py
.
''' Interface using TKinter
'''
import logging
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
from irfpy.util.future import OrderedDict
import datetime
import dateutil.parser
import functools
import Tkinter as Tk
import matplotlib.pyplot as plt
import irfpy.util.tk
import vima_extra_emae_ui
import vima_extra_et_ui
class Frame(Tk.Frame):
def __init__(self, master=None):
Tk.Frame.__init__(self, master)
self.master.title('vima_extra_tkinter')
self.choices = OrderedDict([
['-' * 20 + ' Select the plot type' + '-' * 20, (lambda: None)],
['Energy-time diagram of VEX IMAEXTRA for H+, O+, and rest', irfpy.util.tk.functoplevel(vima_extra_et_ui.Frame)],
['Mass separated 2D plots (Ene-Az-El-Mass) using VEX IMAEXTRA', irfpy.util.tk.functoplevel(vima_extra_emae_ui.Frame)],
])
choicekeys = tuple(self.choices.keys())
self.optionmenu_value = Tk.StringVar()
self.optionmenu_value.set(choicekeys[0])
optionmenu = Tk.OptionMenu(self, self.optionmenu_value, *choicekeys)
optionmenu.grid(row=0, column=0, columnspan=4, padx=5, pady=5)
self.b_plot = Tk.Button(self, text='Select', command=self.plot)
self.b_plot.grid(row=2, column=0, columnspan=2, padx=5, pady=5)
self.b_quit = Tk.Button(self, text='Quit', command=self.quit)
self.b_quit.grid(row=2, column=2, columnspan=2, padx=5, pady=5)
def plot(self):
plottype = self.optionmenu_value.get()
logging.info('Plottype= %s' % plottype)
self.choices[plottype]()
def main():
root = Tk.Tk()
f = Frame(root)
f.pack()
root.mainloop()
if __name__ == '__main__':
main()
You can only change the contents self.choices
, then you may get different
set of the GUI softwares starting from a single window.
Todo
The class should be more genralized and to be “template” class.