Beispiel #1
0
class SearchHelper:
    def __init__(self, searchframe):
        self.frame = searchframe

        # keyword entry
        largefont = ('Veranda', 24)
        self.ent_keyword = Entry(self.frame,
                                 width=40,
                                 relief='raised',
                                 font=largefont,
                                 bd=1)
        # todo <Return> and entry is not empty call search()

        self.but_search = Button(
            self.frame,
            text='Search',
            width=15,
            state='disable',
            font="Veranda 16",
            command=lambda: self.frame.search(
                'http://cbrown686-test.apigee.net/cyberapi/articles?q=keywordtitlebody&title='
                + self.ent_keyword.get() + '&body=' + self.ent_keyword.get()))

        self.var = IntVar()
        self.var.set(0)
        self.check_filter = Checkbutton(self.frame,
                                        text="Advanced Filter",
                                        onvalue=1,
                                        offvalue=0,
                                        variable=self.var,
                                        command=self.filter_op,
                                        font="Veranda 16")
        calltipwindow.createToolTip(
            self.check_filter, "Click here for options to narrow your search")

        calltipwindow.createToolTip(
            self.ent_keyword, "Enter a word or phrase here to search by.")
        self.ent_keyword.bind('<Escape>', self.clear_text)
        self.ent_keyword.bind(
            '<Key>', lambda event: self.callenable(event, 'DefaultSearch'))

        if self.var.get():
            self.frame.searchButton(None)

        # filter stuff
        self.appearing_label = Label(searchframe,
                                     text='Appearing In:',
                                     background='#282828',
                                     font=15,
                                     foreground='#5DE0DC')
        self.box_value = StringVar()
        self.box = Combobox(searchframe, textvariable=self.box_value)
        calltipwindow.createToolTip(
            self.appearing_label, "Select where you want us to search "
            "for your provided search phrase.")
        calltipwindow.createToolTip(
            self.box, "Select where you want us to search "
            "for your provided search phrase.")
        self.box['values'] = ('Default', 'Title', 'Body', 'URL')
        self.box.current(0)
        self.box.bind('<<ComboboxSelected>>', self.setbaseurl)

        # author
        self.author_label = Label(searchframe,
                                  text='Author:',
                                  background='#282828',
                                  font=15,
                                  foreground='#5DE0DC')
        self.author_entry = Entry(searchframe,
                                  width=22,
                                  bd=2,
                                  background='#9A9A9A')
        calltipwindow.createToolTip(
            self.author_label,
            "Enter an author's first and/or last name (not case-sensitive).")
        calltipwindow.createToolTip(
            self.author_entry,
            "Enter an author's first and/or last name (not case-sensitive).")

        # subjectivity
        self.fsub_label = Label(searchframe,
                                text='Subjectivity:',
                                background='#282828',
                                font=15,
                                foreground='#5DE0DC')
        calltipwindow.createToolTip(
            self.fsub_label,
            "Choose an option here if you only want to see articles"
            " that are more objectively or subjectively written")
        self.var2 = IntVar()
        self.var2.set(1)
        self.fsub_nv = Radiobutton(searchframe,
                                   text="Don't Care",
                                   variable=self.var2,
                                   value=1,
                                   background='#282828',
                                   foreground='#5DE0DC')
        calltipwindow.createToolTip(
            self.fsub_nv,
            "Select this if you want all articles returned regarless of how they are written."
        )
        self.fsub_gt = Radiobutton(searchframe,
                                   text='More Subjective',
                                   variable=self.var2,
                                   value=2,
                                   background='#282828',
                                   foreground='#5DE0DC')
        calltipwindow.createToolTip(
            self.fsub_gt,
            "Select this if you only want articles that are more subjectively written."
        )
        self.fsub_lt = Radiobutton(searchframe,
                                   text='More Objective',
                                   variable=self.var2,
                                   value=3,
                                   background='#282828',
                                   foreground='#5DE0DC')

        calltipwindow.createToolTip(
            self.fsub_lt,
            "Select this if you only want articles that are more objectively written."
        )
        # date
        self.fD_label = Label(searchframe,
                              text='Date:',
                              background='#282828',
                              font=15,
                              foreground='#5DE0DC')
        self.fD_format = Label(searchframe,
                               text='00/00/0000',
                               background='#282828',
                               foreground='#BBBBBB')
        self.fD_format.configure(foreground='grey')
        self.fD_beinlab = Label(searchframe,
                                text='From:',
                                background='#282828',
                                foreground='#BBBBBB')
        self.fD_endlab = Label(searchframe,
                               text='To:',
                               background='#282828',
                               foreground='#BBBBBB')
        self.fD_ent = Entry(searchframe, width=10, bd=2, background='#9A9A9A')
        self.fD_ent.insert('end', '01/01/0001')
        self.fD_ent2 = Entry(searchframe, width=10, bd=2, background='#9A9A9A')
        self.fD_ent2.insert('end', strftime('%m/%d/%Y'))

        calltipwindow.createToolTip(
            self.fD_label,
            "Narrow your results to articles published in the dates here.")
        calltipwindow.createToolTip(
            self.fD_format,
            "Narrow your results to articles published in the dates here.")
        calltipwindow.createToolTip(
            self.fD_beinlab,
            "Narrow your results to articles published in the dates here.")
        calltipwindow.createToolTip(
            self.fD_endlab,
            "Narrow your results to articles published in the dates here.")
        calltipwindow.createToolTip(self.fD_ent, "Enter Start Date here.")
        calltipwindow.createToolTip(self.fD_ent2, "Enter End Date here.")

        # filter placements
        offset = 100
        self.appearing_label.place(x=400, y=380 + offset)

        # appearing pick
        self.box.place(x=510, y=380 + offset)

        # author label
        self.author_label.place(x=400, y=405 + offset)

        # author entry
        self.author_entry.place(x=510, y=405 + offset)

        # subjectivity
        self.fsub_label.place(x=400, y=430 + offset)
        self.fsub_nv.place(x=510, y=430 + offset)
        self.fsub_gt.place(x=510, y=455 + offset)
        self.fsub_lt.place(x=510, y=480 + offset)

        # date
        self.fD_label.place(x=400, y=505 + offset)
        self.fD_format.place(x=445, y=507 + offset)
        self.fD_beinlab.place(x=510, width=45, y=505 + offset)
        self.fD_ent.place(x=555, width=65, y=505 + offset)
        self.fD_endlab.place(x=630, y=505 + offset)
        self.fD_ent2.place(x=660, width=65, y=505 + offset)

        # buttons
        self.but_search.place(relx=.505, rely=.6, anchor=W)

        # ENTRY BOX for keyword
        self.ent_keyword.place(relx=.5, rely=.5, anchor=CENTER)

        # check button
        self.check_filter.place(relx=.495, rely=.6, relheight=.059, anchor=E)

        self.hidefilters()

    #filter options populate uppon check box of Advanced search option
    def filter_op(self):
        if self.var.get() is 1:
            self.showfilters()
        else:
            self.hidefilters()

    def resetsearch(self):
        self.ent_keyword.destroy()
        self.but_search.destroy()
        self.check_filter.destroy()

        self.appearing_label.destroy()
        self.box.destroy()
        self.author_label.destroy()
        self.author_entry.destroy()
        self.fsub_label.destroy()
        self.fsub_nv.destroy()
        self.fsub_gt.destroy()
        self.fsub_lt.destroy()
        self.fD_label.destroy()
        self.fD_format.destroy()
        self.fD_ent.destroy()
        self.fD_beinlab.destroy()
        self.fD_endlab.destroy()
        self.fD_ent2.destroy()
        self.__init__(self.frame)

    def hidefilters(self):
        self.appearing_label.lower()
        self.box.lower()
        self.author_label.lower()
        self.author_entry.lower()
        self.fsub_label.lower()
        self.fsub_nv.lower()
        self.fsub_gt.lower()
        self.fsub_lt.lower()
        self.fD_label.lower()
        self.fD_format.lower()
        self.fD_ent.lower()
        self.fD_beinlab.lower()
        self.fD_endlab.lower()
        self.fD_ent2.lower()

    def showfilters(self):
        self.appearing_label.lift()
        self.box.lift()
        self.author_label.lift()
        self.author_entry.lift()
        self.fsub_label.lift()
        self.fsub_nv.lift()
        self.fsub_gt.lift()
        self.fsub_lt.lift()
        self.fD_label.lift()
        self.fD_format.lift()
        self.fD_ent.lift()
        self.fD_beinlab.lift()
        self.fD_endlab.lift()
        self.fD_ent2.lift()

    def showsearch(self):
        self.ent_keyword.lift()
        self.but_search.lift()
        self.check_filter.lift()

        if self.var.get():
            self.showfilters()

    def hidesearch(self):
        self.ent_keyword.lower()
        self.but_search.lower()
        self.check_filter.lower()

    def setbaseurl(self, event):
        if self.box.current() is 0:
            self.but_search.config(command=lambda: self.frame.search(
                'http://cbrown686-test.apigee.net/cyberapi/articles?q=keywordtitlebody&title='
                + self.ent_keyword.get() + '&body=' + self.ent_keyword.get()))
        elif self.box.current() is 1:
            self.but_search.config(command=lambda: self.frame.search(
                'http://cbrown686-test.apigee.net/cyberapi/articles?q=keywordtitle&title='
                + self.ent_keyword.get()))
        elif self.box.current() is 2:
            self.but_search.config(command=lambda: self.frame.search(
                'http://cbrown686-test.apigee.net/cyberapi/articles?q=bodyonly&body='
                + self.ent_keyword.get()))
        elif self.box.current() is 3:
            self.but_search.config(command=lambda: self.frame.search(
                'http://cbrown686-test.apigee.net/cyberapi/articles?q=uri&uripath='
                + self.ent_keyword.get()))

    def addurlfilters(self, url):
        if self.var.get():
            au = self.author_entry.get()
            au = au.replace(' ', '+')
            # var2 is the state of the radio check button
            if self.var2.get() == 2:
                url = url + '&author=' + au + '&sub=gt&sdate=' + self.fD_ent.get(
                ) + '&edate=' + self.fD_ent2.get()
                # print(url)
            elif self.var2.get() == 3:
                url = url + '&author=' + au + '&sub=gt&sdate=' + self.fD_ent.get(
                ) + '&edate=' + self.fD_ent2.get()
            else:
                url = url + '&author=' + au + '&sub=&sdate=' + self.fD_ent.get(
                ) + '&edate=' + self.fD_ent2.get()
        else:
            url = url + '&author=&sub=&sdate=01/01/0001&edate=' + strftime(
                '%m/%d/%Y')

        return url

    # Hitting escape when editing the ENTRY box will clear it and disable the search button from being able to be used.
    def clear_text(self):
        self.ent_keyword.delete(0, 'end')
        self.but_search.configure(state='disable')

    def callenable(self, event, searchtype):
        self.frame.after(100, lambda: self.enablesearch(event, searchtype))

    # event bind when Return is entered after a title keyword is entered will enable the search button.
    def enablesearch(self, event, searchtype):
        string = ''
        if searchtype == 'DefaultSearch':
            string = self.ent_keyword.get()
        if string.strip() != '':
            self.but_search.configure(state='normal')
        else:
            self.but_search.configure(state='disabled')
