In [9]:
import tkinter as tk
from tkinter import ttk
from analytical import *
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure

In [10]:
SCT = Scatterer()
ICONST = 1
LARGE_FONT=("Verdanna", 12)

In [11]:
class BasicApp(tk.Tk):
    def __init__(self):
        self.Frame = tk.Frame 
        label = tk.Label(self.Frame,text="Scatterer Instance", font=LARGE_FONT)
        label.grid(column=1,row=1)
        self.rows=[]
        
        AddSphere = tk.Button(self.Frame, text="Add Sphere",
                              command = lambda: self.AddSphereRow() )
        AddSphere.grid(column=1,row=2)
        
        IQPlot = tk.Button(self.Frame, text="Plot",
                           command = lambda: self.Plot())
        IQPlot.grid(column=1,row=3)
        
        self.i = 0 # i is the maximimum row number
        
    def AddSphereRow(self):
        global ICONST
        temp=SphereRow(self.Frame)
        self.rows.append(temp)
        ICONST+=1
        
    def Plot(self):
        global SCT
        gen = SCT.genIQ()
        IQ = list(gen)
        print(IQ)
        TOPLEVEL = tk.Toplevel()
        TOPLEVEL.title("Plot")
        #plotting
        f = Figure(figsize=(5,5), dpi=100)
        a = f.add_subplot(111)
        a.plot(Q_RANGE,IQ)

        canvas = FigureCanvasTkAgg(f, TOPLEVEL)
        canvas.show()
        canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)

        toolbar = NavigationToolbar2TkAgg(canvas, TOPLEVEL)
        toolbar.update()
        canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)

Convinence Classes


In [12]:
class SphereRow():
   def __init__(self,frame):
        #Store 
        self.EXISTS = False
        i = ICONST
        self.i= ICONST
        self.frame = frame
        #Declare String Variables
        self.x_var = tk.DoubleVar()
        self.y_var = tk.DoubleVar()
        self.z_var = tk.DoubleVar()
        self.rho_var = tk.DoubleVar()
        self.radius_var = tk.DoubleVar()
        #X,Y,Z
        self.x = tk.Label(self.frame, text="X:", font=LARGE_FONT)
        self.x.grid(column=2, row=i)
        self.x_val = tk.Entry(self.frame, textvariable=self.x_var)
        self.x_val.grid(column=3, row=i)
        self.y = tk.Label(self.frame, text="Y:", font=LARGE_FONT)
        self.y.grid(column=4, row=i)
        self.y_val = tk.Entry(self.frame, textvariable=self.y_var)
        self.y_val.grid(column=5, row=i)
        self.z = tk.Label(self.frame, text="Z:", font=LARGE_FONT)
        self.z.grid(column=6, row=i)
        self.z_val = tk.Entry(self.frame, textvariable=self.z_var)
        self.z_val.grid(column=7, row =i)
        #Rho
        self.rho = tk.Label(self.frame, text="Rho:", font=LARGE_FONT)
        self.rho.grid(column=8, row=i)
        self.rho_val = tk.Entry(self.frame, textvariable=self.rho_var)
        self.rho_val.grid(column=9, row =i)
        #Radius
        self.radius= tk.Label(self.frame, text="Radius:", font=LARGE_FONT)
        self.radius.grid(column=10, row=i)
        self.radius_val = tk.Entry(self.frame, textvariable=self.radius_var)
        self.radius_val.grid(column=11, row =i)
        
        #Declare Buttons
        self.Confirm = tk.Button(self.frame, text= "Confirm", command =lambda: AddSphere(self))
        self.Confirm.grid(column=12, row=i)
        
        self.Delete = tk.Button(self.frame,text="Delete", command =lambda: Delete(self))
        self.Delete.grid(column=13, row=i)
        
        self.WidgetList = [self.x,self.x_val,self.y,self.y_val,self.z,self.z_val,self.rho,
                        self.rho_val,self.radius,self.radius_val,self.Confirm,self.Delete]
        
        def AddSphere(self):
            global SCT
            #store every value so entry.get() is called only once per entry feild
            Rho,Radius = self.rho_var.get(), self.radius_var.get()
            X,Y,Z = self.x_var.get(),self.y_var.get(),self.z_var.get()
            print(Rho)
            print(Radius)
            print(X)
            print(Y)
            print(Z)
            #if the shape is contained in the scatterer
            #update each value
            if self.EXISTS == False:
                mono_sphere(SCT, Rho,Radius,X,Y,Z)
                self.EXISTS = True
            else:
                SCT.shapes[self.i-1].x = X
                SCT.shapes[self.i-1].y = Y
                SCT.shapes[self.i-1].z = Z
                SCT.shapes[self.i-1].contrast = Rho
                SCT.shapes[self.i-1].r = Radius
            #if the shape isn't contained, declare a new one
            
            print(SCT.shapes[i].radius)

        def Delete(self):
            if self.EXISTS==True:
                self.EXISTS = False
                SCT.shapes[self.i].remove()
            for Widget in self.WidgetList:
                Widget.grid_remove()
            global ICONST
            ICONST -=1

In [13]:
root = BasicApp()
root.title("Pysaxs")
root.mainloop()


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-13-fba245db08d3> in <module>()
----> 1 root = BasicApp()
      2 root.title("Pysaxs")
      3 root.mainloop()

<ipython-input-11-2bc9c07ac2d0> in __init__(self)
      3     def __init__(self):
      4         self.Frame = tk.Frame
----> 5         label = tk.Label(self.Frame,text="Scatterer Instance", font=LARGE_FONT)
      6         label.grid(column=1,row=1)
      7         self.rows=[]

/usr/lib/python3.5/tkinter/__init__.py in __init__(self, master, cnf, **kw)
   2607 
   2608         """
-> 2609         Widget.__init__(self, master, 'label', cnf, kw)
   2610 
   2611 class Listbox(Widget, XView, YView):

/usr/lib/python3.5/tkinter/__init__.py in __init__(self, master, widgetName, cnf, kw, extra)
   2133             cnf = _cnfmerge((cnf, kw))
   2134         self.widgetName = widgetName
-> 2135         BaseWidget._setup(self, master, cnf)
   2136         if self._tclCommands is None:
   2137             self._tclCommands = []

/usr/lib/python3.5/tkinter/__init__.py in _setup(self, master, cnf)
   2111                 master = _default_root
   2112         self.master = master
-> 2113         self.tk = master.tk
   2114         name = None
   2115         if 'name' in cnf:

AttributeError: type object 'Frame' has no attribute 'tk'

In [ ]: