Skip to content
Snippets Groups Projects
interface.py 10.47 KiB
from tkinter import *
from copy import deepcopy
import pygame as pyg
from functools import partial
from definition import Plate, Photon
from math import pi
from matplotlib.figure import Figure

#Define the number of plates in our system
def number_elements(root):
    v = StringVar(root)
    v.set(1)
    entry = Entry(root, textvariable = v, width=2)
    entry.config(font = ('Bahnschrift SemiLight','25'), bg = '#cccccc', fg = '#000000' )
    title = Label(root, text = "Choose number of elements", font = ('Bahnschrift Semibold','30'), fg = '#000000', bg = '#ffffff', relief = 'raised')
    title.grid(column = 2, row = 2)
    entry.grid(column = 2, row = 3)
    return v

#Define the initial states of the photon
def initial_photon(root):
    title = Label(root, text = "Initial coefficients", font = ('Bahnschrift Semibold','30'), fg = '#000000', bg = '#ffffff', relief = 'raised')
    title.grid(column = 5, row = 2, columnspan = 5)
    state0 = StringVar(root)
    state0.set(0)
    entry0 = Entry(root, textvariable = state0, width=2)
    entry0.config(font = ('Bahnschrift SemiLight','25'), bg = '#cccccc', fg = '#000000' )
    entry0.grid(column = 5, row = 3)
    label0 = Label(root, text = "|0>", font = ('Bahnschrift SemiLight','30'), fg = '#000000', bg = '#ffffff')
    label0.grid(column = 6, row = 3)
    labelplus = Label(root, text = "+", font = ('Bahnschrift SemiLight','30'), fg = '#000000', bg = '#ffffff')
    labelplus.grid(column = 7, row = 3)
    state1 = StringVar(root)
    state1.set(0)
    entry1 = Entry(root, textvariable = state1, width=2)
    entry1.config(font = ('Bahnschrift SemiLight','25'), bg = '#cccccc', fg = '#000000' )
    entry1.grid(column = 8, row = 3)
    label1 = Label(root, text = "|1>", font = ('Bahnschrift SemiLight','30'), fg = '#000000', bg = '#ffffff')
    label1.grid(column = 9, row = 3)
    return state0, state1

#Show the current properties of the plates while defining them
def display(plate,window):
    size = len(plate)
    number_title = Label(window, text = 'Number', font = ('Bahnschrift Semibold','25'), fg = '#ffffff', bg = '#aaaaaa', relief= 'groove')
    number_title.grid(column = 10, row = 0)
    type_title = Label(window, text = 'Type', font = ('Bahnschrift Semibold','25'), fg = '#ffffff', bg = '#aaaaaa', relief= 'groove')
    type_title.grid(column = 11, row = 0)
    theta_title = Label(window, text = 'Theta', font = ('Bahnschrift Semibold','25'), fg = '#ffffff', bg = '#aaaaaa', relief= 'groove')
    theta_title.grid(column = 12, row = 0)
    delta_title = Label(window, text = 'Delta', font = ('Bahnschrift Semibold','25'), fg = '#ffffff', bg = '#aaaaaa', relief= 'groove')
    delta_title.grid(column = 13, row = 0)
    orientation_title = Label(window, text = 'Orientation', font = ('Bahnschrift Semibold','25'), fg = '#ffffff', bg = '#aaaaaa', relief= 'groove')
    orientation_title.grid(column = 14, row = 0)
    for i in range(size):
        number = Label(window, text = str(i+1), font = ('Bahnschrift SemiBold Condensed','20'), fg = '#000000', bg = '#ffffff')
        number.grid(column = 10, row = i+1)
        str_element = StringVar(window)
        str_element.set('0')
        str_element = plate[i].element
        current_element = Label(window, text = str_element, font = ('Bahnschrift SemiBold Condensed','20'), fg = '#000000', bg = '#ffffff')
        current_element.grid(column = 11, row = i+1)
        str_theta = StringVar(window)
        str_theta.set('0')
        str_theta = str(plate[i].theta*180/pi) 
        current_theta = Label(window, text = str_theta, font = ('Bahnschrift SemiBold Condensed','20'), fg = '#000000', bg = '#ffffff')
        current_theta.grid(column = 12, row = i+1)
        str_delta = StringVar(window)
        str_delta.set('0')
        str_delta = str(plate[i].delta*180/pi) 
        current_delta = Label(window, text = str_delta, font = ('Bahnschrift SemiBold Condensed','20'), fg = '#000000', bg = '#ffffff')
        current_delta.grid(column = 13, row = i+1)
        str_orientation = StringVar(window)
        str_orientation.set('0')
        str_orientation = plate[i].orientation 
        current_orientation = Label(window, text = str_orientation, font = ('Bahnschrift SemiBold Condensed','20'), fg = '#000000', bg = '#ffffff')
        current_orientation.grid(column = 14, row = i+1)