class Window(object):
    """"This class creates a GUI using the built in python libary tkinter"""

    def __init__(self, window):
        self.window = window

        self.check = False

        self.animateval = False

        titlel = Label(window, text="Evolutionary Spatial Games", height=3)
        titlel.grid(row=0, column=0, rowspan=2)

        self.cellularAutomata = Canvas(window, height=600, width=600, background="blue")
        self.cellularAutomata.grid(row=2, column=0, rowspan="20")

        l2 = Label(window, text="Payoff matrix:", width=16)
        l2.grid(row=3, column=1)

        l3 = Label(window, text="Cell size: ", width=16)
        l3.grid(row=6, column=1)

        l8 = Label(window, text="Moore Neighbourhood: ", width=22)
        l8.grid(row=7, column=1)

        l9 = Label(window, text="Von Nuemann Neighbourhood: ", width=26)
        l9.grid(row=8, column=1)

        l4 = Label(window, text="Initial Distribution: ", width=16)
        l4.grid(row=10, column=1)

        l9 = Label(window, text="Fixed boundary (A): ", width=26)
        l9.grid(row=11, column=1)

        l9 = Label(window, text="Reflective boundary:  ", width=26)
        l9.grid(row=12, column=1)

        l9 = Label(window, text="Periodic boundary: ", width=26)
        l9.grid(row=13, column=1)

        la = Label(window, text="Count (A|B|C): ", width=16)
        la.grid(row=16, column=1)

        l5 = Label(window, text="Iterations: ", width=16)
        l5.grid(row=17, column=1)

        b1 = Button(window, text="Draw", command=self.draw_command)
        b1.grid(row=19, column=1)

        self.b2 = Button(window, text="Start", command=self.begin_command)
        self.b2.grid(row=19, column=2)

        self.e1 = Scale(
            window, width=8, orient=HORIZONTAL, from_=2, to=3, label="Strategies"
        )
        self.e1.grid(row=0, column=1)

        self.e1.bind("<ButtonRelease-1>", self.change_entry)

        self.li = Label(window, text="B invades A: ", width=16)
        self.li.grid(row=14, column=1)

        self.ival = IntVar()
        self.iv = Checkbutton(window, variable=self.ival)
        self.iv.grid(row=14, column=2)

        self.ld = Label(window, text="Dynamic", width=16)
        self.ld.grid(row=15, column=1)

        self.dyval = IntVar()
        self.dyval.set(1)
        self.dy = Checkbutton(window, variable=self.dyval)
        self.dy.grid(row=15, column=2)

        self.e2 = IntVar()
        self.e2 = Entry(window, textvariable=self.e2, width=6)
        self.e2.grid(row=3, column=2)

        self.e3 = IntVar()
        self.e3 = Entry(window, textvariable=self.e3, width=6)
        self.e3.grid(row=3, column=3)

        self.e4 = IntVar()
        self.e4 = Entry(window, textvariable=self.e4, width=6)
        self.e4.grid(row=4, column=2)

        self.e5 = IntVar()
        self.e5 = Entry(window, textvariable=self.e5, width=6)
        self.e5.grid(row=4, column=3)

        self.cellsize = IntVar()
        self.cellsize.set(8)
        self.cellsize = Entry(window, textvariable=self.cellsize, width=6)
        self.cellsize.grid(row=6, column=2)

        self.p1 = DoubleVar()
        self.p1 = Entry(window, textvariable=self.p1, width=6)
        self.p1.grid(row=10, column=2)

        self.p2 = DoubleVar()
        self.p2 = Entry(window, textvariable=self.p2, width=6)
        self.p2.grid(row=10, column=3)

        self.neighbourE = IntVar()
        self.neighbourE.set(1)
        self.moore = Radiobutton(window, variable=self.neighbourE, value=1)
        self.moore.grid(row=7, column=2)

        self.nuemann = Radiobutton(window, variable=self.neighbourE, value=2)
        self.nuemann.grid(row=8, column=2)

        self.boundaryvar = IntVar()
        self.boundaryvar.set(2)
        self.fixed = Radiobutton(window, variable=self.boundaryvar, value=1)
        self.fixed.grid(row=11, column=2)

        self.reflective = Radiobutton(window, variable=self.boundaryvar, value=2)
        self.reflective.grid(row=12, column=2)

        self.periodic = Radiobutton(window, variable=self.boundaryvar, value=3)
        self.periodic.grid(row=13, column=2)

        self.a1 = Listbox(window, width=4, height=1)
        self.a1.grid(row=16, column=2)

        self.a2 = Listbox(window, width=4, height=1)
        self.a2.grid(row=16, column=3)

        self.i1 = Listbox(window, width=4, height=1)
        self.i1.grid(row=17, column=2)

    def draw_command(self):
        self.cellularAutomata.delete("all")
        self.count = 0
        self.i1.delete(0, END)
        self.i1.insert(END, self.count)
        try:
            self.b3.destroy()
            self.b2 = Button(window, text="Start", command=self.begin_command)
            self.b2.grid(row=19, column=2)
        except AttributeError:
            pass
        try:
            if self.e1.get() == 2:
                matrix = [
                    [self.e2.get(), self.e3.get()],
                    [self.e4.get(), self.e5.get()],
                ]
                self.SpatialGame = spatialGame(
                    600,
                    600,
                    self.cellsize.get(),
                    [self.p1.get(), self.p2.get()],
                    self.e1.get(),
                    matrix,
                    self.ival.get(),
                    self.neighbourE.get(),
                    self.boundaryvar.get(),
                    self.dyval.get(),
                )
            if self.e1.get() == 3:
                matrix = [
                    [self.e2.get(), self.e3.get(), self.e6.get()],
                    [self.e4.get(), self.e5.get(), self.e7.get()],
                    [self.e8.get(), self.e9.get(), self.e10.get()],
                ]
                self.SpatialGame = spatialGame(
                    600,
                    600,
                    self.cellsize.get(),
                    [self.p1.get(), self.p2.get(), self.p3.get()],
                    self.e1.get(),
                    matrix,
                    self.ival.get(),
                    self.neighbourE.get(),
                    self.boundaryvar.get(),
                    self.dyval.get(),
                )
            self.cells = self.SpatialGame.cells
            for x in range(0, self.SpatialGame.width):
                for y in range(0, self.SpatialGame.height):
                    if self.cells[x][y] == 2:
                        square_coords = (
                            x * self.SpatialGame.cell_size,
                            y * self.SpatialGame.cell_size,
                            x * self.SpatialGame.cell_size + self.SpatialGame.cell_size,
                            y * self.SpatialGame.cell_size + self.SpatialGame.cell_size,
                        )
                        self.cellularAutomata.create_rectangle(
                            square_coords, fill="red", outline="red"
                        )
                    if self.SpatialGame.cells[x][y] == 3:
                        square_coords = (
                            x * self.SpatialGame.cell_size,
                            y * self.SpatialGame.cell_size,
                            x * self.SpatialGame.cell_size + self.SpatialGame.cell_size,
                            y * self.SpatialGame.cell_size + self.SpatialGame.cell_size,
                        )
                        self.cellularAutomata.create_rectangle(
                            square_coords, fill="pink", outline="pink"
                        )
        except ValueError:
            self.cellularAutomata.create_text(
                300,
                300,
                fill="White",
                font="Times 20 bold",
                text="Your probability distribution must add to 1.",
            )

    def begin_command(self):
        self.animateval = True
        self.animate()

    def next(self):
        self.cellularAutomata.delete("all")
        self.SpatialGame.run_rules()
        self.cells = self.SpatialGame.cells
        self.count = self.count + 1
        self.i1.delete(0, END)
        self.i1.insert(END, self.count)
        self.b2.destroy()
        self.b3 = Button(window, text="Stop", command=self.stop_command)
        self.b3.grid(row=19, column=2)
        self.animateval = True
        for x in range(0, self.SpatialGame.width):
            for y in range(0, self.SpatialGame.height):
                if self.cells[x][y] == 2:
                    square_coords = (
                        x * self.SpatialGame.cell_size,
                        y * self.SpatialGame.cell_size,
                        x * self.SpatialGame.cell_size + self.SpatialGame.cell_size,
                        y * self.SpatialGame.cell_size + self.SpatialGame.cell_size,
                    )
                    self.cellularAutomata.create_rectangle(
                        square_coords, fill="red", outline="red"
                    )
                if self.cells[x][y] == 4:
                    square_coords = (
                        x * self.SpatialGame.cell_size,
                        y * self.SpatialGame.cell_size,
                        x * self.SpatialGame.cell_size + self.SpatialGame.cell_size,
                        y * self.SpatialGame.cell_size + self.SpatialGame.cell_size,
                    )
                    self.cellularAutomata.create_rectangle(
                        square_coords, fill="green", outline="green"
                    )
                if self.cells[x][y] == 5:
                    square_coords = (
                        x * self.SpatialGame.cell_size,
                        y * self.SpatialGame.cell_size,
                        x * self.SpatialGame.cell_size + self.SpatialGame.cell_size,
                        y * self.SpatialGame.cell_size + self.SpatialGame.cell_size,
                    )
                    self.cellularAutomata.create_rectangle(
                        square_coords, fill="yellow", outline="yellow"
                    )
                if self.cells[x][y] == 3:
                    square_coords = (
                        x * self.SpatialGame.cell_size,
                        y * self.SpatialGame.cell_size,
                        x * self.SpatialGame.cell_size + self.SpatialGame.cell_size,
                        y * self.SpatialGame.cell_size + self.SpatialGame.cell_size,
                    )
                    self.cellularAutomata.create_rectangle(
                        square_coords, fill="pink", outline="pink"
                    )
                if self.cells[x][y] == 6:
                    square_coords = (
                        x * self.SpatialGame.cell_size,
                        y * self.SpatialGame.cell_size,
                        x * self.SpatialGame.cell_size + self.SpatialGame.cell_size,
                        y * self.SpatialGame.cell_size + self.SpatialGame.cell_size,
                    )
                    self.cellularAutomata.create_rectangle(
                        square_coords, fill="purple", outline="purple"
                    )
        self.a1.delete(0, END)
        self.a1.insert(END, self.SpatialGame.stratA)
        self.a2.delete(0, END)
        self.a2.insert(END, self.SpatialGame.stratB)
        try:
            self.a3.delete(0, END)
            self.a3.insert(END, self.SpatialGame.stratC)
        except:
            pass

    def change_entry(self, event):
        if self.e1.get() == 3 and self.check == False:
            self.check = True
            self.e6 = IntVar()
            self.e6 = Entry(window, textvariable=self.e6, width=6)
            self.e6.grid(row=3, column=4)
            self.e7 = IntVar()
            self.e7 = Entry(window, textvariable=self.e7, width=6)
            self.e7.grid(row=4, column=4)
            self.e8 = IntVar()
            self.e8 = Entry(window, textvariable=self.e8, width=6)
            self.e8.grid(row=5, column=2)
            self.e9 = IntVar()
            self.e9 = Entry(window, textvariable=self.e9, width=6)
            self.e9.grid(row=5, column=3)
            self.e10 = IntVar()
            self.e10 = Entry(window, textvariable=self.e10, width=6)
            self.e10.grid(row=5, column=4)
            self.p3 = DoubleVar()
            self.p3 = Entry(window, textvariable=self.p3, width=6)
            self.p3.grid(row=10, column=4)
            self.li.destroy()
            self.iv.destroy()
            self.ival = IntVar()
            self.ival.set(0)
            self.a3 = Listbox(window, width=4, height=1)
            self.a3.grid(row=16, column=4)
        elif self.e1.get() == 2 and self.check == True:
            self.li = Label(window, text="B invades A: ", width=16)
            self.li.grid(row=14, column=1)
            self.ival = IntVar()
            self.iv = Checkbutton(window, variable=self.ival)
            self.iv.grid(row=14, column=2)
            self.check = False
            self.e6.destroy()
            self.e7.destroy()
            self.e8.destroy()
            self.e9.destroy()
            self.e10.destroy()
            self.p3.destroy()
            self.a3.destroy()

    def stop_command(self):
        self.animateval = False
        self.b3.destroy()
        self.b2 = Button(window, text="Start", command=self.begin_command)
        self.b2.grid(row=19, column=2)

    def animate(self):
        while self.animateval == True:
            self.next()
            self.window.update()
            time.sleep(0.5)