Source code for

''' Tkinter helper module.

.. codeauthor:: Yoshifumi Futaana
import abc
import tkinter as Tk

[docs]class ToplevelFrame(Tk.Toplevel): ''' Using Tk.Frame class, instance Toplevel object. :: +----- master (If given) -------------------+ | +----- ToplevelFrame -> Tk.Toplevel ----+ | | | +--- Frame -> Tk.Frame -----------+ | | | | | | | | | | +---------------------------------+ | | | +---------------------------------------+ | +-------------------------------------------+ ''' def __init__(self, Frame_cls, master=None): ''' Create the Toplevel derivative object from Frame derivative class :param Frame_cls: Derivative class of Tk.Frame. Not instance/object!! .. code-block:: python # Usual way of instance Frame class MyFrame (Tk.Frame): def __init__(self, master=None): Tk.Frame.__init__(self, master) b = Tk.Button(self, text='OK') b.pack() # For usual usage tk = Tk.Tk() tp = Tk.Toplevel(tk) f = MyFrame(tp) f.pack() tk.mainloop() # For using this class tk = Tk.Tk() tp = ToplevelFrame(MyFrame, master=tk) tk.mainloop() ''' Tk.Toplevel.__init__(self, master) tlframe = Frame_cls(self) tlframe.pack()
[docs]def functoplevel(frame_cls, master=None): ''' Return the function that returns Toplevel instance from frame class. This can be used as an argument for "command" option! ''' ff = lambda: ToplevelFrame(frame_cls, master) return ff
[docs]class PyanaFrame(Tk.Frame): ''' Abstract frame for general use. The frame is intended to specify paramters for plots. Therefore, in the bottom, two buttons "Plot" and "Close" are placed by default. You have to define the following three methods, at least - :meth:`framename`: Name of the frame - :meth:`create_widget`: All the widget, other than the buttons - :meth:`go`: Command when "Plot" button is pressed. See :ref:`how_to_gui` for details. ''' def __init__(self, master=None): Tk.Frame.__init__(self, master) self.master.title(self.framename()) = Tk.Frame(self) self.create_widget() # Main widget. All inside should be "placed" frm_okquit = Tk.Frame(self) self.btn_go = Tk.Button(frm_okquit, text='Plot', command=self.go) self.btn_go.pack(side=Tk.LEFT) self.btn_quit = Tk.Button(frm_okquit, text='Close', command=self.quit) self.btn_quit.pack(side=Tk.LEFT) frm_okquit.pack()
[docs] @abc.abstractmethod def framename(self): ''' Return a short string showing frame name ''' raise NotImplementedError('Please define ``framename``')
[docs] @abc.abstractmethod def create_widget(self): ''' Define all the widget below ```` (``Tk.Frame``). Here you may define all the widget, and connect to commands. All the widget is intended to be placed under the Tk.Frame object, named ````. ''' raise NotImplementedError('Please define ``create_widget``')
[docs] @abc.abstractmethod def go(self): ''' Go function. Called from "Go" button. ''' raise NotImplementedError('Please define ``go``')
[docs] def quit(self): ''' Quit function. Default is self.master.destroy ''' self.master.destroy()
import unittest import doctest
[docs]def doctests(): return unittest.TestSuite(( doctest.DocTestSuite(), ))
if __name__ == '__main__': unittest.main(defaultTest='doctests')