#Defining the properties of the plates
def define_elements(window,size):
    Number = [i for i in range(1,size+1)]
    Type = ('Linear Retarder', 'Linear Polarizer', 'Circular Polarizer')
    Inverse_Type = {'Linear Retarder' : 'LR', 'Linear Polarizer' : 'LP', 'Circular Polarizer' : 'CP'}
    Orientation = ('None', 'Right', 'Left')
    number_title = Label(window, text = 'Number', font = ('Bahnschrift Semibold','30'), fg = '#000000', bg = '#ffffff', relief= 'raised')
    number_title.grid(column = 0, row = 0)
    type_title = Label(window, text = 'Type', font = ('Bahnschrift Semibold','30'), fg = '#000000', bg = '#ffffff', relief= 'raised')
    type_title.grid(column = 1, row = 0)
    theta_title = Label(window, text = 'Theta', font = ('Bahnschrift Semibold','30'), fg = '#000000', bg = '#ffffff', relief= 'raised')
    theta_title.grid(column = 2, row = 0)
    delta_title = Label(window, text = 'Delta', font = ('Bahnschrift Semibold','30'), fg = '#000000', bg = '#ffffff', relief= 'raised')
    delta_title.grid(column = 3, row = 0)
    orientation_title = Label(window, text = 'Orientation', font = ('Bahnschrift Semibold','30'), fg = '#000000', bg = '#ffffff', relief= 'raised')
    orientation_title.grid(column = 4, row = 0)
    global number
    number = StringVar(window)
    number.set(Number[0])
    om_number = OptionMenu(window, number, *Number)
    om_number.config(font = ('Bahnschrift SemiLight','25'), bg = '#cccccc', fg = '#000000')
    om_number.grid(column = 0, row = 1)
    global type
    type = StringVar(window)
    type.set(Type[0])
    om_type = OptionMenu(window, type, *Type)
    om_type.config(font = ('Bahnschrift SemiLight','25'), bg = '#cccccc', fg = '#000000' )
    om_type.grid(column = 1, row = 1)
    global orientation
    orientation = StringVar(window)
    orientation.set(Orientation[0])
    om_orientation = OptionMenu(window, orientation, *Orientation)
    om_orientation.config(font = ('Bahnschrift SemiLight','25'), bg = '#cccccc', fg = '#000000' )
    om_orientation.grid(column = 4, row = 1)
    global theta
    theta = StringVar(window)
    theta.set(0)
    entry_theta = Entry(window, textvariable = theta, width=3)
    entry_theta.config(font = ('Bahnschrift SemiLight','25'), bg = '#cccccc', fg = '#000000' )
    entry_theta.grid(column = 2, row = 1)
    global delta
    delta = StringVar(window)
    delta.set(0)
    entry_delta = Entry(window, textvariable = delta, width=3)
    entry_delta.config(font = ('Bahnschrift SemiLight','25'), bg = '#cccccc', fg = '#000000')
    entry_delta.grid(column = 3, row = 1)
    def give_plate():
        global number
        global type
        global theta
        global delta
        global orientation
        global plate
        new_plate = Plate(Inverse_Type[type.get()],float(theta.get())*pi/180, float(delta.get())*pi/180, orientation.get())
        print(new_plate.element, new_plate.theta, new_plate.delta, new_plate.orientation)
        plate[int(number.get())-1] = new_plate
        display(plate,window)
    button = Button(window, fg="#ffffff",text= "Validate", command=give_plate, font = ('Bahnschrift SemiLight','20','bold'), relief = 'raised', bg = 'black')
    button.grid(column = 4, row = 6)
    button_quit = Button(window, text="Finish", fg="#ffffff", command=window.destroy, font = ('Bahnschrift SemiLight','20','bold'), relief = 'raised', bg = 'black')
    button_quit.grid(column = 0, row = 6)
    #return (Inverse_Type[type.get()],theta.get(), delta.get(), orientation.get())

class Time:
    def __init__(self, k, photon, window):
        self.k = k
        self.photon = photon
        self.window = window
    def bloch_sphere(self):
        (self.photon).representation(self.k)
    def do(self):
        state_in_k = Button(self.window, text="State", fg="#ffffff", command=self.bloch_sphere, font = ('Bahnschrift SemiLight','20','bold'), relief = 'raised', bg = 'black')
        state_in_k.grid(column = 2*self.k, row = 0, pady = 20)

#Simulation of the optical path
def optical_path(window, photon):
    length = len(photon.state0)
    for i in range(length):
        time = Time(i,photon,window)
        time.do()       
        if i != length-1:
            plate_number = Label(window, text = 'Plate' + str(i+1), font = ('Bahnschrift Semibold','15'), fg = '#000000', bg = '#ffffff', relief = 'raised')
            plate_number.grid(column = 2*i+1, row = 0)

#Global software
def graphical_grid_init():
    root = Tk()
    root.geometry("+150+20")
    root.config(bg = '#ffffff')
    root.title('PhotoniCS')
    titre = Label(root, text = "PhotoniCS", font = ('Bahnschrift SemiBold Condensed','60'), fg = '#000000', bg = '#bbbbbb', relief= 'groove')
    titre.grid(column = 4, row = 0)
    button = Button(root, text="Quit", fg="#ffffff", command=quit, font = ('Bahnschrift SemiLight','20','bold'), relief = 'raised', bg = 'black')
    button.grid(column = 0, row = 6)
    global v
    v = number_elements(root)
    global plate
    global state0
    global state1
    state0, state1 = initial_photon(root)
    "pyg.mixer.init()"
    def define():
            #bg_music= pyg.mixer.Sound("bg.wav")
            #bg_music.play()
            #pyg.mixer.music.set_volume(0.5)
            window = Toplevel()
            window.config(bg = '#ffffff')
            window.geometry('+100+350')
            global plate
            plate = [Plate('LR') for i in range(int(v.get()))]
            define_elements(window,int(v.get()))
    button = Button(root, fg="#ffffff",text= "Define", command=define, font = ('Bahnschrift SemiLight','20','bold'), relief = 'raised', bg = 'black')
    button.grid(column = 2, row = 6)
    def simulate():
            #bg_music= pyg.mixer.Sound("bg.wav")
            #bg_music.play()
            #pyg.mixer.music.set_volume(0.5)
            photon = Photon(float(state0.get()),float(state1.get()))
            global plate
            for i in range(int(v.get())):
                photon.gate(plate[i])
            window = Toplevel()
            window.config(bg = '#ffffff')
            optical_path(window,photon)
            window.geometry('+150+20')
            #pyg.mixer.quit()
    button_simulate = Button(root, fg="#ffffff",text= "Simulate", command=simulate, font = ('Bahnschrift SemiLight','20','bold'), relief = 'raised', bg = 'black')
    button_simulate.grid(column = 7, row = 6)
    root.mainloop()

graphical_grid_init()