Esempio n. 1
0
class SettingsOBDView(Frame):        
    settings_list = ["SAE J1850 PWM","SAE J1850 VPW","AUTO, ISO 9141-2","ISO 14230-4 (KWP 5BAUD)","ISO 14230-4 (KWP FAST)","ISO 15765-4 (CAN 11/500)","ISO 15765-4 (CAN 29/500)","ISO 15765-4 (CAN 11/250)","ISO 15765-4 (CAN 29/250)","SAE J1939 (CAN 29/250)"]
    def __init__(self, ui_cont, *args, **kwargs):
        Frame.__init__(self, *args, **kwargs)
        self.initializeGauges()
        self.placeGauges()
        self.ui_cont = ui_cont
        
    def initializeGauges(self):
        self.title_font = font.Font(family="FreeMono", size=15, weight="bold")
        self.list_font = font.Font(family="FreeMono", size=13)
        self.settings_text = Label(self, text="OBD Connection", background="white", font=self.title_font)
        self.listbox = Listbox(self, font=self.list_font, selectmode="single", borderwidth=0, relief="flat", highlightthickness=0)
        self.listbox.bind('<ButtonRelease-1>', self.item_deselected)
        for item in self.settings_list:
            self.listbox.insert(10, item)
            

    def placeGauges(self):
        self.settings_text.pack(fill="x")
        self.listbox.pack(fill="both", expand=1)

    def item_deselected(self, event):
        curr_selct = self.listbox.curselection()
        if len(curr_selct) > 0:
            item_id = self.listbox.curselection()[0]
            item_name = self.settings_list[item_id]
            self.listbox.selection_clear(item_id)
Esempio n. 2
0
    def build(self, listbox: tkinter.Listbox):
        if listbox.curselection().__len__() > 0:
            logger.debug("Building " + listbox.get(listbox.curselection()[0]))

            self.tile.construction = Game.Construction(self.game, self.tile.rel_pos_tuple,
                                                       listbox.get(listbox.curselection()[0]))
            self.level.constructions.append(self.tile.construction)
            self.back_to_main_frame()
Esempio n. 3
0
class GetFileSelection:
    """Shows a listbox, where user can make selections from list.
    Returns list from given path with only the selected objects from directory,
    Obtained list is saved in global variable 'file_list'.

    Can be used to select samples to be processed in the scripts.
    """
    def __init__(self, list_path):
        self.master = Tk()
        self.master.title('Select samples to be processed.')
        frame = Frame(self.master)
        frame.pack()

        self.list_box = Listbox(frame,
                                width=40,
                                height=30,
                                selectmode='extended')
        self.list_box.pack(side='left')

        files = self.insert_list(list_path)

        self.quit = Button(frame,
                           text='Exit and run program',
                           command=self.exit(files),
                           width=15)
        self.quit.pack(side='bottom')

        self.button = Button(frame,
                             text='Show list',
                             command=self.print_list,
                             width=15)
        self.button.pack(side='bottom')

        self.master.mainloop()

    def insert_list(self, list_path):
        """Inserts list elements to GUI"""
        files = os.listdir(list_path)
        files.sort()
        for k in range(len(files)):
            self.list_box.insert('end', files[k])
        return files

    def print_list(self):
        """Prints selected list on command line."""
        val = self.list_box.curselection()
        print('Selected values: {0}'.format(val))

    def exit(self, files):
        """Exits the GUI. File list is saved in a global variable."""
        global file_list
        file_list = self.list_box.curselection()
        files = [files[i] for i in file_list]
        file_list = files[:]
        print('')
        self.master.destroy()
Esempio n. 4
0
class Configuration():
    def __init__(self, db):

        self.main = Tk()
        self.main.title("Configuration")
        self.db = db

        Button(self.main, text='New tab', command=self.NewTab).grid(row=0,
                                                                    column=0)

        self.maindaily_listbox = Listbox(self.main, exportselection=0)
        self.maindaily_listbox.grid(row=1, column=0)
        self.ls = []
        self.d_tabs = self.db.get_tasks_for_table_('Tabs')
        for tab in self.d_tabs:
            self.maindaily_listbox.insert(END, tab)
        self.maindaily_listbox.config(width=20, height=len(self.d_tabs) + 1)

        self.EntryTask = Entry(self.main)
        self.EntryTask.grid(row=2, column=0)
        self.EntryTask.insert(0, 'new name')

        Button(self.main, text="Rename", command=self.Rename).grid(row=3,
                                                                   column=0)
        Button(self.main, text="Delete", command=self.Delete).grid(row=4,
                                                                   column=0)

    def Rename(self):
        NewName = str(self.EntryTask.get())
        Tab2Update = self.maindaily_listbox.get(
            self.maindaily_listbox.curselection())

        if len(NewName) > 0 and NewName != 'new name':
            for tab in self.d_tabs:
                if tab == Tab2Update:
                    self.db.__update_table__('Tabs', 'tab_id', NewName,
                                             'tab_id', Tab2Update)
        self.main.destroy()

    def Delete(self):
        Tab2Delete = self.maindaily_listbox.get(
            self.maindaily_listbox.curselection())
        self.db.__delete_from_table__('Tabs', Tab2Delete,
                                      self.d_tabs[Tab2Delete])
        self.main.destroy()

    def NewTab(self):
        tab = simpledialog.askstring("askstring", "Enter New Tab")
        position_id = 0
        for tab in self.d_tabs:
            if int(self.d_tabs[tab]) > position_id:
                position_id = int(self.d_tabs[tab])
        self.db.__insert_in_table__('Tabs', tab, str(position_id + 1))
        self.maindaily_listbox.insert(END, tab)
        self.maindaily_listbox.config(width=20, height=len(self.d_tabs) + 1)
Esempio n. 5
0
class SettingsView(Frame):
    settings_list = [
        "General", "Wi-Fi", "Appearance", "GPS Chip", "OBD Connection"
    ]

    def __init__(self, ui_cont, *args, **kwargs):
        Frame.__init__(self, *args, **kwargs)
        self.initializeGauges()
        self.placeGauges()
        self.ui_cont = ui_cont
        self.gps_chip_view = SettingsGPSView(ui_cont, self.ui_cont.root)
        self.general_view = SettingsGeneralView(ui_cont, self.ui_cont.root)
        self.wifi_view = SettingsWifiView(ui_cont, self.ui_cont.root)
        self.appearance_view = SettingsAppearanceView(ui_cont,
                                                      self.ui_cont.root)
        self.obd_view = SettingsOBDView(ui_cont, self.ui_cont.root)

    def initializeGauges(self):
        self.title_font = font.Font(family="FreeMono", size=15, weight="bold")
        self.list_font = font.Font(family="FreeMono", size=28)
        self.settings_text = Label(self,
                                   text="Settings",
                                   background="white",
                                   font=self.title_font)
        self.listbox = Listbox(self,
                               font=self.list_font,
                               selectmode="single",
                               borderwidth=0,
                               relief="flat",
                               highlightthickness=0)
        self.listbox.bind('<ButtonRelease-1>', self.item_deselected)
        for item in self.settings_list:
            self.listbox.insert(10, item)

    def placeGauges(self):
        self.settings_text.pack(fill="x")
        self.listbox.pack(fill="both", expand=1)

    def item_deselected(self, event):
        curr_selct = self.listbox.curselection()
        if len(curr_selct) > 0:
            item_id = self.listbox.curselection()[0]
            item_name = self.settings_list[item_id]
            if item_name == "General":
                self.ui_cont.display_view(self.general_view)
            if item_name == "Wi-Fi":
                self.ui_cont.display_view(self.wifi_view)
            if item_name == "Appearance":
                self.ui_cont.display_view(self.appearance_view)
            if item_name == "GPS Chip":
                self.ui_cont.display_view(self.gps_chip_view)
            if item_name == "OBD Connection":
                self.ui_cont.display_view(self.obd_view)
            self.listbox.selection_clear(item_id)
Esempio n. 6
0
class CollectionFrame(Frame):
    """
    Frame permétant de gérer la collection d'épisodes.
    """
    def __init__(self,
                 master=None,
                 select_callback=None,
                 delete_callback=None):
        Frame.__init__(self, master)

        self._select_callback = select_callback
        self._delete_callback = delete_callback

        choices = Variable(self)
        self.listbox = Listbox(self, listvariable=choices, selectmode='single')
        self.listbox.pack()

        buttons_frame = Frame(self)
        Button(buttons_frame, text="Select",
               command=self.get_selected_element).pack(side=RIGHT)
        Button(buttons_frame,
               text="Delete",
               command=self.delete_selected_element).pack(side=RIGHT)
        buttons_frame.pack()

    def add_element(self, element, index=END):
        """
        Ajoute un épisode dans la liste. L'index permet d'ordonner les épisodes.
        :param element: Titre de l'épisode
        :param index: indice d'insertion
        :return: None
        """
        self.listbox.insert(index, element)

    def get_selected_element(self):
        curselection = self.listbox.curselection()
        if len(curselection) != 1:
            print("Pas de selection")
        else:
            self._select_callback(curselection[0])

    def delete_selected_element(self):
        curselection = self.listbox.curselection()
        if len(curselection) != 1:
            print("Pas de selection")
        else:
            ep_index = curselection[0]
            self.listbox.delete(ep_index)
            self._delete_callback(ep_index)
Esempio n. 7
0
class PlayerView:
    directories = []

    def __init__(self, parent_window):
        self.parent_window = parent_window
        self.frame = Frame(parent_window.window)

        self.button_play = Button(self.frame,
                                  text=f"Play",
                                  command=self.__button_play_clicked)
        self.button_stop = Button(self.frame,
                                  text=f"Stop",
                                  command=self.__button_stop_clicked)
        self.button_open = Button(self.frame,
                                  text=f"Open",
                                  command=self.__button_open_clicked)
        self.song_list = Listbox(self.frame)
        self.player = Player()

        self.song_list.grid(column=0, row=0)

        self.button_open.config(width=8)
        self.button_open.grid(column=0, row=1)

        self.button_stop.config(width=8)
        self.button_stop.grid(column=1, row=1)

        self.button_play.config(width=8)
        self.button_play.grid(column=2, row=1)

    def show(self):
        self.frame.grid(column=0, row=0)

    def hide(self):
        self.frame.grid_forget()

    def __button_open_clicked(self):
        open_file_dialog = OpenFileDialog()
        self.directories = open_file_dialog.show()
        for index, directory in enumerate(self.directories):
            self.song_list.insert(index, directory)

    def __button_play_clicked(self):
        if self.song_list.curselection():
            selected_song = self.song_list.get(self.song_list.curselection())
            self.player.play(selected_song)

    def __button_stop_clicked(self):
        self.player.stop()
Esempio n. 8
0
class App:

    WIDTH = 600
    HEIGHT = 800

    def __init__(self, root, funcs):
        self.root = root
        self.root.title("Plotki Migotki")
        self.root.geometry("%dx%d%+d%+d" % (App.WIDTH, App.HEIGHT, 0, 0))
        self.main_frame = Frame(root, width=App.WIDTH, height=App.HEIGHT)
        self.main_frame.pack_propagate(0)
        self.main_frame.pack()
        self.list_map = funcs
        self.listbox = Listbox(self.main_frame, height=4, width=15, selectbackground="orange")
        for ix, (entry, _f) in enumerate(self.list_map):
            plot_type, title = entry
            self.listbox.insert(ix, title)
            if plot_type == BARCHART:
                self.listbox.itemconfig(ix, bg='green')
            elif plot_type == BOXPLOT:
                self.listbox.itemconfig(ix, bg='yellow')
            elif plot_type == SCATTERPLOT:
                self.listbox.itemconfig(ix, bg='grey')
            elif plot_type == HISTOGRAM:
                self.listbox.itemconfig(ix, bg='magenta')
        self.listbox.bind("<Double-Button-1>", self.call_back)
        self.listbox.bind("<Return>", self.call_back)
        self.listbox.pack(expand=1, fill=tk.BOTH)

    def call_back(self, event):
        zones = self.listbox.curselection()
        assert len(zones) == 1
        ix = zones[0]
        ix, cb = self.list_map[ix]
        cb()
Esempio n. 9
0
 def _rename(self, listbox: tk.Listbox):
     selection = listbox.curselection()[0]
     self.original = self.leftover.pop(selection)
     self.master.withdraw()
     self.final = simpledialog.askstring(
         "Rename", "Enter new name for line: " + self.original)
     self.master.destroy()
Esempio n. 10
0
class Application(Frame):
    '''创建框架模板'''
    def __init__(self, master):
        super(Application, self).__init__(master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.label1 = Label(self, text='Select youi items')
        self.label1.grid(row=0, column=0, sticky=W)
        # 创建选择列表控件
        self.listbox1 = Listbox(self, selectmode=EXTENDED)
        items = ['item one', 'item two', 'item three']
        # 添加选择条目
        for item in items:
            self.listbox1.insert(END, item)
        self.listbox1.grid(row=1)
        self.button1 = Button(self, text='Submit', command=self.display)
        self.button1.grid(row=2, column=0, sticky=W)

    def display(self):
        # 获取被选中的条目
        # items为选项索引组成的元组
        items = self.listbox1.curselection()
        for item in items:
            str_item = self.listbox1.get(item)
            print(str_item)
            print('*' * 20)
Esempio n. 11
0
class SelectMultiple(TwoCells):
    def __init__(self, parent, text, values, **kwargs):
        super().__init__(parent, **kwargs)
        self.label = Label(self.frame, text=text)
        self.label.grid(column=0, row=0, padx=5, pady=5, sticky=W)
        self.selected_options = StringVar(self.frame)
        self.selected_options.set(values)
        self.listbox = Listbox(self.frame,
                               listvariable=self.selected_options,
                               selectmode=MULTIPLE)
        self.listbox.grid(column=1, row=0, padx=5, pady=5, sticky=E)

    def disable(self):
        self.listbox.configure(state=DISABLED)
        return self

    def enable(self):
        self.listbox.configure(state=NORMAL)
        return self

    def set(self, values):
        if values:
            self.enable()
        else:
            self.disable()
        self.listbox.selection_clear(0, self.listbox.size())
        for value in values:
            for index in range(self.listbox.size()):
                if value == self.listbox.get(index):
                    self.listbox.selection_set(index)
                    self.listbox.event_generate("<<ListboxSelect>>")

    def get(self):
        return [self.listbox.get(i) for i in self.listbox.curselection()]
Esempio n. 12
0
def popupmsg():
    popup = Tk()
    popup.wm_title("!")
    listbox = Listbox(popup)
    listbox.pack()

    listbox.insert(1, "AND 0-0")
    listbox.insert(2, "AND 0-1")
    listbox.insert(3, "AND 1-0")
    listbox.insert(4, "AND 1-1")
    listbox.insert(5, "OR 0-0")
    listbox.insert(6, "OR 0-1")
    listbox.insert(7, "OR 1-0")
    listbox.insert(8, "OR 1-1")
    listbox.insert(9, "NOT 0")
    listbox.insert(10, "NOT 1")

    B1 = ttk.Button(popup,
                    text="Simulate",
                    command=lambda: showSimulation(listbox.curselection()))
    B1.pack()

    B2 = ttk.Button(popup, text="Exit", command=popup.destroy)
    B2.pack()

    popup.mainloop()
Esempio n. 13
0
 def _getselectedvalues(songlistobject: tk.Listbox, listedlist: list):
     selectedindices = songlistobject.curselection()
     songobjs = set()
     for i in selectedindices:
         if i < len(listedlist):
             songobjs.add(listedlist[i])
     return songobjs
Esempio n. 14
0
class liste_mots:
    """Définit un cadre contenant une liste de mot avec un ascenseur"""
    def __init__(self,parent,titre,compare):
        police_mots=font.Font(parent, size=12, family='Courier')
        self.cadre = LabelFrame(parent, borderwidth=2,
                                relief=GROOVE,text=titre)
        self.ascenseur=Scrollbar(self.cadre,orient=VERTICAL)
        self.mots=Listbox(self.cadre, font=police_mots, width=LARGEUR_LISTE_MOTS,yscrollcommand = self.ascenseur.set )
        self.mots.bind('<<ListboxSelect>>', self.selection)
        self.mots.grid(column=0,row=0)
        self.ascenseur.grid(column=1,row=0,sticky=S+N)
        self.ascenseur.config( command = self.mots.yview )
        self.liste_mots=[]
        self.compare=compare
    def ajoute_mot_couleur(self,mot,couleur):
        fin=self.mots.size()
        self.mots.insert(fin,mot)
        self.mots.itemconfig(fin,fg=couleur)
        self.liste_mots.append(mot)
    def selection(self,e):
        mot=self.liste_mots[self.mots.curselection()[0]]
        self.compare.modifie_mot1(mot)
        self.parent.infos.delete("0.0",END)
        valeur=self.parent.dict_moyennes[mot]
        texte="Selon ce modèle, le mot '"+mot+"' a été lu en moyenne "+str(round(valeur,NB_DECIMALES))+ "fois par un élève de ce profil au cours de l'année écoulée."
        self.parent.infos.insert(END,texte)
Esempio n. 15
0
 def _getextrasongselectedplaylists(
         self, playlistreeview: tk.Listbox) -> list[Playlist]:
     x = [
         self.playlistswithtargetsong[i]
         for i in playlistreeview.curselection()
     ]
     return x
Esempio n. 16
0
class grid_plotter_GUI:
    def __init__(self, filenames, selection):
        self.master = Tk()
        self.selection = selection
        self.master.title("Grid plotter")
        # Big title
        self.label = Label(self.master,
                           text="Select the grid to plot:",
                           font=("Courier", 18))
        self.label.pack()

        # List box
        self.listbox = Listbox(self.master, font=("Courier", 14))
        index = 1
        for filename in filenames:
            self.listbox.insert(index, filename[:-5])
        self.listbox.pack()

        # Finish button
        self.finish_button = Button(self.master,
                                    text="Display plot",
                                    command=self.finish)
        self.finish_button.pack()
        self.master.mainloop()

    def finish(self):
        self.selection[0] = self.listbox.get(self.listbox.curselection(),
                                             self.listbox.curselection())
        self.master.destroy()
Esempio n. 17
0
class SubWindow():
    def __init__(self, main_window, acc_list, main):
        self.master = Toplevel(main_window)
        self.master.resizable(False, False)
        self.master.title("Select Your Account")
        self.main = main

        self.len_max = 50
        for m in acc_list:
            if len(m[0]) > self.len_max:
                self.len_max = len(m[0])

        self.listbox = Listbox(self.master,
                               width=self.len_max,
                               selectmode=SINGLE)
        self.listbox.grid(row=0, column=1)
        self.listbox.bind('<Double-Button>', self.selected_event)
        self.listbox_list = []
        for item in acc_list:
            self.listbox.insert(END, item[0])
            self.listbox_list.append(item[1])

    def selected_event(self, e=None):
        self.main.set_membershipID(
            self.listbox_list[self.listbox.curselection()[0]])
        self.master.destroy()
Esempio n. 18
0
class AlexListBox(Frame):
    def __init__(self,
                 parent,
                 items=[],
                 width=20,
                 height=10,
                 selectioncommand=None):

        super().__init__(parent)

        self.selectioncommand = selectioncommand

        self.listbox = Listbox(self, width=width, height=height)
        self.listbox.grid(row=0, column=0)
        scrollbar_y = AlexScrollbar(self, command=self.listbox.yview)
        scrollbar_y.grid(row=0, column=1, sticky=N + S)
        self.listbox.configure(yscrollcommand=scrollbar_y.set)
        scrollbar_x = AlexScrollbar(self,
                                    command=self.listbox.xview,
                                    orient=HORIZONTAL)
        scrollbar_x.grid(row=1, column=0, sticky=E + W)
        self.listbox.configure(xscrollcommand=scrollbar_x.set)
        if self.selectioncommand is not None:
            self.listbox.bind('<<ListboxSelect>>', self._select_callback)
        self.set_items(items)

    def _select_callback(self, event):

        selection = self.get()
        # ignore unselect
        if selection != None:
            self.selectioncommand(selection)

    def set_items(self, items):

        self.listbox.delete(0, END)
        self.items = []
        for item in items:
            self.items.append(item)
            self.listbox.insert(END, "%s" % item)

    def get_items(self):

        return self.items

    def get(self):

        selections = self.listbox.curselection()
        if len(selections) > 0:
            return self.items[selections[0]]
        else:
            return None

    def set(self, selection):

        self.listbox.selection_clear(0, len(self.items))
        for i in range(0, len(self.items)):
            if self.items[i] == selection:
                self.listbox.selection_set(i)
Esempio n. 19
0
 def insert_list(self, items):
     frame = self.frame
     frame.pack(fill="y")
     lb = Listbox(frame, selectmode="multiple")
     for i in items:
         lb.insert("end", i)
     lb.pack()
     self.ok_cancel_buttons(call=lambda: self.del_items(lb.curselection()))
Esempio n. 20
0
 def insert_list(self, items):
     frame = self.frame
     frame.pack(fill="y")
     lb = Listbox(frame, selectmode="multiple")
     for i in items:
         lb.insert("end", i)
     lb.pack()
     self.ok_cancel_buttons(call=lambda : self.del_items(lb.curselection()))
Esempio n. 21
0
class Application(Frame):
    """Application Class"""
    def __init__(self, master):
        super(Application, self).__init__(master)

        # self.create_widgets()
        # noinspection SpellCheckingInspection
        self.playlistbox = Listbox(self,
                                   width=40,
                                   height=10,
                                   selectmode=SINGLE)
        for song in PLAYLIST:
            self.playlistbox.insert(END, song)

        self.grid(rowspan=5, columnspan=4)
        self.playlistbox.grid(row=1)
        self.play_button = Button(self, text='Play', command=self.play)
        self.loop_button = Button(self, text='Loop', command=self.loop)
        self.add_button = Button(self, text='Add', command=self.add)
        self.play_button.grid(row=4, column=0)
        self.loop_button.grid(row=4, column=1)
        self.add_button.grid(row=4, column=2)
        self.pack()

        init()

    def play(self):
        """play function"""
        if PLAYLIST:
            tk2.showinfo(
                'Notice',
                'No songs in your playlist!\nClick Add to add songs.')
        else:
            pygame.mixer.music.stop()
            selected_songs = self.playlistbox.curselection()
            play_it = PLAYLIST[int(selected_songs[0])]
            pygame.mixer.music.load(play_it)
            pygame.mixer.music.play()

    @staticmethod
    def loop():
        """loop function"""
        pygame.mixer.music.stop()
        pygame.mixer.music.play(-1)

    def add(self):
        """add function"""
        # noinspection SpellCheckingInspection
        file = tk.askopenfilenames(initialdir='~/Downloads')
        songs_tuple = Tk.splitlist(
            file)  # turn user's opened filenames into tuple
        songs_list = list(songs_tuple)  # convert to list
        # Add the full filename of song to playlist list, and a shortened version to the listBox
        for song in songs_list:
            PLAYLIST.append(song)
            temp_array = song.split('/')
            song_short = temp_array[len(temp_array) - 1]
            self.playlistbox.insert(END, song_short)
Esempio n. 22
0
class List:
    def __init__(self, root, array=[]):
        self.__root = root

        self.__frame = Frame(self.__root)

        self.__scrollBar = Scrollbar(self.__frame)

        self.__list = Listbox(self.__frame,
                              font=12,
                              selectmode=SINGLE,
                              width=10,
                              yscrollcommand=self.__scrollBar.set)
        self.__list.config(width=40)
        self.__list.pack(side=LEFT, anchor=W, fill=BOTH, expand=True)

        self.__lessons = []
        self.setList(array)

        self.__scrollBar.config(command=self.__list.yview)
        self.__scrollBar.pack(side=RIGHT, fill=Y)

        self.__frame.pack(side=LEFT, anchor=W, fill=BOTH, expand=True)

    def getObjectList(self):
        return self.__list

    def setList(self, array=[]):
        self.__deleteAll()

        self.__lessons = [x for x in array]
        for item in array:
            self.__list.insert(END, str(item))

    def getSelectedElement(self, e=None):
        if self.__list.curselection() is ():
            return None
        else:
            return self.__lessons[self.__list.curselection()[0]]

    def setSelectedItem(self, i):
        self.__list.activate(i)

    def __deleteAll(self):
        self.__list.delete(0, END)
Esempio n. 23
0
class ScenarioCenter(Toplevel):
    def __init__(self, parent, scEvents, scName):
        super().__init__(parent, height=100)
        self.parent = parent
        self.attributes('-topmost', 'true')
        self.resizable(width=False, height=False)
        self.title('Scenario Center')
        self.scEvents = scEvents
        self.scName = scName
        editListFrame = Frame(self)
        editListFrame.pack()
        self.currentSelection = []

        self.lb = Listbox(editListFrame, selectmode=MULTIPLE)
        for i in self.scEvents:
            self.lb.insert(END, i)

        self.lb.bind("<<ListboxSelect>>", self.onSelect)
        self.lb.grid(row=0, column=0, columnspan=2)

        self.DeleteBtn = Button(editListFrame,
                                text='Delete',
                                width=12,
                                height=1)
        self.DeleteBtn.grid(row=0, column=2, padx=5)
        self.DeleteBtn.bind('<ButtonRelease-1>', self.onDeleteClick)

        self.cancelBtn = Button(editListFrame,
                                text='Cancel',
                                width=12,
                                height=1)
        self.cancelBtn.grid(row=1, column=1, pady=2)
        self.cancelBtn.bind('<ButtonRelease-1>', self.onCancelClick)
        self.okBtn = Button(editListFrame, text='OK', width=12, height=1)
        self.okBtn.grid(row=1, column=2, padx=1, pady=2)
        self.okBtn.bind('<ButtonRelease-1>', self.onOkClick)

    def onCancelClick(self, event):
        log.deleteScenarioFile(self.scName)
        self.parent.updateList()
        self.destroy()

    def onDeleteClick(self, event):
        print(self.scEvents)
        self.lb.delete(0, END)
        for idx in self.currentSelection[::-1]:
            del self.scEvents[idx]
        for i in self.scEvents:
            self.lb.insert(END, i)

    def onOkClick(self, event):
        log.saveSelectedScenario(self.scName, self.scEvents)
        self.destroy()

    def onSelect(self, event):
        self.currentSelection = list(self.lb.curselection())
Esempio n. 24
0
class RSVisCanvasFrame(Frame):

    #   method --------------------------------------------------------------
    # -----------------------------------------------------------------------
    def __init__(
        self, 
        parent,
        images, 
        data,
        **kwargs
    ):
        super(RSVisCanvasFrame, self).__init__(parent)

        self.columnconfigure(2, weight=1)
        self.rowconfigure(0, weight=1)
        self.scrollbar = Scrollbar(self, orient="vertical", width=16)
        self.listbox = Listbox(self, yscrollcommand=self.scrollbar.set, width=37, activestyle=UNDERLINE)
        self.scrollbar.config(command=self.listbox.yview)
        self.scrollbar.grid(row=0, column=0, sticky=N+S)
        self.listbox.grid(row=0, column=1, sticky=N+S)
        for count, item in enumerate(images):
            self.listbox.insert(END, pathlib.Path(item[0].path).stem)
        self.listbox.bind("<<ListboxSelect>>", self.listbox_event)

        self._canvas = rsviscv.RSVisCanvas(self, images, data, **kwargs)
        self._canvas.set_container()
        self._canvas.grid(row=0, column=2, sticky=N+S+E+W)

        # parent.bind("<a>", self.key_a)
        # parent.bind("<d>", self.key_d)  

    #   method --------------------------------------------------------------
    # -----------------------------------------------------------------------
    def listbox_event(self,event):
        try:
            self._canvas._idx_list(index=self.listbox.curselection()[0])
            self._canvas.set_container()
        except IndexError:
            pass

    #   method --------------------------------------------------------------
    # -----------------------------------------------------------------------
    def update_listbox(self, event):
        self.listbox.activate(self._canvas.get_idx_list())

    #   method --------------------------------------------------------------
    # -----------------------------------------------------------------------
    def key_a(self, event, **kwargs):
        """Show the previous image in given image list (see listbox)."""
        self.update_listbox(event)

    #   method --------------------------------------------------------------
    # -----------------------------------------------------------------------
    def key_d(self, event, **kwargs):
        """Show the next image in given image list (see listbox)."""
        self.update_listbox(event)
Esempio n. 25
0
def quick(lb: tk.Listbox):
    selected = lb.curselection()
    if len(selected) < 1:
        messagebox.showwarning(title='Invalid selection',
                               message='Please select an entry to proceed')
        return
    oid = selected[0] + 1
    success = bill_tracker.quick_pay(oid)
    if success is not None:
        messagebox.showinfo(title='Success', message='Quick Pay Succeeded')
Esempio n. 26
0
class AskColumns:

    def __init__(self, master, all_cols):

        self.master = master
        self.master.deiconify()
        self.frame = Frame(self.master)
        self.all_cols = all_cols

        self.prompt = 'Please select which columns will be necessary for ' \
                      'the output excel file.'
        self.label = Label(master, text=self.prompt,
                           width=35,
                           wraplength=150,
                           justify='center').pack()
        self.add_listbox()
        '''submit_button = Button(self,
                               text='Submit',
                               command=self.update_vals())
        submit_button.pack()'''
        self.frame.pack()

    def add_listbox(self):

        self.listbox_frame = Frame(self.master)

        # create a scroll bar for the list box
        scrollbar = Scrollbar(self.listbox_frame)
        scrollbar.pack(side='right', fill='y')

        # create list box
        self.listbox = Listbox(self.listbox_frame,
                               selectmode='multiple',
                               exportselection=0)
        # add column names to the list box
        for col_name in self.all_cols:
            self.listbox.insert(self.all_cols.index(col_name), col_name)
        self.listbox.pack(side='left', fill='y')

        # pack the listbox
        self.listbox_frame.pack()

        # attach listbox to scrollbar
        self.listbox.config(yscrollcommand=scrollbar.set)
        scrollbar.config(command=self.listbox.yview)

    def update_vals(self):

        chosen_lines = self.listbox.curselection()
        cols = [self.all_cols[line] for line in chosen_lines]

        self.master.quit()

        return cols
Esempio n. 27
0
def get_listbox_selected_values(widget: tk.Listbox) -> List[str]:
    """Return values of selections in Listbox.

    :param widget: Listbox to process
    """
    selections = widget.curselection()
    values = []
    for i, __ in enumerate(selections):
        value = widget.get(selections[i])
        values.append(value)
    return values
class MiniWindow:
    def __init__(self,root,list):
        self.list = list
        self.mini = Toplevel(root)
        self.mini.wm_title("Matches")
        print (root.winfo_screenwidth())
        self.mini.geometry("%dx%d+%d+%d" %(500,200,root.winfo_x()+root.winfo_width(),root.winfo_y()))
        self.filelist = Listbox(self.mini)
        for item in self.list:
            self.filelist.insert('end',str(item))
        self.filelist.bind("<<ListboxSelect>>",self.onClick)
        self.filelist.pack(fill="both")
    def onClick(self,event):
        print(self.filelist.curselection())
        index = int(self.filelist.curselection()[0])
        link = self.list[index]
        filedir = os.path.dirname(link)
        if os.name == 'nt':
            os.startfile(filedir)
        elif os.name == 'posix':
            subprocess.Popen(["xdg-open",filedir])
Esempio n. 29
0
class HelpWindow(Toplevel):
    def __init__(self, master=None):
        super().__init__(master)
        self.root = master
        self.title = 'Help'
        self.list = Listbox(self)
        self.list.pack(side="left", fill="y")
        self.text = scrolledtext.ScrolledText(self, wrap=WORD)
        self.text.pack(side="right", fill="both", expand=True)
        self.images = []
        self.load_list()
        self.list.bind('<<ListboxSelect>>', self.list_click)
        self.list.select_set(0)
        self.list_click(None)

    def load_list(self):
        self.files = {}

        path = os.path.dirname(os.path.realpath(__file__))
        file_path = os.path.join(path, 'index.json')
        f = open(file_path, 'rt')
        self.files = json.loads(f.read())
        for counter, entry in enumerate(self.files['files']):
            self.list.insert(counter, entry['name'])
        f.close()

    def list_click(self, event):
        index = self.list.curselection()[0]
        item = self.files['files'][index]
        path = os.path.dirname(os.path.realpath(__file__))
        fileName = os.path.normpath(os.path.join(path, item['fileName']))
        self.render_file(fileName)

    def render_file(self, fileName):
        path = os.path.dirname(os.path.realpath(__file__))
        f = open(os.path.join(fileName), 'rt')
        buffer = f.read()
        f.close()
        parser = MarkdownParser()
        tokens = parser.parse(buffer)
        renderer = MarkdownRenderTk(self.text)
        renderer.render(tokens, os.path.normpath(os.path.join(path, '../res')),
                        self.images, self.link_click)

    def link_click(self, url, title):
        if (url.startswith('http:') or url.startswith('https:')):
            # Open a web browser with that link.
            webbrowser.open(url)
        else:
            # Open a markdown file.
            path = os.path.dirname(os.path.realpath(__file__))
            fileName = os.path.normpath(os.path.join(path, url))
            self.render_file(fileName)
Esempio n. 30
0
 def selection_print_to_TextFrame(self, lb: tk.Listbox, tbox: tk.Text):
     # リストボックスで選択したテキスト取得
     selected_idx = lb.curselection()
     if len(selected_idx) == 0:
         return
     selected_idx = selected_idx[0]
     words = lb.get(selected_idx)
     if self.find_indexces_list:
         self.update_textbox(tbox, words,
                             self.find_indexces_list[selected_idx])
     else:
         self.update_textbox(tbox, words)
Esempio n. 31
0
class EntryOptionsWindow:
    def __init__(self, ls: str, tk: Tk, select_path=False) -> None:
        self.select_path = select_path
        self.List = ls
        self.Tk = tk
        self.Root = Toplevel(self.Tk)
        self.Root.withdraw()
        self.Frame = Frame(self.Root)
        self.Box = Listbox(self.Frame, selectmode='extended', width=54, height=24)
        for i in globals()[self.List]:
            self.Box.insert(END, i)
        self.Scroll = Scrollbar(self.Frame, command=self.Box.yview)
        self.Entry = Entry(self.Frame)
        self.ButtonAdd = Button(self.Frame, text='Добавить', command=self.__add_item)
        self.ButtonDel = Button(self.Frame, text='Удалить', command=self.__del_item)
        self.ButtonDone = Button(self.Frame, text='Готово', command=self.__save_list)
        self.ButtonExit = Button(self.Frame, text='Отмена', command=self.Root.destroy)

    def __add_item(self) -> None:
        if self.select_path:
            text = filedialog.askdirectory()
        else:
            text = self.Entry.get()
        if text:
            self.Box.insert(END, text)
            self.Entry.delete(0, END)

    def __del_item(self) -> None:
        select = list(self.Box.curselection())
        select.reverse()
        for i in select:
            self.Box.delete(i)

    def __save_list(self) -> None:
        globals()[self.List] = list(self.Box.get(0, END))
        self.Root.destroy()

    def main(self) -> None:
        center_win(self.Root, '500x400')
        self.Root.deiconify()
        self.Root.title(f'Editing {self.List}')
        self.Box.pack(side='left', expand=True)
        self.Scroll.pack(side='left', fill='y')
        self.Box.config(yscrollcommand=self.Scroll.set)
        self.Frame.pack(side='left', padx=10)
        if not self.select_path:
            self.Entry.pack(anchor='n')
        self.ButtonAdd.pack(fill='x')
        self.ButtonDel.pack(fill='x')
        self.ButtonDone.pack(fill='x')
        self.ButtonExit.pack(fill='x')
        self.Root.mainloop()
Esempio n. 32
0
class DriversWindow:
    def __init__(self, parent, model):
        self.model = model
        self.parent = parent
        self.window = Toplevel(parent.window)
        self.window.title('Lista sterowników')
        self.window.geometry('600x400')
        self.window.rowconfigure(0, weight=1)
        self.window.columnconfigure(0, weight=1)
        self.x_scroll = Scrollbar(self.window, orient=tk.HORIZONTAL)
        self.y_scroll = Scrollbar(self.window, orient=tk.VERTICAL)
        self.x_scroll.grid(row=1, column=0, columnspan=2, sticky=tk.E + tk.W)
        self.y_scroll.grid(row=0, column=2, sticky=tk.N + tk.S)
        self.listbox = Listbox(self.window,
                               xscrollcommand=self.x_scroll.set,
                               yscrollcommand=self.y_scroll.set)
        self.x_scroll['command'] = self.listbox.xview
        self.y_scroll['command'] = self.listbox.yview
        threading.Thread(target=self.populate_list).start()
        self.prog_window = ProgressWindow(self, 'Trwa szukanie sterowników')
        self.prog_window.start()
        self.listbox.grid(row=0,
                          column=0,
                          sticky=tk.N + tk.E + tk.S + tk.W,
                          columnspan=2)
        Button(self.window, text='Wybierz',
               command=self.pick_dev).grid(row=2, sticky=tk.NE, padx=10)
        Button(self.window, text='Anuluj',
               command=self.destroy).grid(row=2,
                                          column=1,
                                          sticky=tk.NW,
                                          padx=10)
        self.window.transient(master=parent.window)

    def pick_dev(self):
        self.model.project.devpath = self.listbox.get(
            self.listbox.curselection()).split()[-1]
        self.parent.refresh_driver()
        self.destroy()

    def populate_list(self):
        dev_list = self.model.list_dev_paths()
        for d in dev_list:
            if d is not '':
                self.listbox.insert(tk.END, d)
        self.prog_window.destroy()
        self.window.grab_set()

    def destroy(self):
        self.window.grab_release()
        self.window.destroy()
Esempio n. 33
0
class LabeledListBox(Frame):
    def __init__(self, parent, list_model, label_text):
        Frame.__init__(self, parent)

        self._list_model = list_model
        self._list_objects = []
        self._selected_items = []
        scrollbar = Scrollbar(self, orient=VERTICAL)
        Label(self, text=label_text).pack()
        self.listbox = Listbox(self, selectmode=EXTENDED, exportselection=0, yscrollcommand=scrollbar.set, borderwidth=0, highlightthickness=0)
        scrollbar.config(command=self.listbox.yview)
        scrollbar.pack(side=RIGHT, fill=Y)
        self.listbox.pack(side=LEFT, fill=BOTH, expand=1)
        self.listbox.bind('<<ListboxSelect>>', self._on_select)
        self._list_model.list_items_model.add_listener(self._list_items_changed)
        self._list_model.selected_items_model.add_listener(self._selected_items_changed)
        self._update_list_items()

    def _list_items_changed(self, values):
        self._update_list_items()

    def _selected_items_changed(self, values):
        self._update_selected_items()

    def _update_list_items(self):
        values, labels = self._list_model.list_items_model.get_list_values()
        if not values == self._list_objects:
            self._list_objects = []
            self._selected_items = []
            self.listbox.delete(0, END)
            for value, label in zip(values, labels):
                self._list_objects.append(value)
                self.listbox.insert(END, label)
            self._update_selected_items()

    def _update_selected_items(self):
        selected_items = self._list_model.selected_items_model.selected_items
        if not selected_items == self._selected_items:
            self._selected_items = selected_items
            for index, list_item in enumerate(self._list_objects):
                if list_item in selected_items:
                    self.listbox.selection_set(index)

    def _on_select(self, evt):
        visible_selected_indices = self.listbox.curselection()
        for index, list_item in enumerate(self._list_objects):
            if index in visible_selected_indices:
                self._list_model.selected_items_model.select(list_item)
            else:
                self._list_model.selected_items_model.deselect(list_item)
Esempio n. 34
0
class SelectDeviceFrame(CopilotInnerFrame):
    def __init__(self, master, config, state):
        super(SelectDeviceFrame, self).__init__(master, config)

        self._state = state

        if self._state.action == 'copy':
            self._frame_lbl['text'] = 'Copy To Device'
        elif self._state.action == 'delete':
            self._frame_lbl['text'] = 'Delete From Device'

        self._next_btn['command'] = self._next_cmd

        self._dev_list = Listbox(self._master, font=self._config.item_font)
        self._dev_list.grid(row=1, column=0, columnspan=3, sticky='nsew')
        self._dev_list.configure(yscrollcommand=self._sb.set)
        self._sb['command'] = self._dev_list.yview

        self._refresh_drives()

    def _next_cmd(self):
        if len(self._dev_list.curselection()) > 0:
            item_idx = int(self._dev_list.curselection()[0])
            self._state.to_device = self._parts[item_idx]
            if self._state.action == 'copy':
                self._new_state_window(DeviceToFrame, self._state)
            elif self._state.action == 'delete':
                self._new_state_window(CopyFileFrame, self._state)

    def _refresh_drives(self):
        self._parts = []
        for drive in usb_drives():
            for part in drive.partitions():
                drive_opt = DriveOption(drive, part)
                self._parts.append(drive_opt)
                self._dev_list.insert('end', drive_opt)
Esempio n. 35
0
class RecursiveDescentApp(object):
    """
    A graphical tool for exploring the recursive descent parser.  The tool
    displays the parser's tree and the remaining text, and allows the
    user to control the parser's operation.  In particular, the user
    can expand subtrees on the frontier, match tokens on the frontier
    against the text, and backtrack.  A "step" button simply steps
    through the parsing process, performing the operations that
    ``RecursiveDescentParser`` would use.
    """
    def __init__(self, grammar, sent, trace=0):
        self._sent = sent
        self._parser = SteppingRecursiveDescentParser(grammar, trace)

        # Set up the main window.
        self._top = Tk()
        self._top.title('Recursive Descent Parser Application')

        # Set up key bindings.
        self._init_bindings()

        # Initialize the fonts.
        self._init_fonts(self._top)

        # Animations.  animating_lock is a lock to prevent the demo
        # from performing new operations while it's animating.
        self._animation_frames = IntVar(self._top)
        self._animation_frames.set(5)
        self._animating_lock = 0
        self._autostep = 0

        # The user can hide the grammar.
        self._show_grammar = IntVar(self._top)
        self._show_grammar.set(1)

        # Create the basic frames.
        self._init_menubar(self._top)
        self._init_buttons(self._top)
        self._init_feedback(self._top)
        self._init_grammar(self._top)
        self._init_canvas(self._top)

        # Initialize the parser.
        self._parser.initialize(self._sent)

        # Resize callback
        self._canvas.bind('<Configure>', self._configure)

    #########################################
    ##  Initialization Helpers
    #########################################

    def _init_fonts(self, root):
        # See: <http://www.astro.washington.edu/owen/ROTKFolklore.html>
        self._sysfont = tkinter.font.Font(font=Button()["font"])
        root.option_add("*Font", self._sysfont)

        # TWhat's our font size (default=same as sysfont)
        self._size = IntVar(root)
        self._size.set(self._sysfont.cget('size'))

        self._boldfont = tkinter.font.Font(family='helvetica', weight='bold',
                                    size=self._size.get())
        self._font = tkinter.font.Font(family='helvetica',
                                    size=self._size.get())
        if self._size.get() < 0: big = self._size.get()-2
        else: big = self._size.get()+2
        self._bigfont = tkinter.font.Font(family='helvetica', weight='bold',
                                    size=big)

    def _init_grammar(self, parent):
        # Grammar view.
        self._prodframe = listframe = Frame(parent)
        self._prodframe.pack(fill='both', side='left', padx=2)
        self._prodlist_label = Label(self._prodframe, font=self._boldfont,
                                     text='Available Expansions')
        self._prodlist_label.pack()
        self._prodlist = Listbox(self._prodframe, selectmode='single',
                                 relief='groove', background='white',
                                 foreground='#909090', font=self._font,
                                 selectforeground='#004040',
                                 selectbackground='#c0f0c0')

        self._prodlist.pack(side='right', fill='both', expand=1)

        self._productions = list(self._parser.grammar().productions())
        for production in self._productions:
            self._prodlist.insert('end', ('  %s' % production))
        self._prodlist.config(height=min(len(self._productions), 25))

        # Add a scrollbar if there are more than 25 productions.
        if len(self._productions) > 25:
            listscroll = Scrollbar(self._prodframe,
                                   orient='vertical')
            self._prodlist.config(yscrollcommand = listscroll.set)
            listscroll.config(command=self._prodlist.yview)
            listscroll.pack(side='left', fill='y')

        # If they select a production, apply it.
        self._prodlist.bind('<<ListboxSelect>>', self._prodlist_select)

    def _init_bindings(self):
        # Key bindings are a good thing.
        self._top.bind('<Control-q>', self.destroy)
        self._top.bind('<Control-x>', self.destroy)
        self._top.bind('<Escape>', self.destroy)
        self._top.bind('e', self.expand)
        #self._top.bind('<Alt-e>', self.expand)
        #self._top.bind('<Control-e>', self.expand)
        self._top.bind('m', self.match)
        self._top.bind('<Alt-m>', self.match)
        self._top.bind('<Control-m>', self.match)
        self._top.bind('b', self.backtrack)
        self._top.bind('<Alt-b>', self.backtrack)
        self._top.bind('<Control-b>', self.backtrack)
        self._top.bind('<Control-z>', self.backtrack)
        self._top.bind('<BackSpace>', self.backtrack)
        self._top.bind('a', self.autostep)
        #self._top.bind('<Control-a>', self.autostep)
        self._top.bind('<Control-space>', self.autostep)
        self._top.bind('<Control-c>', self.cancel_autostep)
        self._top.bind('<space>', self.step)
        self._top.bind('<Delete>', self.reset)
        self._top.bind('<Control-p>', self.postscript)
        #self._top.bind('<h>', self.help)
        #self._top.bind('<Alt-h>', self.help)
        self._top.bind('<Control-h>', self.help)
        self._top.bind('<F1>', self.help)
        #self._top.bind('<g>', self.toggle_grammar)
        #self._top.bind('<Alt-g>', self.toggle_grammar)
        #self._top.bind('<Control-g>', self.toggle_grammar)
        self._top.bind('<Control-g>', self.edit_grammar)
        self._top.bind('<Control-t>', self.edit_sentence)

    def _init_buttons(self, parent):
        # Set up the frames.
        self._buttonframe = buttonframe = Frame(parent)
        buttonframe.pack(fill='none', side='bottom', padx=3, pady=2)
        Button(buttonframe, text='Step',
               background='#90c0d0', foreground='black',
               command=self.step,).pack(side='left')
        Button(buttonframe, text='Autostep',
               background='#90c0d0', foreground='black',
               command=self.autostep,).pack(side='left')
        Button(buttonframe, text='Expand', underline=0,
               background='#90f090', foreground='black',
               command=self.expand).pack(side='left')
        Button(buttonframe, text='Match', underline=0,
               background='#90f090', foreground='black',
               command=self.match).pack(side='left')
        Button(buttonframe, text='Backtrack', underline=0,
               background='#f0a0a0', foreground='black',
               command=self.backtrack).pack(side='left')
        # Replace autostep...
#         self._autostep_button = Button(buttonframe, text='Autostep',
#                                        underline=0, command=self.autostep)
#         self._autostep_button.pack(side='left')

    def _configure(self, event):
        self._autostep = 0
        (x1, y1, x2, y2) = self._cframe.scrollregion()
        y2 = event.height - 6
        self._canvas['scrollregion'] = '%d %d %d %d' % (x1,y1,x2,y2)
        self._redraw()

    def _init_feedback(self, parent):
        self._feedbackframe = feedbackframe = Frame(parent)
        feedbackframe.pack(fill='x', side='bottom', padx=3, pady=3)
        self._lastoper_label = Label(feedbackframe, text='Last Operation:',
                                     font=self._font)
        self._lastoper_label.pack(side='left')
        lastoperframe = Frame(feedbackframe, relief='sunken', border=1)
        lastoperframe.pack(fill='x', side='right', expand=1, padx=5)
        self._lastoper1 = Label(lastoperframe, foreground='#007070',
                                background='#f0f0f0', font=self._font)
        self._lastoper2 = Label(lastoperframe, anchor='w', width=30,
                                foreground='#004040', background='#f0f0f0',
                                font=self._font)
        self._lastoper1.pack(side='left')
        self._lastoper2.pack(side='left', fill='x', expand=1)

    def _init_canvas(self, parent):
        self._cframe = CanvasFrame(parent, background='white',
                                   #width=525, height=250,
                                   closeenough=10,
                                   border=2, relief='sunken')
        self._cframe.pack(expand=1, fill='both', side='top', pady=2)
        canvas = self._canvas = self._cframe.canvas()

        # Initially, there's no tree or text
        self._tree = None
        self._textwidgets = []
        self._textline = None

    def _init_menubar(self, parent):
        menubar = Menu(parent)

        filemenu = Menu(menubar, tearoff=0)
        filemenu.add_command(label='Reset Parser', underline=0,
                             command=self.reset, accelerator='Del')
        filemenu.add_command(label='Print to Postscript', underline=0,
                             command=self.postscript, accelerator='Ctrl-p')
        filemenu.add_command(label='Exit', underline=1,
                             command=self.destroy, accelerator='Ctrl-x')
        menubar.add_cascade(label='File', underline=0, menu=filemenu)

        editmenu = Menu(menubar, tearoff=0)
        editmenu.add_command(label='Edit Grammar', underline=5,
                             command=self.edit_grammar,
                             accelerator='Ctrl-g')
        editmenu.add_command(label='Edit Text', underline=5,
                             command=self.edit_sentence,
                             accelerator='Ctrl-t')
        menubar.add_cascade(label='Edit', underline=0, menu=editmenu)

        rulemenu = Menu(menubar, tearoff=0)
        rulemenu.add_command(label='Step', underline=1,
                             command=self.step, accelerator='Space')
        rulemenu.add_separator()
        rulemenu.add_command(label='Match', underline=0,
                             command=self.match, accelerator='Ctrl-m')
        rulemenu.add_command(label='Expand', underline=0,
                             command=self.expand, accelerator='Ctrl-e')
        rulemenu.add_separator()
        rulemenu.add_command(label='Backtrack', underline=0,
                             command=self.backtrack, accelerator='Ctrl-b')
        menubar.add_cascade(label='Apply', underline=0, menu=rulemenu)

        viewmenu = Menu(menubar, tearoff=0)
        viewmenu.add_checkbutton(label="Show Grammar", underline=0,
                                 variable=self._show_grammar,
                                 command=self._toggle_grammar)
        viewmenu.add_separator()
        viewmenu.add_radiobutton(label='Tiny', variable=self._size,
                                 underline=0, value=10, command=self.resize)
        viewmenu.add_radiobutton(label='Small', variable=self._size,
                                 underline=0, value=12, command=self.resize)
        viewmenu.add_radiobutton(label='Medium', variable=self._size,
                                 underline=0, value=14, command=self.resize)
        viewmenu.add_radiobutton(label='Large', variable=self._size,
                                 underline=0, value=18, command=self.resize)
        viewmenu.add_radiobutton(label='Huge', variable=self._size,
                                 underline=0, value=24, command=self.resize)
        menubar.add_cascade(label='View', underline=0, menu=viewmenu)

        animatemenu = Menu(menubar, tearoff=0)
        animatemenu.add_radiobutton(label="No Animation", underline=0,
                                    variable=self._animation_frames,
                                    value=0)
        animatemenu.add_radiobutton(label="Slow Animation", underline=0,
                                    variable=self._animation_frames,
                                    value=10, accelerator='-')
        animatemenu.add_radiobutton(label="Normal Animation", underline=0,
                                    variable=self._animation_frames,
                                    value=5, accelerator='=')
        animatemenu.add_radiobutton(label="Fast Animation", underline=0,
                                    variable=self._animation_frames,
                                    value=2, accelerator='+')
        menubar.add_cascade(label="Animate", underline=1, menu=animatemenu)


        helpmenu = Menu(menubar, tearoff=0)
        helpmenu.add_command(label='About', underline=0,
                             command=self.about)
        helpmenu.add_command(label='Instructions', underline=0,
                             command=self.help, accelerator='F1')
        menubar.add_cascade(label='Help', underline=0, menu=helpmenu)

        parent.config(menu=menubar)

    #########################################
    ##  Helper
    #########################################

    def _get(self, widget, treeloc):
        for i in treeloc: widget = widget.subtrees()[i]
        if isinstance(widget, TreeSegmentWidget):
            widget = widget.label()
        return widget

    #########################################
    ##  Main draw procedure
    #########################################

    def _redraw(self):
        canvas = self._canvas

        # Delete the old tree, widgets, etc.
        if self._tree is not None:
            self._cframe.destroy_widget(self._tree)
        for twidget in self._textwidgets:
            self._cframe.destroy_widget(twidget)
        if self._textline is not None:
            self._canvas.delete(self._textline)

        # Draw the tree.
        helv = ('helvetica', -self._size.get())
        bold = ('helvetica', -self._size.get(), 'bold')
        attribs = {'tree_color': '#000000', 'tree_width': 2,
                   'node_font': bold, 'leaf_font': helv,}
        tree = self._parser.tree()
        self._tree = tree_to_treesegment(canvas, tree, **attribs)
        self._cframe.add_widget(self._tree, 30, 5)

        # Draw the text.
        helv = ('helvetica', -self._size.get())
        bottom = y = self._cframe.scrollregion()[3]
        self._textwidgets = [TextWidget(canvas, word, font=self._font)
                             for word in self._sent]
        for twidget in self._textwidgets:
            self._cframe.add_widget(twidget, 0, 0)
            twidget.move(0, bottom-twidget.bbox()[3]-5)
            y = min(y, twidget.bbox()[1])

        # Draw a line over the text, to separate it from the tree.
        self._textline = canvas.create_line(-5000, y-5, 5000, y-5, dash='.')

        # Highlight appropriate nodes.
        self._highlight_nodes()
        self._highlight_prodlist()

        # Make sure the text lines up.
        self._position_text()


    def _redraw_quick(self):
        # This should be more-or-less sufficient after an animation.
        self._highlight_nodes()
        self._highlight_prodlist()
        self._position_text()

    def _highlight_nodes(self):
        # Highlight the list of nodes to be checked.
        bold = ('helvetica', -self._size.get(), 'bold')
        for treeloc in self._parser.frontier()[:1]:
            self._get(self._tree, treeloc)['color'] = '#20a050'
            self._get(self._tree, treeloc)['font'] = bold
        for treeloc in self._parser.frontier()[1:]:
            self._get(self._tree, treeloc)['color'] = '#008080'

    def _highlight_prodlist(self):
        # Highlight the productions that can be expanded.
        # Boy, too bad tkinter doesn't implement Listbox.itemconfig;
        # that would be pretty useful here.
        self._prodlist.delete(0, 'end')
        expandable = self._parser.expandable_productions()
        untried = self._parser.untried_expandable_productions()
        productions = self._productions
        for index in range(len(productions)):
            if productions[index] in expandable:
                if productions[index] in untried:
                    self._prodlist.insert(index, ' %s' % productions[index])
                else:
                    self._prodlist.insert(index, ' %s (TRIED)' %
                                          productions[index])
                self._prodlist.selection_set(index)
            else:
                self._prodlist.insert(index, ' %s' % productions[index])

    def _position_text(self):
        # Line up the text widgets that are matched against the tree
        numwords = len(self._sent)
        num_matched = numwords - len(self._parser.remaining_text())
        leaves = self._tree_leaves()[:num_matched]
        xmax = self._tree.bbox()[0]
        for i in range(0, len(leaves)):
            widget = self._textwidgets[i]
            leaf = leaves[i]
            widget['color'] = '#006040'
            leaf['color'] = '#006040'
            widget.move(leaf.bbox()[0] - widget.bbox()[0], 0)
            xmax = widget.bbox()[2] + 10

        # Line up the text widgets that are not matched against the tree.
        for i in range(len(leaves), numwords):
            widget = self._textwidgets[i]
            widget['color'] = '#a0a0a0'
            widget.move(xmax - widget.bbox()[0], 0)
            xmax = widget.bbox()[2] + 10

        # If we have a complete parse, make everything green :)
        if self._parser.currently_complete():
            for twidget in self._textwidgets:
                twidget['color'] = '#00a000'

        # Move the matched leaves down to the text.
        for i in range(0, len(leaves)):
            widget = self._textwidgets[i]
            leaf = leaves[i]
            dy = widget.bbox()[1] - leaf.bbox()[3] - 10.0
            dy = max(dy, leaf.parent().label().bbox()[3] - leaf.bbox()[3] + 10)
            leaf.move(0, dy)

    def _tree_leaves(self, tree=None):
        if tree is None: tree = self._tree
        if isinstance(tree, TreeSegmentWidget):
            leaves = []
            for child in tree.subtrees(): leaves += self._tree_leaves(child)
            return leaves
        else:
            return [tree]

    #########################################
    ##  Button Callbacks
    #########################################

    def destroy(self, *e):
        self._autostep = 0
        if self._top is None: return
        self._top.destroy()
        self._top = None

    def reset(self, *e):
        self._autostep = 0
        self._parser.initialize(self._sent)
        self._lastoper1['text'] = 'Reset Application'
        self._lastoper2['text'] = ''
        self._redraw()

    def autostep(self, *e):
        if self._animation_frames.get() == 0:
            self._animation_frames.set(2)
        if self._autostep:
            self._autostep = 0
        else:
            self._autostep = 1
            self._step()

    def cancel_autostep(self, *e):
        #self._autostep_button['text'] = 'Autostep'
        self._autostep = 0

    # Make sure to stop auto-stepping if we get any user input.
    def step(self, *e): self._autostep = 0; self._step()
    def match(self, *e): self._autostep = 0; self._match()
    def expand(self, *e): self._autostep = 0; self._expand()
    def backtrack(self, *e): self._autostep = 0; self._backtrack()

    def _step(self):
        if self._animating_lock: return

        # Try expanding, matching, and backtracking (in that order)
        if self._expand(): pass
        elif self._parser.untried_match() and self._match(): pass
        elif self._backtrack(): pass
        else:
            self._lastoper1['text'] = 'Finished'
            self._lastoper2['text'] = ''
            self._autostep = 0

        # Check if we just completed a parse.
        if self._parser.currently_complete():
            self._autostep = 0
            self._lastoper2['text'] += '    [COMPLETE PARSE]'

    def _expand(self, *e):
        if self._animating_lock: return
        old_frontier = self._parser.frontier()
        rv = self._parser.expand()
        if rv is not None:
            self._lastoper1['text'] = 'Expand:'
            self._lastoper2['text'] = rv
            self._prodlist.selection_clear(0, 'end')
            index = self._productions.index(rv)
            self._prodlist.selection_set(index)
            self._animate_expand(old_frontier[0])
            return 1
        else:
            self._lastoper1['text'] = 'Expand:'
            self._lastoper2['text'] = '(all expansions tried)'
            return 0

    def _match(self, *e):
        if self._animating_lock: return
        old_frontier = self._parser.frontier()
        rv = self._parser.match()
        if rv is not None:
            self._lastoper1['text'] = 'Match:'
            self._lastoper2['text'] = rv
            self._animate_match(old_frontier[0])
            return 1
        else:
            self._lastoper1['text'] = 'Match:'
            self._lastoper2['text'] = '(failed)'
            return 0

    def _backtrack(self, *e):
        if self._animating_lock: return
        if self._parser.backtrack():
            elt = self._parser.tree()
            for i in self._parser.frontier()[0]:
                elt = elt[i]
            self._lastoper1['text'] = 'Backtrack'
            self._lastoper2['text'] = ''
            if isinstance(elt, Tree):
                self._animate_backtrack(self._parser.frontier()[0])
            else:
                self._animate_match_backtrack(self._parser.frontier()[0])
            return 1
        else:
            self._autostep = 0
            self._lastoper1['text'] = 'Finished'
            self._lastoper2['text'] = ''
            return 0

    def about(self, *e):
        ABOUT = ("NLTK Recursive Descent Parser Application\n"+
                 "Written by Edward Loper")
        TITLE = 'About: Recursive Descent Parser Application'
        try:
            from tkinter.messagebox import Message
            Message(message=ABOUT, title=TITLE).show()
        except:
            ShowText(self._top, TITLE, ABOUT)

    def help(self, *e):
        self._autostep = 0
        # The default font's not very legible; try using 'fixed' instead.
        try:
            ShowText(self._top, 'Help: Recursive Descent Parser Application',
                     (__doc__ or '').strip(), width=75, font='fixed')
        except:
            ShowText(self._top, 'Help: Recursive Descent Parser Application',
                     (__doc__ or '').strip(), width=75)

    def postscript(self, *e):
        self._autostep = 0
        self._cframe.print_to_file()

    def mainloop(self, *args, **kwargs):
        """
        Enter the Tkinter mainloop.  This function must be called if
        this demo is created from a non-interactive program (e.g.
        from a secript); otherwise, the demo will close as soon as
        the script completes.
        """
        if in_idle(): return
        self._top.mainloop(*args, **kwargs)

    def resize(self, size=None):
        if size is not None: self._size.set(size)
        size = self._size.get()
        self._font.configure(size=-(abs(size)))
        self._boldfont.configure(size=-(abs(size)))
        self._sysfont.configure(size=-(abs(size)))
        self._bigfont.configure(size=-(abs(size+2)))
        self._redraw()

    #########################################
    ##  Expand Production Selection
    #########################################

    def _toggle_grammar(self, *e):
        if self._show_grammar.get():
            self._prodframe.pack(fill='both', side='left', padx=2,
                                 after=self._feedbackframe)
            self._lastoper1['text'] = 'Show Grammar'
        else:
            self._prodframe.pack_forget()
            self._lastoper1['text'] = 'Hide Grammar'
        self._lastoper2['text'] = ''

#     def toggle_grammar(self, *e):
#         self._show_grammar = not self._show_grammar
#         if self._show_grammar:
#             self._prodframe.pack(fill='both', expand='y', side='left',
#                                  after=self._feedbackframe)
#             self._lastoper1['text'] = 'Show Grammar'
#         else:
#             self._prodframe.pack_forget()
#             self._lastoper1['text'] = 'Hide Grammar'
#         self._lastoper2['text'] = ''

    def _prodlist_select(self, event):
        selection = self._prodlist.curselection()
        if len(selection) != 1: return
        index = int(selection[0])
        old_frontier = self._parser.frontier()
        production = self._parser.expand(self._productions[index])

        if production:
            self._lastoper1['text'] = 'Expand:'
            self._lastoper2['text'] = production
            self._prodlist.selection_clear(0, 'end')
            self._prodlist.selection_set(index)
            self._animate_expand(old_frontier[0])
        else:
            # Reset the production selections.
            self._prodlist.selection_clear(0, 'end')
            for prod in self._parser.expandable_productions():
                index = self._productions.index(prod)
                self._prodlist.selection_set(index)

    #########################################
    ##  Animation
    #########################################

    def _animate_expand(self, treeloc):
        oldwidget = self._get(self._tree, treeloc)
        oldtree = oldwidget.parent()
        top = not isinstance(oldtree.parent(), TreeSegmentWidget)

        tree = self._parser.tree()
        for i in treeloc:
            tree = tree[i]

        widget = tree_to_treesegment(self._canvas, tree,
                                     node_font=self._boldfont,
                                     leaf_color='white',
                                     tree_width=2, tree_color='white',
                                     node_color='white',
                                     leaf_font=self._font)
        widget.label()['color'] = '#20a050'

        (oldx, oldy) = oldtree.label().bbox()[:2]
        (newx, newy) = widget.label().bbox()[:2]
        widget.move(oldx-newx, oldy-newy)

        if top:
            self._cframe.add_widget(widget, 0, 5)
            widget.move(30-widget.label().bbox()[0], 0)
            self._tree = widget
        else:
            oldtree.parent().replace_child(oldtree, widget)

        # Move the children over so they don't overlap.
        # Line the children up in a strange way.
        if widget.subtrees():
            dx = (oldx + widget.label().width()/2 -
                  widget.subtrees()[0].bbox()[0]/2 -
                  widget.subtrees()[0].bbox()[2]/2)
            for subtree in widget.subtrees(): subtree.move(dx, 0)

        self._makeroom(widget)

        if top:
            self._cframe.destroy_widget(oldtree)
        else:
            oldtree.destroy()

        colors = ['gray%d' % (10*int(10*x/self._animation_frames.get()))
                  for x in range(self._animation_frames.get(),0,-1)]

        # Move the text string down, if necessary.
        dy = widget.bbox()[3] + 30 - self._canvas.coords(self._textline)[1]
        if dy > 0:
            for twidget in self._textwidgets: twidget.move(0, dy)
            self._canvas.move(self._textline, 0, dy)

        self._animate_expand_frame(widget, colors)

    def _makeroom(self, treeseg):
        """
        Make sure that no sibling tree bbox's overlap.
        """
        parent = treeseg.parent()
        if not isinstance(parent, TreeSegmentWidget): return

        index = parent.subtrees().index(treeseg)

        # Handle siblings to the right
        rsiblings = parent.subtrees()[index+1:]
        if rsiblings:
            dx = treeseg.bbox()[2] - rsiblings[0].bbox()[0] + 10
            for sibling in rsiblings: sibling.move(dx, 0)

        # Handle siblings to the left
        if index > 0:
            lsibling = parent.subtrees()[index-1]
            dx = max(0, lsibling.bbox()[2] - treeseg.bbox()[0] + 10)
            treeseg.move(dx, 0)

        # Keep working up the tree.
        self._makeroom(parent)

    def _animate_expand_frame(self, widget, colors):
        if len(colors) > 0:
            self._animating_lock = 1
            widget['color'] = colors[0]
            for subtree in widget.subtrees():
                if isinstance(subtree, TreeSegmentWidget):
                    subtree.label()['color'] = colors[0]
                else:
                    subtree['color'] = colors[0]
            self._top.after(50, self._animate_expand_frame,
                            widget, colors[1:])
        else:
            widget['color'] = 'black'
            for subtree in widget.subtrees():
                if isinstance(subtree, TreeSegmentWidget):
                    subtree.label()['color'] = 'black'
                else:
                    subtree['color'] = 'black'
            self._redraw_quick()
            widget.label()['color'] = 'black'
            self._animating_lock = 0
            if self._autostep: self._step()

    def _animate_backtrack(self, treeloc):
        # Flash red first, if we're animating.
        if self._animation_frames.get() == 0: colors = []
        else: colors = ['#a00000', '#000000', '#a00000']
        colors += ['gray%d' % (10*int(10*x/(self._animation_frames.get())))
                   for x in range(1, self._animation_frames.get()+1)]

        widgets = [self._get(self._tree, treeloc).parent()]
        for subtree in widgets[0].subtrees():
            if isinstance(subtree, TreeSegmentWidget):
                widgets.append(subtree.label())
            else:
                widgets.append(subtree)

        self._animate_backtrack_frame(widgets, colors)

    def _animate_backtrack_frame(self, widgets, colors):
        if len(colors) > 0:
            self._animating_lock = 1
            for widget in widgets: widget['color'] = colors[0]
            self._top.after(50, self._animate_backtrack_frame,
                            widgets, colors[1:])
        else:
            for widget in widgets[0].subtrees():
                widgets[0].remove_child(widget)
                widget.destroy()
            self._redraw_quick()
            self._animating_lock = 0
            if self._autostep: self._step()

    def _animate_match_backtrack(self, treeloc):
        widget = self._get(self._tree, treeloc)
        node = widget.parent().label()
        dy = (1.0 * (node.bbox()[3] - widget.bbox()[1] + 14) /
              max(1, self._animation_frames.get()))
        self._animate_match_backtrack_frame(self._animation_frames.get(),
                                            widget, dy)

    def _animate_match(self, treeloc):
        widget = self._get(self._tree, treeloc)

        dy = ((self._textwidgets[0].bbox()[1] - widget.bbox()[3] - 10.0) /
              max(1, self._animation_frames.get()))
        self._animate_match_frame(self._animation_frames.get(), widget, dy)

    def _animate_match_frame(self, frame, widget, dy):
        if frame > 0:
            self._animating_lock = 1
            widget.move(0, dy)
            self._top.after(10, self._animate_match_frame,
                            frame-1, widget, dy)
        else:
            widget['color'] = '#006040'
            self._redraw_quick()
            self._animating_lock = 0
            if self._autostep: self._step()

    def _animate_match_backtrack_frame(self, frame, widget, dy):
        if frame > 0:
            self._animating_lock = 1
            widget.move(0, dy)
            self._top.after(10, self._animate_match_backtrack_frame,
                            frame-1, widget, dy)
        else:
            widget.parent().remove_child(widget)
            widget.destroy()
            self._animating_lock = 0
            if self._autostep: self._step()

    def edit_grammar(self, *e):
        CFGEditor(self._top, self._parser.grammar(), self.set_grammar)

    def set_grammar(self, grammar):
        self._parser.set_grammar(grammar)
        self._productions = list(grammar.productions())
        self._prodlist.delete(0, 'end')
        for production in self._productions:
            self._prodlist.insert('end', (' %s' % production))

    def edit_sentence(self, *e):
        sentence = " ".join(self._sent)
        title = 'Edit Text'
        instr = 'Enter a new sentence to parse.'
        EntryDialog(self._top, sentence, instr, self.set_sentence, title)

    def set_sentence(self, sentence):
        self._sent = sentence.split() #[XX] use tagged?
        self.reset()
Esempio n. 36
0
File: tkui.py Progetto: mindhog/mawb
class TextSelect(Frame):

    def __init__(self, client, anchor, items, destroyAnchor=False):
        """
        Args:
            client: [SelectionClient] The window that text is returned to.
            anchor: A window that the text selection popup is created
                relative to.
            items: [str], items to display in the listbox.
            destroyAnchor: [bool] if true, destroy the anchor after
                positioning the window.
        """
        self.top = Toplevel()
        self.anchor = anchor
        self.top.overrideredirect(1)
        self.top.wm_geometry('+%s+%s' % (anchor.winfo_rootx() + anchor.winfo_x(),
                                         anchor.winfo_rooty() + anchor.winfo_y()
                                         )
                             )
        super(TextSelect, self).__init__(self.top)
        self.entry = Entry(self)
        self.client = client
        self.items = items
        self.place(x = 0.5, y = 0.5, height = 100, width = 100)
        self.entry.bind('<Return>', self.close)
        self.entry.bind('<KeyPress>', self.filter)
        self.entry.bind('<Escape>', self.abort)
        self.entry.bind('<Up>', self.up)
        self.entry.bind('<Down>', self.down)
        self.entry.pack()

        # Create the list of items.
        self.list = Listbox(self)
        for item in self.items:
            self.list.insert('end', item)
        self.list.pack()
        self.grid()
        self.entry.focus()

        # Reposition the select button against the anchor.  We defer this
        # until after idle so that the anchor has a chance to get rendered.
        def reposition(*args):
            self.top.wm_geometry('+%s+%s' % (
                anchor.winfo_rootx(),
                anchor.winfo_rooty())
            )
            if destroyAnchor:
                anchor.destroy()
        self.after_idle(reposition)

    def close(self, event):
        sel = self.list.curselection()
        if sel:
            item = self.list.get(sel[0])
        else:
            item = self.entry.get()

        # Note that the order of this appears to be significant: destroying
        # before selecting leaves the focus in a weird state.
        self.client.selected(item)
        self.top.destroy()
        return 'braek'

    def abort(self, event):
        self.top.destroy()
        self.client.aborted()
        return 'break'

    def up(self, event):
        sel = self.list.curselection()
        if not sel:
            self.list.selection_set(0)
            return 'break'
        sel = sel[0]

        print('sel is %s size is %s' % (sel, self.list.size()))
        if sel > 0:
            print('setting selection to %s' % sel)
            self.list.selection_clear(sel)
            self.list.selection_set(sel - 1)
            self.list.see(sel)
        return 'break'

    def down(self, event):
        sel = self.list.curselection()
        if not sel:
            self.list.selection_set(0)
            return 'break'
        sel = sel[0]

        print('sel is %s size is %s' % (sel, self.list.size()))
        if sel < self.list.size() - 1:
            print('setting selection to %s' % (sel + 1))
            self.list.selection_clear(sel)
            self.list.selection_set(sel + 1)
            self.list.see(sel)
        return 'break'

    def filter(self, event):
        """Filter the listbox based on the contents of the entryfield."""
        # first add the character to the entry.
        currentText = self.entry.get()
        print(event.keysym)
        if event.keysym == 'BackSpace':
            # Handle backspace specially.
            if currentText:
                currentText = currentText[:-1]
                self.entry.delete(0, 'end')
                self.entry.insert(0, currentText)
            else:
                return 'break'
        else:
            # Assume normal character. Insert it.
            self.entry.insert('insert', event.char)
            currentText += event.char

        self.list.delete(0, 'end')
        pattern = currentText.upper()
        for item in self.items:
            if pattern in item.upper():
                self.list.insert('end', item)

        return 'break'
Esempio n. 37
0
class ShiftReduceApp(object):
    """
    A graphical tool for exploring the shift-reduce parser.  The tool
    displays the parser's stack and the remaining text, and allows the
    user to control the parser's operation.  In particular, the user
    can shift tokens onto the stack, and can perform reductions on the
    top elements of the stack.  A "step" button simply steps through
    the parsing process, performing the operations that
    ``nltk.parse.ShiftReduceParser`` would use.
    """
    def __init__(self, grammar, sent, trace=0):
        self._sent = sent
        self._parser = SteppingShiftReduceParser(grammar, trace)

        # Set up the main window.
        self._top = Tk()
        self._top.title('Shift Reduce Parser Application')

        # Animations.  animating_lock is a lock to prevent the demo
        # from performing new operations while it's animating.
        self._animating_lock = 0
        self._animate = IntVar(self._top)
        self._animate.set(10) # = medium

        # The user can hide the grammar.
        self._show_grammar = IntVar(self._top)
        self._show_grammar.set(1)

        # Initialize fonts.
        self._init_fonts(self._top)

        # Set up key bindings.
        self._init_bindings()

        # Create the basic frames.
        self._init_menubar(self._top)
        self._init_buttons(self._top)
        self._init_feedback(self._top)
        self._init_grammar(self._top)
        self._init_canvas(self._top)

        # A popup menu for reducing.
        self._reduce_menu = Menu(self._canvas, tearoff=0)

        # Reset the demo, and set the feedback frame to empty.
        self.reset()
        self._lastoper1['text'] = ''

    #########################################
    ##  Initialization Helpers
    #########################################

    def _init_fonts(self, root):
        # See: <http://www.astro.washington.edu/owen/ROTKFolklore.html>
        self._sysfont = tkinter.font.Font(font=Button()["font"])
        root.option_add("*Font", self._sysfont)

        # TWhat's our font size (default=same as sysfont)
        self._size = IntVar(root)
        self._size.set(self._sysfont.cget('size'))

        self._boldfont = tkinter.font.Font(family='helvetica', weight='bold',
                                    size=self._size.get())
        self._font = tkinter.font.Font(family='helvetica',
                                    size=self._size.get())

    def _init_grammar(self, parent):
        # Grammar view.
        self._prodframe = listframe = Frame(parent)
        self._prodframe.pack(fill='both', side='left', padx=2)
        self._prodlist_label = Label(self._prodframe,
                                     font=self._boldfont,
                                     text='Available Reductions')
        self._prodlist_label.pack()
        self._prodlist = Listbox(self._prodframe, selectmode='single',
                                 relief='groove', background='white',
                                 foreground='#909090',
                                 font=self._font,
                                 selectforeground='#004040',
                                 selectbackground='#c0f0c0')

        self._prodlist.pack(side='right', fill='both', expand=1)

        self._productions = list(self._parser.grammar().productions())
        for production in self._productions:
            self._prodlist.insert('end', (' %s' % production))
        self._prodlist.config(height=min(len(self._productions), 25))

        # Add a scrollbar if there are more than 25 productions.
        if 1:#len(self._productions) > 25:
            listscroll = Scrollbar(self._prodframe,
                                   orient='vertical')
            self._prodlist.config(yscrollcommand = listscroll.set)
            listscroll.config(command=self._prodlist.yview)
            listscroll.pack(side='left', fill='y')

        # If they select a production, apply it.
        self._prodlist.bind('<<ListboxSelect>>', self._prodlist_select)

        # When they hover over a production, highlight it.
        self._hover = -1
        self._prodlist.bind('<Motion>', self._highlight_hover)
        self._prodlist.bind('<Leave>', self._clear_hover)

    def _init_bindings(self):
        # Quit
        self._top.bind('<Control-q>', self.destroy)
        self._top.bind('<Control-x>', self.destroy)
        self._top.bind('<Alt-q>', self.destroy)
        self._top.bind('<Alt-x>', self.destroy)

        # Ops (step, shift, reduce, undo)
        self._top.bind('<space>', self.step)
        self._top.bind('<s>', self.shift)
        self._top.bind('<Alt-s>', self.shift)
        self._top.bind('<Control-s>', self.shift)
        self._top.bind('<r>', self.reduce)
        self._top.bind('<Alt-r>', self.reduce)
        self._top.bind('<Control-r>', self.reduce)
        self._top.bind('<Delete>', self.reset)
        self._top.bind('<u>', self.undo)
        self._top.bind('<Alt-u>', self.undo)
        self._top.bind('<Control-u>', self.undo)
        self._top.bind('<Control-z>', self.undo)
        self._top.bind('<BackSpace>', self.undo)

        # Misc
        self._top.bind('<Control-p>', self.postscript)
        self._top.bind('<Control-h>', self.help)
        self._top.bind('<F1>', self.help)
        self._top.bind('<Control-g>', self.edit_grammar)
        self._top.bind('<Control-t>', self.edit_sentence)

        # Animation speed control
        self._top.bind('-', lambda e,a=self._animate:a.set(20))
        self._top.bind('=', lambda e,a=self._animate:a.set(10))
        self._top.bind('+', lambda e,a=self._animate:a.set(4))

    def _init_buttons(self, parent):
        # Set up the frames.
        self._buttonframe = buttonframe = Frame(parent)
        buttonframe.pack(fill='none', side='bottom')
        Button(buttonframe, text='Step',
               background='#90c0d0', foreground='black',
               command=self.step,).pack(side='left')
        Button(buttonframe, text='Shift', underline=0,
               background='#90f090', foreground='black',
               command=self.shift).pack(side='left')
        Button(buttonframe, text='Reduce', underline=0,
               background='#90f090', foreground='black',
               command=self.reduce).pack(side='left')
        Button(buttonframe, text='Undo', underline=0,
               background='#f0a0a0', foreground='black',
               command=self.undo).pack(side='left')

    def _init_menubar(self, parent):
        menubar = Menu(parent)

        filemenu = Menu(menubar, tearoff=0)
        filemenu.add_command(label='Reset Parser', underline=0,
                             command=self.reset, accelerator='Del')
        filemenu.add_command(label='Print to Postscript', underline=0,
                             command=self.postscript, accelerator='Ctrl-p')
        filemenu.add_command(label='Exit', underline=1,
                             command=self.destroy, accelerator='Ctrl-x')
        menubar.add_cascade(label='File', underline=0, menu=filemenu)

        editmenu = Menu(menubar, tearoff=0)
        editmenu.add_command(label='Edit Grammar', underline=5,
                             command=self.edit_grammar,
                             accelerator='Ctrl-g')
        editmenu.add_command(label='Edit Text', underline=5,
                             command=self.edit_sentence,
                             accelerator='Ctrl-t')
        menubar.add_cascade(label='Edit', underline=0, menu=editmenu)

        rulemenu = Menu(menubar, tearoff=0)
        rulemenu.add_command(label='Step', underline=1,
                             command=self.step, accelerator='Space')
        rulemenu.add_separator()
        rulemenu.add_command(label='Shift', underline=0,
                             command=self.shift, accelerator='Ctrl-s')
        rulemenu.add_command(label='Reduce', underline=0,
                             command=self.reduce, accelerator='Ctrl-r')
        rulemenu.add_separator()
        rulemenu.add_command(label='Undo', underline=0,
                             command=self.undo, accelerator='Ctrl-u')
        menubar.add_cascade(label='Apply', underline=0, menu=rulemenu)

        viewmenu = Menu(menubar, tearoff=0)
        viewmenu.add_checkbutton(label="Show Grammar", underline=0,
                                 variable=self._show_grammar,
                                 command=self._toggle_grammar)
        viewmenu.add_separator()
        viewmenu.add_radiobutton(label='Tiny', variable=self._size,
                                 underline=0, value=10, command=self.resize)
        viewmenu.add_radiobutton(label='Small', variable=self._size,
                                 underline=0, value=12, command=self.resize)
        viewmenu.add_radiobutton(label='Medium', variable=self._size,
                                 underline=0, value=14, command=self.resize)
        viewmenu.add_radiobutton(label='Large', variable=self._size,
                                 underline=0, value=18, command=self.resize)
        viewmenu.add_radiobutton(label='Huge', variable=self._size,
                                 underline=0, value=24, command=self.resize)
        menubar.add_cascade(label='View', underline=0, menu=viewmenu)

        animatemenu = Menu(menubar, tearoff=0)
        animatemenu.add_radiobutton(label="No Animation", underline=0,
                                    variable=self._animate, value=0)
        animatemenu.add_radiobutton(label="Slow Animation", underline=0,
                                    variable=self._animate, value=20,
                                    accelerator='-')
        animatemenu.add_radiobutton(label="Normal Animation", underline=0,
                                    variable=self._animate, value=10,
                                    accelerator='=')
        animatemenu.add_radiobutton(label="Fast Animation", underline=0,
                                    variable=self._animate, value=4,
                                    accelerator='+')
        menubar.add_cascade(label="Animate", underline=1, menu=animatemenu)


        helpmenu = Menu(menubar, tearoff=0)
        helpmenu.add_command(label='About', underline=0,
                             command=self.about)
        helpmenu.add_command(label='Instructions', underline=0,
                             command=self.help, accelerator='F1')
        menubar.add_cascade(label='Help', underline=0, menu=helpmenu)

        parent.config(menu=menubar)

    def _init_feedback(self, parent):
        self._feedbackframe = feedbackframe = Frame(parent)
        feedbackframe.pack(fill='x', side='bottom', padx=3, pady=3)
        self._lastoper_label = Label(feedbackframe, text='Last Operation:',
                                     font=self._font)
        self._lastoper_label.pack(side='left')
        lastoperframe = Frame(feedbackframe, relief='sunken', border=1)
        lastoperframe.pack(fill='x', side='right', expand=1, padx=5)
        self._lastoper1 = Label(lastoperframe, foreground='#007070',
                                background='#f0f0f0', font=self._font)
        self._lastoper2 = Label(lastoperframe, anchor='w', width=30,
                                foreground='#004040', background='#f0f0f0',
                                font=self._font)
        self._lastoper1.pack(side='left')
        self._lastoper2.pack(side='left', fill='x', expand=1)

    def _init_canvas(self, parent):
        self._cframe = CanvasFrame(parent, background='white',
                                   width=525, closeenough=10,
                                   border=2, relief='sunken')
        self._cframe.pack(expand=1, fill='both', side='top', pady=2)
        canvas = self._canvas = self._cframe.canvas()

        self._stackwidgets = []
        self._rtextwidgets = []
        self._titlebar = canvas.create_rectangle(0,0,0,0, fill='#c0f0f0',
                                                 outline='black')
        self._exprline = canvas.create_line(0,0,0,0, dash='.')
        self._stacktop = canvas.create_line(0,0,0,0, fill='#408080')
        size = self._size.get()+4
        self._stacklabel = TextWidget(canvas, 'Stack', color='#004040',
                                      font=self._boldfont)
        self._rtextlabel = TextWidget(canvas, 'Remaining Text',
                                      color='#004040', font=self._boldfont)
        self._cframe.add_widget(self._stacklabel)
        self._cframe.add_widget(self._rtextlabel)

    #########################################
    ##  Main draw procedure
    #########################################

    def _redraw(self):
        scrollregion = self._canvas['scrollregion'].split()
        (cx1, cy1, cx2, cy2) = [int(c) for c in scrollregion]

        # Delete the old stack & rtext widgets.
        for stackwidget in self._stackwidgets:
            self._cframe.destroy_widget(stackwidget)
        self._stackwidgets = []
        for rtextwidget in self._rtextwidgets:
            self._cframe.destroy_widget(rtextwidget)
        self._rtextwidgets = []

        # Position the titlebar & exprline
        (x1, y1, x2, y2) = self._stacklabel.bbox()
        y = y2-y1+10
        self._canvas.coords(self._titlebar, -5000, 0, 5000, y-4)
        self._canvas.coords(self._exprline, 0, y*2-10, 5000, y*2-10)

        # Position the titlebar labels..
        (x1, y1, x2, y2) = self._stacklabel.bbox()
        self._stacklabel.move(5-x1, 3-y1)
        (x1, y1, x2, y2) = self._rtextlabel.bbox()
        self._rtextlabel.move(cx2-x2-5, 3-y1)

        # Draw the stack.
        stackx = 5
        for tok in self._parser.stack():
            if isinstance(tok, Tree):
                attribs = {'tree_color': '#4080a0', 'tree_width': 2,
                           'node_font': self._boldfont,
                           'node_color': '#006060',
                           'leaf_color': '#006060', 'leaf_font':self._font}
                widget = tree_to_treesegment(self._canvas, tok,
                                             **attribs)
                widget.label()['color'] = '#000000'
            else:
                widget = TextWidget(self._canvas, tok,
                                    color='#000000', font=self._font)
            widget.bind_click(self._popup_reduce)
            self._stackwidgets.append(widget)
            self._cframe.add_widget(widget, stackx, y)
            stackx = widget.bbox()[2] + 10

        # Draw the remaining text.
        rtextwidth = 0
        for tok in self._parser.remaining_text():
            widget = TextWidget(self._canvas, tok,
                                color='#000000', font=self._font)
            self._rtextwidgets.append(widget)
            self._cframe.add_widget(widget, rtextwidth, y)
            rtextwidth = widget.bbox()[2] + 4

        # Allow enough room to shift the next token (for animations)
        if len(self._rtextwidgets) > 0:
            stackx += self._rtextwidgets[0].width()

        # Move the remaining text to the correct location (keep it
        # right-justified, when possible); and move the remaining text
        # label, if necessary.
        stackx = max(stackx, self._stacklabel.width()+25)
        rlabelwidth = self._rtextlabel.width()+10
        if stackx >= cx2-max(rtextwidth, rlabelwidth):
            cx2 = stackx + max(rtextwidth, rlabelwidth)
        for rtextwidget in self._rtextwidgets:
            rtextwidget.move(4+cx2-rtextwidth, 0)
        self._rtextlabel.move(cx2-self._rtextlabel.bbox()[2]-5, 0)

        midx = (stackx + cx2-max(rtextwidth, rlabelwidth))/2
        self._canvas.coords(self._stacktop, midx, 0, midx, 5000)
        (x1, y1, x2, y2) = self._stacklabel.bbox()

        # Set up binding to allow them to shift a token by dragging it.
        if len(self._rtextwidgets) > 0:
            def drag_shift(widget, midx=midx, self=self):
                if widget.bbox()[0] < midx: self.shift()
                else: self._redraw()
            self._rtextwidgets[0].bind_drag(drag_shift)
            self._rtextwidgets[0].bind_click(self.shift)

        # Draw the stack top.
        self._highlight_productions()

    def _draw_stack_top(self, widget):
        # hack..
        midx = widget.bbox()[2]+50
        self._canvas.coords(self._stacktop, midx, 0, midx, 5000)

    def _highlight_productions(self):
        # Highlight the productions that can be reduced.
        self._prodlist.selection_clear(0, 'end')
        for prod in self._parser.reducible_productions():
            index = self._productions.index(prod)
            self._prodlist.selection_set(index)

    #########################################
    ##  Button Callbacks
    #########################################

    def destroy(self, *e):
        if self._top is None: return
        self._top.destroy()
        self._top = None

    def reset(self, *e):
        self._parser.initialize(self._sent)
        self._lastoper1['text'] = 'Reset App'
        self._lastoper2['text'] = ''
        self._redraw()

    def step(self, *e):
        if self.reduce(): return True
        elif self.shift(): return True
        else:
            if list(self._parser.parses()):
                self._lastoper1['text'] = 'Finished:'
                self._lastoper2['text'] = 'Success'
            else:
                self._lastoper1['text'] = 'Finished:'
                self._lastoper2['text'] = 'Failure'

    def shift(self, *e):
        if self._animating_lock: return
        if self._parser.shift():
            tok = self._parser.stack()[-1]
            self._lastoper1['text'] = 'Shift:'
            self._lastoper2['text'] = '%r' % tok
            if self._animate.get():
                self._animate_shift()
            else:
                self._redraw()
            return True
        return False

    def reduce(self, *e):
        if self._animating_lock: return
        production = self._parser.reduce()
        if production:
            self._lastoper1['text'] = 'Reduce:'
            self._lastoper2['text'] = '%s' % production
            if self._animate.get():
                self._animate_reduce()
            else:
                self._redraw()
        return production

    def undo(self, *e):
        if self._animating_lock: return
        if self._parser.undo():
            self._redraw()

    def postscript(self, *e):
        self._cframe.print_to_file()

    def mainloop(self, *args, **kwargs):
        """
        Enter the Tkinter mainloop.  This function must be called if
        this demo is created from a non-interactive program (e.g.
        from a secript); otherwise, the demo will close as soon as
        the script completes.
        """
        if in_idle(): return
        self._top.mainloop(*args, **kwargs)

    #########################################
    ##  Menubar callbacks
    #########################################

    def resize(self, size=None):
        if size is not None: self._size.set(size)
        size = self._size.get()
        self._font.configure(size=-(abs(size)))
        self._boldfont.configure(size=-(abs(size)))
        self._sysfont.configure(size=-(abs(size)))

        #self._stacklabel['font'] = ('helvetica', -size-4, 'bold')
        #self._rtextlabel['font'] = ('helvetica', -size-4, 'bold')
        #self._lastoper_label['font'] = ('helvetica', -size)
        #self._lastoper1['font'] = ('helvetica', -size)
        #self._lastoper2['font'] = ('helvetica', -size)
        #self._prodlist['font'] = ('helvetica', -size)
        #self._prodlist_label['font'] = ('helvetica', -size-2, 'bold')
        self._redraw()

    def help(self, *e):
        # The default font's not very legible; try using 'fixed' instead.
        try:
            ShowText(self._top, 'Help: Shift-Reduce Parser Application',
                     (__doc__ or '').strip(), width=75, font='fixed')
        except:
            ShowText(self._top, 'Help: Shift-Reduce Parser Application',
                     (__doc__ or '').strip(), width=75)

    def about(self, *e):
        ABOUT = ("NLTK Shift-Reduce Parser Application\n"+
                 "Written by Edward Loper")
        TITLE = 'About: Shift-Reduce Parser Application'
        try:
            from tkinter.messagebox import Message
            Message(message=ABOUT, title=TITLE).show()
        except:
            ShowText(self._top, TITLE, ABOUT)

    def edit_grammar(self, *e):
        CFGEditor(self._top, self._parser.grammar(), self.set_grammar)

    def set_grammar(self, grammar):
        self._parser.set_grammar(grammar)
        self._productions = list(grammar.productions())
        self._prodlist.delete(0, 'end')
        for production in self._productions:
            self._prodlist.insert('end', (' %s' % production))

    def edit_sentence(self, *e):
        sentence = " ".join(self._sent)
        title = 'Edit Text'
        instr = 'Enter a new sentence to parse.'
        EntryDialog(self._top, sentence, instr, self.set_sentence, title)

    def set_sentence(self, sent):
        self._sent = sent.split() #[XX] use tagged?
        self.reset()

    #########################################
    ##  Reduce Production Selection
    #########################################

    def _toggle_grammar(self, *e):
        if self._show_grammar.get():
            self._prodframe.pack(fill='both', side='left', padx=2,
                                 after=self._feedbackframe)
            self._lastoper1['text'] = 'Show Grammar'
        else:
            self._prodframe.pack_forget()
            self._lastoper1['text'] = 'Hide Grammar'
        self._lastoper2['text'] = ''

    def _prodlist_select(self, event):
        selection = self._prodlist.curselection()
        if len(selection) != 1: return
        index = int(selection[0])
        production = self._parser.reduce(self._productions[index])
        if production:
            self._lastoper1['text'] = 'Reduce:'
            self._lastoper2['text'] = '%s' % production
            if self._animate.get():
                self._animate_reduce()
            else:
                self._redraw()
        else:
            # Reset the production selections.
            self._prodlist.selection_clear(0, 'end')
            for prod in self._parser.reducible_productions():
                index = self._productions.index(prod)
                self._prodlist.selection_set(index)

    def _popup_reduce(self, widget):
        # Remove old commands.
        productions = self._parser.reducible_productions()
        if len(productions) == 0: return

        self._reduce_menu.delete(0, 'end')
        for production in productions:
            self._reduce_menu.add_command(label=str(production),
                                          command=self.reduce)
        self._reduce_menu.post(self._canvas.winfo_pointerx(),
                               self._canvas.winfo_pointery())

    #########################################
    ##  Animations
    #########################################

    def _animate_shift(self):
        # What widget are we shifting?
        widget = self._rtextwidgets[0]

        # Where are we shifting from & to?
        right = widget.bbox()[0]
        if len(self._stackwidgets) == 0: left = 5
        else: left = self._stackwidgets[-1].bbox()[2]+10

        # Start animating.
        dt = self._animate.get()
        dx = (left-right)*1.0/dt
        self._animate_shift_frame(dt, widget, dx)

    def _animate_shift_frame(self, frame, widget, dx):
        if frame > 0:
            self._animating_lock = 1
            widget.move(dx, 0)
            self._top.after(10, self._animate_shift_frame,
                            frame-1, widget, dx)
        else:
            # but: stacktop??

            # Shift the widget to the stack.
            del self._rtextwidgets[0]
            self._stackwidgets.append(widget)
            self._animating_lock = 0

            # Display the available productions.
            self._draw_stack_top(widget)
            self._highlight_productions()

    def _animate_reduce(self):
        # What widgets are we shifting?
        numwidgets = len(self._parser.stack()[-1]) # number of children
        widgets = self._stackwidgets[-numwidgets:]

        # How far are we moving?
        if isinstance(widgets[0], TreeSegmentWidget):
            ydist = 15 + widgets[0].label().height()
        else:
            ydist = 15 + widgets[0].height()

        # Start animating.
        dt = self._animate.get()
        dy = ydist*2.0/dt
        self._animate_reduce_frame(dt/2, widgets, dy)

    def _animate_reduce_frame(self, frame, widgets, dy):
        if frame > 0:
            self._animating_lock = 1
            for widget in widgets: widget.move(0, dy)
            self._top.after(10, self._animate_reduce_frame,
                            frame-1, widgets, dy)
        else:
            del self._stackwidgets[-len(widgets):]
            for widget in widgets:
                self._cframe.remove_widget(widget)
            tok = self._parser.stack()[-1]
            if not isinstance(tok, Tree): raise ValueError()
            label = TextWidget(self._canvas, str(tok.label()), color='#006060',
                               font=self._boldfont)
            widget = TreeSegmentWidget(self._canvas, label, widgets,
                                       width=2)
            (x1, y1, x2, y2) = self._stacklabel.bbox()
            y = y2-y1+10
            if not self._stackwidgets: x = 5
            else: x = self._stackwidgets[-1].bbox()[2] + 10
            self._cframe.add_widget(widget, x, y)
            self._stackwidgets.append(widget)

            # Display the available productions.
            self._draw_stack_top(widget)
            self._highlight_productions()

#             # Delete the old widgets..
#             del self._stackwidgets[-len(widgets):]
#             for widget in widgets:
#                 self._cframe.destroy_widget(widget)
#
#             # Make a new one.
#             tok = self._parser.stack()[-1]
#             if isinstance(tok, Tree):
#                 attribs = {'tree_color': '#4080a0', 'tree_width': 2,
#                            'node_font': bold, 'node_color': '#006060',
#                            'leaf_color': '#006060', 'leaf_font':self._font}
#                 widget = tree_to_treesegment(self._canvas, tok.type(),
#                                              **attribs)
#                 widget.node()['color'] = '#000000'
#             else:
#                 widget = TextWidget(self._canvas, tok.type(),
#                                     color='#000000', font=self._font)
#             widget.bind_click(self._popup_reduce)
#             (x1, y1, x2, y2) = self._stacklabel.bbox()
#             y = y2-y1+10
#             if not self._stackwidgets: x = 5
#             else: x = self._stackwidgets[-1].bbox()[2] + 10
#             self._cframe.add_widget(widget, x, y)
#             self._stackwidgets.append(widget)

            #self._redraw()
            self._animating_lock = 0

    #########################################
    ##  Hovering.
    #########################################

    def _highlight_hover(self, event):
        # What production are we hovering over?
        index = self._prodlist.nearest(event.y)
        if self._hover == index: return

        # Clear any previous hover highlighting.
        self._clear_hover()

        # If the production corresponds to an available reduction,
        # highlight the stack.
        selection = [int(s) for s in self._prodlist.curselection()]
        if index in selection:
            rhslen = len(self._productions[index].rhs())
            for stackwidget in self._stackwidgets[-rhslen:]:
                if isinstance(stackwidget, TreeSegmentWidget):
                    stackwidget.label()['color'] = '#00a000'
                else:
                    stackwidget['color'] = '#00a000'

        # Remember what production we're hovering over.
        self._hover = index

    def _clear_hover(self, *event):
        # Clear any previous hover highlighting.
        if self._hover == -1: return
        self._hover = -1
        for stackwidget in self._stackwidgets:
            if isinstance(stackwidget, TreeSegmentWidget):
                stackwidget.label()['color'] = 'black'
            else:
                stackwidget['color'] = 'black'
Esempio n. 38
0
class Controller:
    SNAP_RADIUS = 5

    def __init__(self, tk: Tk):
        tk.title("Layered Polygons")

        menubar = Menu(tk)

        menu_file = Menu(menubar, tearoff=0)
        menu_file.add_command(label="New...", command=self._new_scene)
        menu_file.add_command(label="Open...", command=self._open_scene)
        menu_file.add_separator()
        menu_file.add_command(label="Save", command=self._save_scene)
        menu_file.add_command(label="Save As...", command=self._save_scene_as)
        menu_file.add_separator()
        menu_export = Menu(menu_file, tearoff=0)
        menu_export.add_command(label="Wavefront (.obj)...",
                                command=self._export_obj)
        menu_file.add_cascade(label="Export As", menu=menu_export)
        menu_file.add_separator()
        menu_file.add_command(label="Quit", command=self._quit_app)
        menubar.add_cascade(label="File", menu=menu_file)

        tk.config(menu=menubar)

        paned = PanedWindow(tk, relief=RAISED)
        paned.pack(fill=BOTH, expand=1)

        frame = Frame(paned)
        paned.add(frame)
        self._canvas = LayPolyCanvas(frame)
        bar_x = Scrollbar(frame, orient=HORIZONTAL)
        bar_x.pack(side=BOTTOM, fill=X)
        bar_x.config(command=self._canvas.xview)
        bar_y = Scrollbar(frame, orient=VERTICAL)
        bar_y.pack(side=RIGHT, fill=Y)
        bar_y.config(command=self._canvas.yview)
        self._canvas.config(xscrollcommand=bar_x.set, yscrollcommand=bar_y.set)
        self._canvas.pack(side=LEFT, expand=True, fill=BOTH)
        # Thanks to the two guys on Stack Overflow for that!
        # ( http://stackoverflow.com/a/7734187 )

        self._layer_list = Listbox(paned, selectmode=SINGLE)
        paned.add(self._layer_list)

        self._scene = None
        self._current_layer = None
        self._is_drawing_polygon = False
        self._tk = tk

        self._canvas.bind("<Button-1>", self._canvas_left_click)
        self._canvas.bind("<Button-3>", self._canvas_right_click)
        self._canvas.bind("<Motion>", self._canvas_mouse_moved)
        self._layer_list.bind("<<ListboxSelect>>", self._layer_change)

        self._current_path = None

    def _canvas_left_click(self, event):
        if not self._scene or not self._current_layer:
            return
        x, y = self._canvas.window_to_canvas_coords(event.x, event.y)

        if self._is_drawing_polygon:
            polygon = self._current_layer.get_polygon_at(-1)

            # Move vtx away from mouse to not interfere with search for closest
            polygon.get_vertex_at(-1).\
                set_coords(x-self.SNAP_RADIUS, y-self.SNAP_RADIUS)

            closest_vertex = self._current_layer.get_closest_vertex(
                x, y, self.SNAP_RADIUS)

            if closest_vertex:
                polygon.remove_vertex_at(-1)
                if closest_vertex is polygon.get_vertex_at(0):
                    self._is_drawing_polygon = False
                else:
                    polygon.add_vertex(closest_vertex)
                    polygon.add_vertex(Vertex(x, y))
            else:
                polygon.get_vertex_at(-1)\
                    .set_coords(x, y)
                polygon.add_vertex(Vertex(x, y))

            self._canvas.notify_polygon_change(self._current_layer
                                               .get_polygon_count()-1)
        else:
            # Create start vertex or use already existing one
            start_vertex = self._current_layer\
                .get_closest_vertex(x, y, self.SNAP_RADIUS)
            if not start_vertex:
                start_vertex = Vertex(x, y)

            # Vertex for mouse cursor
            next_vertex = Vertex(x, y)

            self._current_layer.add_polygon(
                Polygon([start_vertex, next_vertex]))

            self._is_drawing_polygon = True

            self._canvas.notify_layer_change()

    def _canvas_right_click(self, event):
        if not self._current_layer:
            return

        if self._is_drawing_polygon:
            self._current_layer.remove_polygon_at(-1)
            self._is_drawing_polygon = False
        else:
            x, y = self._canvas.window_to_canvas_coords(event.x, event.y)
            for i in range(0, self._current_layer.get_polygon_count()):
                if self._current_layer.get_polygon_at(i).contains(x, y):
                    self._current_layer.remove_polygon_at(i)
                    break

        self._canvas.notify_layer_change()

    def _canvas_mouse_moved(self, event):
        if self._is_drawing_polygon:
            x, y = self._canvas.window_to_canvas_coords(event.x, event.y)
            self._current_layer.get_polygon_at(-1).get_vertex_at(-1)\
                .set_coords(x, y)
            self._canvas.notify_polygon_change(self._current_layer
                                               .get_polygon_count()-1)

    def _layer_change(self, event):
        selection = self._layer_list.curselection()
        if len(selection) > 0 and self._scene:
            layer = self._scene.get_layer_at(selection[0])
            if layer:
                self._is_drawing_polygon = False
                self._current_layer = layer
                self._canvas.notify_new_layer(self._current_layer)

    def _set_scene(self, scene: Scene) -> bool:
        if scene.get_layer_count() <= 0:
            messagebox.showerror("Error!", "Scene has no layers!")
            return False

        self._scene = scene

        # Prepare canvas
        # TODO Extra 10px padding for canvas
        width, height = self._scene.get_size()
        self._canvas.config(scrollregion=(0, 0, width, height))

        # Empty listbox, fill it, select first entry
        self._layer_list.delete(0, self._layer_list.size()-1)
        for i in range(0, self._scene.get_layer_count()):
            self._layer_list.insert(i, self._scene.get_layer_at(i).get_name())
        self._layer_list.selection_set(0)
        self._layer_list.event_generate("<<ListboxSelect>>")

        return True

    def _set_current_path(self, path):
        self._current_path = path
        if path:
            self._tk.title(path + " - Layered Polygons")
        else:
            self._tk.title("Untitled - Layered Polygons")

    def _new_scene(self):
        path = filedialog.askopenfilename(defaultextension=".ora",
                                          filetypes=[("OpenRaster files",
                                                      ".ora")])
        if not path:
            return

        scene = ora.read(path)
        if not scene:
            messagebox.showerror("Error!", "File could not be opened!")
            return

        if self._set_scene(scene):
            self._set_current_path(None)

    def _open_scene(self):
        path = filedialog.askopenfilename(defaultextension=".lp",
                                          filetypes=[("Layered polygons"
                                                      " files", ".lp")])
        if not path:
            return

        scene = lp.read(path)
        if not scene:
            messagebox.showerror("Error!", "File could not be opened!")
            return

        if self._set_scene(scene):
            self._set_current_path(path)

    def _save_scene_help(self, path):
        if lp.save(path, self._scene):
            self._set_current_path(path)
        else:
            messagebox.showerror("Error!", "File could not be saved!")

    def _save_scene(self):
        if not self._current_path:
            self._save_scene_as()
            return
        self._save_scene_help(self._current_path)

    def _save_scene_as(self):
        if not self._scene:
            return
        path = filedialog.asksaveasfilename(defaultextension=".lp",
                                            filetypes=[("Layered polygons"
                                                        " files", ".lp")])
        if path:
            self._save_scene_help(path)

    def _export_obj(self):
        if not self._scene:
            return

        path_obj = filedialog.asksaveasfilename(defaultextension=".obj",
                                                filetypes=[("Wavefront object"
                                                            " files",
                                                            ".obj")])
        if not path_obj:
            return
        path_obj, path_mtl, path_data = obj.get_paths(path_obj)
        obj.save(path_obj, path_mtl, path_data, self._scene)

    def _quit_app(self):
        self._tk.quit()
        exit()
class Add_Recipe_Modal(Modal):
	def __init__(self, parent=None, title="Add Recipe"):
		self.shorts = []
		self.amounts = None
		self.ingredients = None
		Modal.__init__(self, parent, title, geometry="375x410" if system() == "Windows" else "375x350")

	def initialize(self):
		amount_label = Label(self, width=8, text="Amount")
		amount_label.grid(row=0, column=0)

		ingredients_label = Label(self, width=35, text="Item Name")
		ingredients_label.grid(row=0, column=1)

		self.amounts_list = Listbox(self, width=8, selectmode="single")
		self.amounts_list.bind("<Double-Button-1>", self.edit)
		self.amounts_list.grid(row=1, column=0, sticky="E")

		self.ingredients_list = Listbox(self, width=35, selectmode="single")
		self.ingredients_list.bind("<Double-Button-1>", self.edit)
		self.ingredients_list.grid(row=1, column=1)

		add_button = Button(self, width=7, text="Add", command=self.add)
		self.bind("<Control-+>", self.add)
		self.bind("<Insert>", self.add)
		add_button.grid(row=2, column=1, sticky="E")

		remove_button = Button(self, text="Remove", command=self.remove)
		self.bind("<Delete>", self.remove)
		remove_button.grid(row=2, column=0)

		produces_label = Label(self, text="Produces: ")
		produces_label.grid(row=3, column=0, sticky="W")

		self.produces_box = Entry(self, width=5)
		self.produces_box.insert(0, "1")
		self.produces_box.grid(row=3, column=1, sticky="W")

		machine_label = Label(self, text="Machine: ")
		machine_label.grid(row=4, column=0, sticky="W")

		self.machine_box = Entry(self)
		self.machine_box.grid(row=4, column=1, sticky="EW")

		info_label = Label(self, text="Extra Info: ")
		info_label.grid(row=5, column=0, sticky="W")

		self.info_box = Entry(self)
		self.info_box.grid(row=5, column=1, sticky="EW")

		cancel_button = Button(self, text="Cancel", width=7, command=self.cancel)
		self.bind("<Escape>", self.cancel)
		cancel_button.grid(row=6, column=0, pady=10)

		ok_button = Button(self, text="OK", width=7, command=self.ok)
		self.bind("<Return>", self.ok)
		ok_button.grid(row=6, column=1, pady=10, sticky="E")

		self.bind("<<ListboxSelect>>", self.sync)

	def sync(self, event):
		if event.widget is self.amounts_list and len(self.amounts_list.curselection()) != 0:
			self.ingredients_list.selection_set(self.amounts_list.curselection()[0])

	def add(self, event=None):
		short, name, amount = Add_Ingredient_Modal().show()
		if short is not None and name is not None and amount is not None:
			if name not in self.ingredients_list.get(0, "end"):
				self.ingredients_list.insert("end", name)
				self.amounts_list.insert("end", amount)
				self.shorts.append(short)

	def edit(self, event=None):
		if len(self.ingredients_list.curselection()) != 0:
			index = self.ingredients_list.curselection()[0]
			current_short = self.shorts[index]
			current_name = self.ingredients_list.get(index)
			current_amount = self.amounts_list.get(index)
			new_short, new_name, new_amount = Edit_Ingredient_Modal().show(current_short, current_name, current_amount)
			if new_short is not None and new_name is not None and new_amount is not None:
				self.amounts_list.delete(index)
				self.ingredients_list.delete(index)
				self.amounts_list.insert(index, new_amount)
				self.ingredients_list.insert(index, new_name)
				self.shorts[index] = new_short


	def remove(self, event=None):
		if len(self.ingredients_list.curselection()) != 0:
			slct = int(self.ingredients_list.curselection()[0])
			self.ingredients_list.delete(slct)
			self.amounts_list.delete(slct)
			del(self.shorts[slct])

	def ok(self, event=None):
		if len(self.ingredients_list.get(0)) != 0 and self.produces_box is not None and self.produces_box.get().isdigit():
			self.produces = int(self.produces_box.get())
			self.amounts = self.amounts_list.get(0, last="end")
			self.ingredients = self.ingredients_list.get(0, last="end")
			self.machine = self.machine_box.get()
			self.info = self.info_box.get()
			self.destroy()

	def cancel(self, event=None):
		self.amounts = None
		self.ingredients = None
		self.shorts = None
		self.produces = None
		self.machine = None
		self.info = None
		self.destroy()

	def show(self):
		Modal.show(self)
		return self.shorts, self.ingredients, self.amounts, self.produces, self.machine, self.info
Esempio n. 40
0
class ListChooserDialog:

	def __init__(self, parent, listEntries, guiPrompt, allowManualFolderSelection, forceFolderOnlyChoosing=False):
		"""
		NOTE: do not call this constructor directly. Use the ListChooserDialog.showDialog function instead.
		"""
		self.forceFolderOnlyChoosing = forceFolderOnlyChoosing

		self.top = tkinter.Toplevel(parent)
		defaultPadding = {"padx":20, "pady":10}

		# Set a description for this dialog (eg "Please choose a game to mod"
		tkinter.Label(self.top, text=guiPrompt).pack()

		# Define the main listbox to hold the choices given by the 'listEntries' parameter
		listboxFrame = tkinter.Frame(self.top)

		# Define a scrollbar and a listbox. The yscrollcommand is so that the listbox can control the scrollbar
		scrollbar = tkinter.Scrollbar(listboxFrame, orient=tkinter.VERTICAL)
		self.listbox = Listbox(listboxFrame, selectmode=tkinter.BROWSE, yscrollcommand=scrollbar.set)

		# Also configure the scrollbar to control the listbox, and pack it
		scrollbar.config(command=self.listbox.yview)
		scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)

		# Setting width to 0 forces auto-resize of listbox see: https://stackoverflow.com/a/26504193/848627
		for item in listEntries:
			self.listbox.insert(tkinter.END, item)
		self.listbox.config(width=0)
		self.listbox.pack(side=tkinter.LEFT, fill=tkinter.BOTH, expand=1)

		# Finally, pack the Frame so its contents are displayed on the dialog
		listboxFrame.pack(**defaultPadding)

		# If the user is allowed to choose a directory manually, add directory chooser button
		if allowManualFolderSelection:
			b2 = tkinter.Button(self.top, text="Choose Folder Manually", command=self.showDirectoryChooser)
			b2.pack(**defaultPadding)

		# Add an 'OK' button. When pressed, the dialog is closed
		b = tkinter.Button(self.top, text="OK", command=self.ok)
		b.pack(**defaultPadding)

		# This variable stores the returned value from the dialog
		self.result = None

	def showDirectoryChooser(self):
		if IS_MAC and not self.forceFolderOnlyChoosing:
			self.result = filedialog.askopenfilename(filetypes=[(None, "com.apple.application")])
		else:
			self.result = filedialog.askdirectory()
		self.top.destroy()

	def ok(self):
		"""
		This function is called when the 'OK' button is pressed. It retrieves the value of the currently selected item,
		then closes the dialog
		:return:
		"""
		selected_value = None

		if len(self.listbox.curselection()) > 0:
			selected_index = self.listbox.curselection()[0]
			selected_value = self.listbox.get(selected_index)

		self.result = selected_value

		self.top.destroy()

	@staticmethod
	def showDialog(rootGUIWindow, choiceList, guiPrompt, allowManualFolderSelection, forceFolderOnlyChoosing=False):
		"""
		Static helper function to show dialog and get a return value. Arguments are the same as constructor
		:param rootGUIWindow: the parent tkinter object of the dialog (can be root window)
		:param choiceList: a list of strings that the user is to choose from
		:param guiPrompt: the description that will be shown on the dialog
		:param allowManualFolderSelection: if true, user is allowed to select a folder manually.
		:return: returns the value the user selected (string), or None if none available
		"""
		d = ListChooserDialog(rootGUIWindow, choiceList, guiPrompt, allowManualFolderSelection, forceFolderOnlyChoosing)
		rootGUIWindow.wait_window(d.top)
		return d.result
Esempio n. 41
0
class ListboxVidget(Vidget, Eventor):
    """
    ListboxVidget contains a Listbox widget. It adds the following abilities:
    - Store items of any type, unlike Listbox widget that only stores texts.
    - Remember selected item even if the listbox widget lost focus.
    - Notify pre-change and post-change events.
    """

    # Error raised when trying to change the listbox while a change is going on
    class CircularCallError(ValueError):
        pass

    # Error raised when trying to change the listbox while it is disabled
    class DisabledError(ValueError):
        pass

    # Event notified when the listbox's items are to be changed
    ITEMS_CHANGE_SOON = 'ITEMS_CHANGE_SOON'

    # Event notified when the listbox's items are changed
    ITEMS_CHANGE_DONE = 'ITEMS_CHANGE_DONE'

    # Event notified when the listbox's active item is to be changed
    ITEMCUR_CHANGE_SOON = 'ITEMCUR_CHANGE_SOON'

    # Event notified when the listbox's active item is changed
    ITEMCUR_CHANGE_DONE = 'ITEMCUR_CHANGE_DONE'

    # Events list
    EVENTS = (
        ITEMS_CHANGE_SOON,
        ITEMS_CHANGE_DONE,
        ITEMCUR_CHANGE_SOON,
        ITEMCUR_CHANGE_DONE,
    )

    def __init__(
        self,
        items=None,
        item_to_text=None,
        normal_bg='',
        normal_fg='',
        active_bg='sky blue',
        active_fg='white',
        selected_bg='steel blue',
        selected_fg='white',
        master=None,
    ):
        """
        Initialize object.

        @param items: Items list.

        @param item_to_text: Item-to-text function. Default is `str`.

        @param normal_bg: Unselected item background color.

        @param normal_fg: Unselected item foreground color.

        @param active_bg: Active item background color. `Active` means the item
        is selected (in general meaning) but the listbox has no focus.

        @param active_fg: Active item foreground color. `Active` means the item
        is selected (in general meaning) but the listbox has no focus.

        @param selected_bg: Selected item background color. `Selected` means
        the item is selected (in general meaning) and the listbox has focus.

        @param selected_fg: Selected item foreground color. `Selected` means
        the item is selected (in general meaning) and the listbox has focus.

        @param master: Master widget.

        @return: None.
        """
        # Initialize Vidget.
        # Create main frame widget.
        Vidget.__init__(
            self,
            master=master,
        )

        # Initialize Eventor
        Eventor.__init__(self)

        # If items list is given
        if items is not None:
            # If items list is not list
            if not isinstance(items, list):
                # Raise error
                raise TypeError(items)

            # If items list is list.

        # If items list is not given, or items list is given and is list

        # Items list
        self._items = items if items is not None else []

        # Item-to-text function. Default is `str`.
        self._item_to_text = item_to_text if item_to_text is not None else str

        # Unselected item background color
        self._normal_fg = normal_fg

        # Unselected item foreground color
        self._normal_bg = normal_bg

        # Active item background color
        self._active_fg = active_fg

        # Active item foreground color
        self._active_bg = active_bg

        # Selected item background color
        self._selected_fg = selected_fg

        # Selected item foreground color
        self._selected_bg = selected_bg

        # Whether the listbox is changing
        self._is_changing = False

        # Active index. `-1` means void, i.e. no item is active.
        self._indexcur = -1

        # Whether active index is being reset to same value
        self._is_resetting = False

        # Create listbox widget
        self._listbox = Listbox(
            master=self.widget(),
            relief='groove',
            activestyle='none',
            highlightthickness=0,
            # Active index cache only supports single-selection mode for now.
            # See 2N6OR.
            selectmode='single',
        )

        # Set the listbox widget as config target
        self.config_target_set(self._listbox)

        # Create x-axis scrollbar
        self._scrollbar_xview = _HiddenScrollbar(
            self.widget(),
            orient=HORIZONTAL,
        )

        # Create y-axis scrollbar
        self._scrollbar_yview = _HiddenScrollbar(
            self.widget(),
            orient=VERTICAL,
        )

        # Mount scrollbars
        self._listbox.config(xscrollcommand=self._scrollbar_xview.set)

        self._listbox.config(yscrollcommand=self._scrollbar_yview.set)

        self._scrollbar_xview.config(command=self._listbox.xview)

        self._scrollbar_yview.config(command=self._listbox.yview)

        # Bind single-click event handler
        self._listbox.bind('<Button-1>', self._on_single_click)

        # Bind double-click event handler
        self._listbox.bind('<Double-Button-1>', self._on_double_click)

        # Update listbox widget
        self._listbox_widget_update(keep_active=False)

        # Update widget
        self._widget_update()

    def _widget_update(self):
        """
        Update widget.

        @return: None.
        """
        # Row 0 for listbox and y-axis scrollbar
        self.widget().rowconfigure(0, weight=1)

        # Row 1 for x-axis scrollbar
        self.widget().rowconfigure(1, weight=0)

        # Column 0 for listbox and x-axis scrollbar
        self.widget().columnconfigure(0, weight=1)

        # Column 1 for y-axis scrollbar
        self.widget().columnconfigure(1, weight=0)

        # Lay out listbox
        self._listbox.grid(row=0, column=0, sticky='NSEW')

        # Lay out x-axis scrollbar
        self._scrollbar_xview.grid(row=1, column=0, sticky='EW')

        # Lay out y-axis scrollbar
        self._scrollbar_yview.grid(row=0, column=1, sticky='NS')

    def is_enabled(self):
        """
        Test whether the listbox is enabled.

        @return: Boolean.
        """
        # Return whether the listbox is enabled
        return self._listbox.config('state')[4] != DISABLED

    def is_changing(self):
        """
        Test whether the listbox is changing.

        @return: Boolean.
        """
        # Return whether the listbox is changing
        return self._is_changing

    def is_resetting(self):
        """
        Test whether the listbox is setting active index to the same value.

        @return: Boolean.
        """
        # Return whether the listbox is setting active index to the same value
        return self._is_resetting

    def size(self):
        """
        Get number of items.

        @return: Number of items.
        """
        # Return number of items
        return len(self._items)

    def items(self):
        """
        Get items list.
        Notice do not change the list outside.

        @return: Items list.
        """
        # Return items list
        return self._items

    def items_set(
        self,
        items,
        notify=True,
        keep_active=False,
    ):
        """
        Set items list.

        Notice do not change the list outside.

        @param items: Items list.

        @param notify: Whether notify pre-change and post-change events.

        @param keep_active: Whether keep or clear active index.

        @return: None.
        """
        # If the items is not list
        if not isinstance(items, list):
            # Raise error
            raise TypeError(items)

        # If the items is list.

        # If the listbox is disabled
        if not self.is_enabled():
            # Raise error
            raise ListboxVidget.DisabledError()

        # If the listbox is not disabled.

        # If the listbox is changing
        if self._is_changing:
            # Raise error
            raise ListboxVidget.CircularCallError()

        # If the listbox is not changing.

        # Set changing flag on
        self._is_changing = True

        # If notify events
        if notify:
            # Notify pre-change event
            self.handler_notify(self.ITEMS_CHANGE_SOON)

        # Store the new items
        self._items = items

        # Update listbox widget
        self._listbox_widget_update(
            keep_active=keep_active
        )

        # If notify events
        if notify:
            # Notify post-change event
            self.handler_notify(self.ITEMS_CHANGE_DONE)

        # Set changing flag off
        self._is_changing = False

    def index_is_valid(self, index):
        """
        Test whether given index is valid. Notice -1 is not valid.

        @param index: Index to test.

        @return: Boolean.
        """
        # Test whether given index is valid
        return 0 <= index and index < self.size()

    def index_is_valid_or_void(self, index):
        """
        Test whether given index is valid or is -1.

        @param index: Index to test.

        @return: Boolean.
        """
        # Test whether given index is valid or is -1
        return index == -1 or self.index_is_valid(index)

    def index_first(self):
        """
        Get the first item's index.

        @return: First item's index, or -1 if the listbox is empty.
        """
        # Return the first item's index
        return 0 if self.size() > 0 else -1

    def index_last(self):
        """
        Get the last item's index.

        @return: Last item's index, or -1 if the listbox is empty.
        """
        # Return the last item's index
        return self.size() - 1

    def indexcur(self, internal=False, raise_error=False):
        """
        Get the active index.

        @param internal: See 2N6OR.

        @return: The active index. If no active active, either return -1, or
        raise IndexError if `raise_error` is True.
        """
        # Get active indexes
        indexcurs = self._indexcurs(internal=internal)

        # If have active indexes
        if indexcurs:
            # Return the first active index
            return indexcurs[0]

        # If no active indexes
        else:
            # If raise error
            if raise_error:
                # Raise error
                raise IndexError(-1)

            # If not raise error
            else:
                # Return -1
                return -1

    def _indexcurs(self, internal=False):
        """
        Get active indexes list.

        2N6OR
        @param internal: Whether use listbox widget's selected indexes, instead
        of cached active index.
        Notice listbox widget has no selected indexes if it has no focus.
        Notice using cached active index only supports single-selection mode,
        which means the result list has at most one index.

        @return: Active indexes list.
        """
        # If use listbox widget's selected indexes
        if internal:
            # Return listbox widget's selected indexes list
            return [int(x) for x in self._listbox.curselection()]

        # If not use listbox widget's selected indexes
        else:
            # If cached active index is valid
            if self.index_is_valid(self._indexcur):
                # Return a list with the cached active index
                return [self._indexcur]

            # If cached active index is not valid
            else:
                # Return empty list
                return []

    def indexcur_set(
        self,
        index,
        focus=False,
        notify=True,
        notify_arg=None,
    ):
        """
        Set active index.

        @param index: The index to set.

        @param focus: Whether set focus on the listbox widget.

        @param notify: Whether notify pre-change and post-change events.

        @param notify_arg: Event argument.

        @return: None.
        """
        # If the index is not valid or -1
        if not self.index_is_valid_or_void(index):
            # Raise error
            raise IndexError(index)

        # If the index is valid or is -1.

        # If the listbox is not enabled
        if not self.is_enabled():
            # Raise error
            raise ListboxVidget.DisabledError()

        # If the listbox is enabled.

        # If the listbox is changing
        if self._is_changing:
            # Raise error
            raise ListboxVidget.CircularCallError()

        # If the listbox is not changing.

        # Set changing flag on
        self._is_changing = True

        # Get old active index
        old_indexcur = self._indexcur

        # Set resetting flag on if new and old indexes are equal
        self._is_resetting = (index == old_indexcur)

        # If notify events
        if notify:
            # Notify pre-change event
            self.handler_notify(self.ITEMCUR_CHANGE_SOON, notify_arg)

        # If old active index is valid
        if self.index_is_valid(old_indexcur):
            # Set old active item's background color to normal color
            self._listbox.itemconfig(old_indexcur, background=self._normal_bg)

            # Set old active item's foreground color to normal color
            self._listbox.itemconfig(old_indexcur, foreground=self._normal_fg)

        # Cache new active index
        self._indexcur = index

        # Clear listbox widget's selection
        self._listbox.selection_clear(0, END)

        # Set listbox widget's selection
        self._listbox.selection_set(index)

        # Set listbox widget's activated index
        self._listbox.activate(index)

        # If new active index is valid
        if index != -1:
            # Set new active item's background color to active color
            self._listbox.itemconfig(index, background=self._active_bg)

            # Set new active item's foreground color to active color
            self._listbox.itemconfig(index, foreground=self._active_fg)

        # If set focus
        if focus:
            # Set focus on the listbox widget
            self._listbox.focus_set()

        # If new active index is valid
        if index != -1:
            # Make the active item visible
            self._listbox.see(index)

        # If notify events
        if notify:
            # Notify post-change event
            self.handler_notify(self.ITEMCUR_CHANGE_DONE, notify_arg)

        # Set resetting flag off
        self._is_resetting = False

        # Set changing flag off
        self._is_changing = False

    def indexcur_set_by_event(
        self,
        event,
        focus=False,
        notify=True,
        notify_arg=None,
    ):
        """
        Set active index using a Tkinter event object that contains coordinates
        of the active item.

        @param event: Tkinter event object.

        @param focus: Whether set focus on the listbox widget.

        @param notify: Whether notify pre-change and post-change events.

        @param notify_arg: Event argument.

        @return: None.
        """
        # Get the event's y co-ordinate's nearest listbox item index
        index = self._listbox.nearest(event.y)

        # If the index is not valid
        if not self.index_is_valid_or_void(index):
            # Ignore the event
            return

        # If the index is valid
        else:
            # Set the index as active index
            self.indexcur_set(
                index=index,
                focus=focus,
                notify=notify,
                notify_arg=notify_arg,
            )

    def item(self, index):
        """
        Get item at given index.

        @return: Item at given index, or IndexError if the index is not valid.
        """
        return self.items()[index]

    def itemcur(self, internal=False, raise_error=False):
        """
        Get the active item.

        @param internal: See 2N6OR.

        @param raise_error: Whether raise error if no active item.

        @return: The active item. If no active item, if `raise_error` is
        True, raise IndexError, otherwise return None.
        """
        # Get active index.
        # May raise IndexError if `raise_error` is True.
        indexcur = self.indexcur(
            internal=internal,
            raise_error=raise_error,
        )

        # If no active index
        if indexcur == -1:
            # Return None
            return None

        # If have active index
        else:
            # Return the active item
            return self.items()[indexcur]

    def item_insert(
        self,
        item,
        index=None,
        notify=True,
        keep_active=True,
    ):
        """
        Insert item at given index.

        @param item: Item to insert.

        @param index: Index to insert. `None` means active index, and if no
        active index, insert at the end.

        @param notify: Whether notify pre-change and post-change events.

        @param keep_active: Whether keep or clear active index.

        @return: None.
        """
        # If notify events
        if notify:
            # Notify pre-change events
            self.handler_notify(self.ITEMCUR_CHANGE_SOON)

            self.handler_notify(self.ITEMS_CHANGE_SOON)

        # Get old active index
        active_index = self.indexcur()

        # If the index is None,
        # it means use active index.
        if index is None:
            # Use active index.
            # `-1` works and means appending.
            index = active_index

        # Insert the item to the items list
        self._items.insert(index, item)

        # If old active index is valid
        if active_index != -1:
            # If old active index is GE the inserted index
            if active_index >= index:
                # Shift active index by one
                active_index += 1

            # If old active index is not GE the inserted index, use it as-is.

        # Set new active index
        self.indexcur_set(index=active_index, notify=False)

        # Update listbox widget
        self._listbox_widget_update(
            keep_active=keep_active
        )

        # If notify events
        if notify:
            # Notify post-change events
            self.handler_notify(self.ITEMS_CHANGE_DONE)

            self.handler_notify(self.ITEMCUR_CHANGE_DONE)

    def item_remove(
        self,
        index,
        notify=True,
        keep_active=True,
    ):
        """
        Remove item at given index.

        @param index: Index to remove.

        @param notify: Whether notify pre-change and post-change events.

        @param keep_active: Whether keep or clear active index.

        @return: None.
        """
        # If the index is not valid
        if not self.index_is_valid(index):
            # Raise error
            raise ValueError(index)

        # If the index is valid.

        # If notify events
        if notify:
            # Notify pre-change events
            self.handler_notify(self.ITEMCUR_CHANGE_SOON)

            self.handler_notify(self.ITEMS_CHANGE_SOON)

        # Get old active index
        active_index = self.indexcur()

        # Remove item at the index
        del self._items[index]

        # If old active index is valid
        if active_index != -1:
            # Get the last index
            index_last = self.index_last()

            # If old active index is GT the last index
            if active_index > index_last:
                # Use the last index as new active index
                active_index = index_last

            # If old active index is not GT the last index, use it as-is.

        # Set new active index
        self.indexcur_set(index=active_index, notify=False)

        # Update listbox widget
        self._listbox_widget_update(
            keep_active=keep_active
        )

        # If notify events
        if notify:
            # Notify post-change events
            self.handler_notify(self.ITEMS_CHANGE_DONE)

            self.handler_notify(self.ITEMCUR_CHANGE_DONE)

    def handler_add(
        self,
        event,
        handler,
        need_arg=False,
    ):
        """
        Add event handler for an event.
        If the event is ListboxVidget event, add the event handler to Eventor.
        If the event is not ListboxVidget event, add the event handler to
        listbox widget.

        Notice this method overrides `Eventor.handler_add` in order to add
        non-ListboxVidget event handler to listbox widget.

        @param event: Event name.

        @param handler: Event handler.

        @param need_arg: Whether the event handler needs event argument.

        @return: None.
        """
        # If the event is ListboxVidget event
        if event in self.EVENTS:
            # Add the event handler to Eventor
            return Eventor.handler_add(
                self,
                event=event,
                handler=handler,
                need_arg=need_arg,
            )

        # If the event is not ListboxVidget event,
        # it is assumed to be Tkinter widget event.
        else:
            # Add the event handler to listbox widget
            return self.bind(
                event=event,
                handler=handler,
            )

    def bind(
        self,
        event,
        handler,
    ):
        """
        Add event handler to listbox widget.

        ListboxVidget internally uses `<Button-1>` and `<Double-Button-1>` to
        capture active index changes. So if the given event is `<Button-1>` or
        `<Double-Button-1>`, the given handler will be wrapped.

        @param event: Event name.

        @param handler: Event handler.

        @return: None.
        """
        # If the event is not `<Button-1>` or `<Double-Button-1>`
        if event not in ['<Button-1>', '<Double-Button-1>']:
            # Add the event handler to listbox widget
            self._listbox.bind(event, handler)

        # If the event is `<Button-1>` or `<Double-Button-1>`
        else:
            # Create event handler wrapper
            def handler_wrapper(e):
                """
                Event handler wrapper that sets new active index and then calls
                the wrapped event handler.

                Setting new active index is needed because when this handler is
                called by Tkinter, the active index of the listbox is still
                old.

                @param e: Tkinter event object.

                @return: None.
                """
                # Set new active index
                self.indexcur_set_by_event(e, notify=True)

                # Call the wrapped event handler
                handler(e)

            # Add the event handler wrapper to the listbox widget
            self._listbox.bind(event, handler_wrapper)

    def _on_single_click(self, event):
        """
        `<Button-1>` event handler that updates active index.

        @param event: Tkinter event object.

        @return: None.
        """
        # Updates active index
        self.indexcur_set_by_event(event, notify=True)

    def _on_double_click(self, event):
        """
        `<Double-Button-1>` event handler that updates active index.

        @param event: Tkinter event object.

        @return: None.
        """
        # Updates active index
        self.indexcur_set_by_event(event, notify=True)

    def _listbox_widget_update(
        self,
        keep_active,
    ):
        """
        Update listbox widget's items and selection.

        @param keep_active: Whether keep or clear active index.

        @return: None.
        """
        # Remove old items from listbox widget
        self._listbox.delete(0, END)

        # Insert new items into listbox widget.
        # For each ListboxVidget items.
        for index, item in enumerate(self.items()):
            # Get item text
            item_text = self._item_to_text(item)

            # Insert the item text into listbox widget
            self._listbox.insert(index, item_text)

            # Set the item's normal background color
            self._listbox.itemconfig(index, background=self._normal_bg)

            # Set the item's normal foreground color
            self._listbox.itemconfig(index, foreground=self._normal_fg)

            # Set the item's selected background color
            self._listbox.itemconfig(index, selectbackground=self._selected_bg)

            # Set the item's selected foreground color
            self._listbox.itemconfig(index, selectforeground=self._selected_fg)

        # If keep active index
        if keep_active:
            # Use old active index
            indexcur = self._indexcur

        # If not keep active index
        else:
            # Set active index to -1
            indexcur = self._indexcur = -1

        # Clear old selection
        self._listbox.selection_clear(0, END)

        # Set new selection.
        # `-1` works.
        self._listbox.selection_set(indexcur)

        # Set new active index.
        # `-1` works.
        self._listbox.activate(indexcur)

        # If new active index is valid
        if indexcur != -1:
            # Set active background color
            self._listbox.itemconfig(indexcur, background=self._active_bg)

            # Set active foreground color
            self._listbox.itemconfig(indexcur, foreground=self._active_fg)

            # Make the active item visible
            self._listbox.see(indexcur)
Esempio n. 42
0
class ProblemBrowser(object):
    def __init__(self):
        self.action = None
        self.root = root = Tk()
        root.title('Never gonna fold you up')
        root.protocol("WM_DELETE_WINDOW", self.close)

        root.pack_propagate(True)

        scrollbar = Scrollbar(root, orient=tkinter.VERTICAL)
        self.problem_list = Listbox(root, exportselection=False, yscrollcommand=scrollbar.set)
        self.problem_list.pack(expand=True, fill=tkinter.BOTH, side=tkinter.LEFT)
        scrollbar.config(command=self.problem_list.yview)
        scrollbar.pack(side=tkinter.LEFT, fill=tkinter.Y)
        self.problem_list.bind('<<ListboxSelect>>', lambda evt: self.populate_problem_canvas())

        self.problem_canvas = Canvas(root, bd=1, relief=tkinter.SUNKEN, width=500, height=500)
        self.problem_canvas.pack(expand=True, fill=tkinter.BOTH, side=tkinter.LEFT)
        self.problem_canvas.bind("<Configure>", lambda evt: self.populate_problem_canvas())

        button_frame = Frame(root)
        button_frame.pack(fill=tkinter.Y, side=tkinter.LEFT)

        # Reposition the figure so it's center of mass is at 0.5, 0.5
        v = IntVar()
        self.center_cb = Checkbutton(button_frame, text="center", variable=v, command=lambda: self.populate_problem_canvas())
        self.center_cb.var = v
        self.center_cb.pack(side=tkinter.TOP)

        # Use meshes.reconstruct_facets instead of polygon/hole logic.
        v = IntVar()
        self.reconstruct_cb = Checkbutton(button_frame, text="reconstruct", variable=v, command=lambda: self.populate_problem_canvas())
        self.reconstruct_cb.var = v
        self.reconstruct_cb.pack(side=tkinter.TOP)

        self.populate_problems()
        self.current_problem_name = None
        self.current_problem = None


    def populate_problems(self):
        self.problem_list.delete(0, tkinter.END)
        for file in sorted((get_root() / 'problems').iterdir()):
            self.problem_list.insert(tkinter.END, file.stem)


    def populate_problem_canvas(self):
        sel = self.problem_list.curselection()
        if not sel: return
        assert len(sel) == 1
        name = self.problem_list.get(sel[0])
        if name != self.current_problem_name:
            self.current_problem_name = name
            self.current_problem = load_problem(name)
        if self.current_problem:
            p = self.current_problem
            if self.center_cb.var.get():
                p = center_problem(p)
            if self.reconstruct_cb.var.get():
                facets = meshes.reconstruct_facets(p)
                facets = meshes.keep_real_facets(facets, p)
                skeleton = [e for f in facets for e in edges_of_a_facet(f)]
                p = Problem(facets, skeleton)
            draw_problem(self.problem_canvas, p)


    def run(self):
        self.root.mainloop()

    def close(self):
        if self.root:
            self.root.destroy()
            self.root = None
Esempio n. 43
0
class LucteriosMainForm(Tk):

    def __init__(self):
        Tk.__init__(self)
        try:
            img = Image("photo", file=join(
                dirname(import_module('lucterios.install').__file__), "lucterios.png"))
            self.tk.call('wm', 'iconphoto', self._w, img)
        except:
            pass
        self.has_checked = False
        self.title(ugettext("Lucterios installer"))
        self.minsize(475, 260)
        self.grid_columnconfigure(0, weight=1)
        self.grid_rowconfigure(0, weight=1)
        self.running_instance = {}
        self.resizable(True, True)
        self.protocol("WM_DELETE_WINDOW", self.on_closing)

        self.ntbk = ttk.Notebook(self)
        self.ntbk.grid(row=0, column=0, columnspan=1, sticky=(N, S, E, W))

        self.create_instance_panel()
        self.create_module_panel()

        stl = ttk.Style()
        stl.theme_use("default")
        stl.configure("TProgressbar", thickness=5)
        self.progress = ttk.Progressbar(
            self, style="TProgressbar", orient='horizontal', mode='indeterminate')
        self.progress.grid(row=1, column=0, sticky=(E, W))

        self.btnframe = Frame(self, bd=1)
        self.btnframe.grid(row=2, column=0, columnspan=1)
        Button(self.btnframe, text=ugettext("Refresh"), width=20, command=self.refresh).grid(
            row=0, column=0, padx=3, pady=3, sticky=(N, S))
        self.btnupgrade = Button(
            self.btnframe, text=ugettext("Search upgrade"), width=20, command=self.upgrade)
        self.btnupgrade.config(state=DISABLED)
        self.btnupgrade.grid(row=0, column=1, padx=3, pady=3, sticky=(N, S))
        Button(self.btnframe, text=ugettext("Close"), width=20, command=self.on_closing).grid(
            row=0, column=2, padx=3, pady=3, sticky=(N, S))

    def on_closing(self):
        all_stop = True
        instance_names = list(self.running_instance.keys())
        for old_item in instance_names:
            if (self.running_instance[old_item] is not None) and self.running_instance[old_item].is_running():
                all_stop = False
        if all_stop or askokcancel(None, ugettext("An instance is always running.\nDo you want to close?")):
            self.destroy()
        else:
            self.refresh()

    def destroy(self):
        instance_names = list(self.running_instance.keys())
        for old_item in instance_names:
            if self.running_instance[old_item] is not None:
                self.running_instance[old_item].stop()
                del self.running_instance[old_item]
        Tk.destroy(self)

    def create_instance_panel(self):
        frm_inst = Frame(self.ntbk)
        frm_inst.grid_columnconfigure(0, weight=1)
        frm_inst.grid_rowconfigure(0, weight=1)
        frm_inst.grid_columnconfigure(1, weight=3)
        frm_inst.grid_rowconfigure(1, weight=0)
        self.instance_list = Listbox(frm_inst, width=20)
        self.instance_list.bind('<<ListboxSelect>>', self.select_instance)
        self.instance_list.pack()
        self.instance_list.grid(row=0, column=0, sticky=(N, S, W, E))

        self.instance_txt = Text(frm_inst, width=75)
        self.instance_txt.grid(row=0, column=1, rowspan=2, sticky=(N, S, W, E))
        self.instance_txt.config(state=DISABLED)

        self.btninstframe = Frame(frm_inst, bd=1)
        self.btninstframe.grid(row=1, column=0, columnspan=1)
        self.btninstframe.grid_columnconfigure(0, weight=1)
        Button(self.btninstframe, text=ugettext("Launch"), width=25, command=self.open_inst).grid(
            row=0, column=0, columnspan=2, sticky=(N, S))
        Button(self.btninstframe, text=ugettext("Modify"), width=10,
               command=self.modify_inst).grid(row=1, column=0, sticky=(N, S))
        Button(self.btninstframe, text=ugettext("Delete"), width=10,
               command=self.delete_inst).grid(row=1, column=1, sticky=(N, S))
        Button(self.btninstframe, text=ugettext("Save"), width=10,
               command=self.save_inst).grid(row=2, column=0, sticky=(N, S))
        Button(self.btninstframe, text=ugettext("Restore"), width=10,
               command=self.restore_inst).grid(row=2, column=1, sticky=(N, S))
        Button(self.btninstframe, text=ugettext("Add"), width=25, command=self.add_inst).grid(
            row=3, column=0, columnspan=2, sticky=(N, S))

        self.ntbk.add(frm_inst, text=ugettext('Instances'))

    def create_module_panel(self):
        frm_mod = Frame(self.ntbk)
        frm_mod.grid_columnconfigure(0, weight=1)
        frm_mod.grid_rowconfigure(0, weight=1)
        self.module_txt = Text(frm_mod)
        self.module_txt.grid(row=0, column=0, sticky=(N, S, W, E))
        self.module_txt.config(state=DISABLED)
        self.ntbk.add(frm_mod, text=ugettext('Modules'))

    def do_progress(self, progressing):
        if not progressing:
            self.progress.stop()
            self.progress.grid_remove()
        else:
            self.progress.start(25)
            self.progress.grid(row=1, column=0, sticky=(E, W))

    def enabled(self, is_enabled, widget=None):
        if widget is None:
            widget = self
            self.do_progress(not is_enabled)
        if is_enabled:
            widget.config(cursor="")
        else:

            widget.config(cursor="watch")
        if isinstance(widget, Button) and (widget != self.btnupgrade):
            if is_enabled and (not hasattr(widget, 'disabled') or not widget.disabled):
                widget.config(state=NORMAL)
            else:
                widget.config(state=DISABLED)
        else:
            for child_cmp in widget.winfo_children():
                self.enabled(is_enabled, child_cmp)

    @ThreadRun
    def refresh(self, instance_name=None):
        if instance_name is None:
            instance_name = self.get_selected_instance_name()
        self.instance_txt.delete("1.0", END)
        self._refresh_instance_list()
        self.set_select_instance_name(instance_name)
        if not self.has_checked:
            self._refresh_modules()
            if self.instance_list.size() == 0:
                sleep(.3)
                self._refresh_modules()
                sleep(.3)
                self.after_idle(self.add_inst)

    def _refresh_modules(self):
        self.btnupgrade.config(state=DISABLED)
        self.module_txt.config(state=NORMAL)
        self.module_txt.delete("1.0", END)
        lct_glob = LucteriosGlobal()
        mod_lucterios, mod_applis, mod_modules = lct_glob.installed()
        self.module_txt.insert(
            END, ugettext("Lucterios core\t\t%s\n") % mod_lucterios[1])
        self.module_txt.insert(END, '\n')
        self.module_txt.insert(END, ugettext("Application\n"))
        for appli_item in mod_applis:
            self.module_txt.insert(
                END, "\t%s\t%s\n" % (appli_item[0].ljust(30), appli_item[1]))
        self.module_txt.insert(END, ugettext("Modules\n"))
        for module_item in mod_modules:
            self.module_txt.insert(
                END, "\t%s\t%s\n" % (module_item[0].ljust(30), module_item[1]))
        extra_urls = lct_glob.get_extra_urls()
        if len(extra_urls) > 0:
            self.module_txt.insert(END, "\n")
            self.module_txt.insert(END, ugettext("Pypi servers\n"))
            for extra_url in extra_urls:
                self.module_txt.insert(END, "\t%s\n" % extra_url)
        self.module_txt.config(state=DISABLED)
        self.has_checked = True

        self.after(1000, lambda: Thread(target=self.check).start())

    def _refresh_instance_list(self):
        self.instance_list.delete(0, END)
        luct_glo = LucteriosGlobal()
        instance_list = luct_glo.listing()
        for item in instance_list:
            self.instance_list.insert(END, item)
            if item not in self.running_instance.keys():
                self.running_instance[item] = None

        instance_names = list(self.running_instance.keys())
        for old_item in instance_names:
            if old_item not in instance_list:
                if self.running_instance[old_item] is not None:
                    self.running_instance[old_item].stop()
                del self.running_instance[old_item]

    def set_select_instance_name(self, instance_name):
        cur_sel = 0
        for sel_iter in range(self.instance_list.size()):
            if self.instance_list.get(sel_iter) == instance_name:
                cur_sel = sel_iter
                break
        self.instance_list.selection_set(cur_sel)
        self.select_instance(None)

    def get_selected_instance_name(self):
        if len(self.instance_list.curselection()) > 0:
            return self.instance_list.get(int(self.instance_list.curselection()[0]))
        else:
            return ""

    def set_ugrade_state(self, must_upgrade):
        if must_upgrade:
            self.btnupgrade.config(state=NORMAL)
            self.btnupgrade["text"] = ugettext("Upgrade needs")
        else:
            self.btnupgrade["text"] = ugettext("No upgrade")
            self.btnupgrade.config(state=DISABLED)

    def check(self):
        must_upgrade = False
        try:
            lct_glob = LucteriosGlobal()
            _, must_upgrade = lct_glob.check()
        finally:
            self.after(300, self.set_ugrade_state, must_upgrade)

    @ThreadRun
    def upgrade(self):
        self.btnupgrade.config(state=DISABLED)
        self.instance_list.config(state=DISABLED)
        try:
            from logging import getLogger
            admin_path = import_module(
                "lucterios.install.lucterios_admin").__file__
            proc = Popen(
                [sys.executable, admin_path, "update"], stderr=STDOUT, stdout=PIPE)
            value = proc.communicate()[0]
            try:
                value = value.decode('ascii')
            except:
                pass
            six.print_(value)
            if proc.returncode != 0:
                getLogger("lucterios.admin").error(value)
            else:
                getLogger("lucterios.admin").info(value)
            showinfo(ugettext("Lucterios installer"), ugettext(
                "The application must restart"))
            python = sys.executable
            os.execl(python, python, *sys.argv)
        finally:
            self._refresh_modules()
            self.btnupgrade.config(state=NORMAL)
            self.instance_list.config(state=NORMAL)

    @ThreadRun
    def select_instance(self, evt):

        if self.instance_list['state'] == NORMAL:
            self.instance_list.config(state=DISABLED)
            try:
                instance_name = self.get_selected_instance_name()
                self.instance_txt.configure(state=NORMAL)
                self.instance_txt.delete("1.0", END)
                if instance_name != '':
                    if instance_name not in self.running_instance.keys():
                        self.running_instance[instance_name] = None
                    inst = LucteriosInstance(instance_name)
                    inst.read()
                    self.instance_txt.insert(END, "\t\t\t%s\n\n" % inst.name)
                    self.instance_txt.insert(
                        END, ugettext("Database\t\t%s\n") % inst.get_database_txt())
                    self.instance_txt.insert(
                        END, ugettext("Appli\t\t%s\n") % inst.get_appli_txt())
                    self.instance_txt.insert(
                        END, ugettext("Modules\t\t%s\n") % inst.get_module_txt())
                    self.instance_txt.insert(
                        END, ugettext("Extra\t\t%s\n") % inst.get_extra_txt())
                    self.instance_txt.insert(END, '\n')
                    if self.running_instance[instance_name] is not None and self.running_instance[instance_name].is_running():
                        self.instance_txt.insert(END, ugettext(
                            "=> Running in http://%(ip)s:%(port)d\n") % {'ip': self.running_instance[instance_name].lan_ip, 'port': self.running_instance[instance_name].port})
                        self.btninstframe.winfo_children()[0]["text"] = ugettext(
                            "Stop")
                    else:
                        self.running_instance[instance_name] = None
                        self.instance_txt.insert(END, ugettext("=> Stopped\n"))
                        self.btninstframe.winfo_children()[0]["text"] = ugettext(
                            "Launch")
                else:
                    self.btninstframe.winfo_children()[0]["text"] = ugettext(
                        "Launch")
                self.btninstframe.winfo_children()[0].disabled = (
                    instance_name == '')
                self.btninstframe.winfo_children()[1].disabled = (
                    instance_name == '')
                self.btninstframe.winfo_children()[2].disabled = (
                    instance_name == '')
                self.btninstframe.winfo_children()[3].disabled = (
                    instance_name == '')
                self.btninstframe.winfo_children()[4].disabled = (
                    instance_name == '')
                self.instance_txt.configure(state=DISABLED)
            finally:
                setup_from_none()
                self.instance_list.config(state=NORMAL)

    @ThreadRun
    def add_modif_inst_result(self, result, to_create):
        inst = LucteriosInstance(result[0])
        inst.set_extra("LANGUAGE_CODE='%s'" % result[5])
        inst.set_appli(result[1])
        inst.set_module(result[2])
        inst.set_database(result[4])
        if to_create:
            inst.add()
        else:
            inst.modif()
        inst = LucteriosInstance(result[0])
        inst.set_extra(result[3])
        inst.security()
        self.refresh(result[0])

    def add_inst(self):
        self.enabled(False)
        try:
            self.do_progress(False)
            ist_edt = InstanceEditor()
            ist_edt.execute()
            ist_edt.transient(self)
            self.wait_window(ist_edt)
        finally:
            self.enabled(True)
        if ist_edt.result is not None:
            self.add_modif_inst_result(ist_edt.result, True)

    def modify_inst(self):
        self.enabled(False)
        try:
            self.do_progress(False)
            ist_edt = InstanceEditor()
            ist_edt.execute(self.get_selected_instance_name())
            ist_edt.transient(self)
            self.wait_window(ist_edt)
        finally:
            self.enabled(True)
        if ist_edt.result is not None:
            self.add_modif_inst_result(ist_edt.result, False)

    @ThreadRun
    def delete_inst_name(self, instance_name):
        inst = LucteriosInstance(instance_name)
        inst.delete()
        self.refresh()

    def delete_inst(self):
        setup_from_none()
        instance_name = self.get_selected_instance_name()
        if askokcancel(None, ugettext("Do you want to delete '%s'?") % instance_name):
            self.delete_inst_name(instance_name)
        else:
            self.refresh()

    @ThreadRun
    def open_inst(self):
        instance_name = self.get_selected_instance_name()
        if instance_name != '':
            try:
                if instance_name not in self.running_instance.keys():
                    self.running_instance[instance_name] = None
                if self.running_instance[instance_name] is None:
                    port = FIRST_HTTP_PORT
                    for inst_obj in self.running_instance.values():
                        if (inst_obj is not None) and (inst_obj.port >= port):
                            port = inst_obj.port + 1
                    self.running_instance[instance_name] = RunServer(
                        instance_name, port)
                    self.running_instance[instance_name].start()
                else:
                    self.running_instance[instance_name].stop()
                    self.running_instance[instance_name] = None
            finally:
                self.set_select_instance_name(instance_name)

    @ThreadRun
    def save_instance(self, instance_name, file_name):
        inst = LucteriosInstance(instance_name)
        inst.filename = file_name
        if inst.archive():
            showinfo(ugettext("Lucterios installer"), ugettext(
                "Instance saved to %s") % file_name)
        else:
            showerror(
                ugettext("Lucterios installer"), ugettext("Instance not saved!"))
        self.refresh(instance_name)

    def save_inst(self):
        instance_name = self.get_selected_instance_name()
        if instance_name != '':
            file_name = asksaveasfilename(
                parent=self, filetypes=[('lbk', '.lbk'), ('*', '.*')])
            if file_name != '':
                self.save_instance(instance_name, file_name)

    @ThreadRun
    def restore_instance(self, instance_name, file_name):
        if file_name[-4:] == '.bkf':
            rest_inst = MigrateFromV1(instance_name, withlog=True)
        else:
            rest_inst = LucteriosInstance(instance_name)
        rest_inst.filename = file_name
        if rest_inst.restore():
            showinfo(ugettext("Lucterios installer"), ugettext(
                "Instance restore from %s") % file_name)
        else:
            showerror(
                ugettext("Lucterios installer"), ugettext("Instance not restored!"))
        self.refresh(instance_name)

    def restore_inst(self):
        instance_name = self.get_selected_instance_name()
        if instance_name != '':
            file_name = askopenfilename(
                parent=self, filetypes=[('lbk', '.lbk'), ('bkf', '.bkf'), ('*', '.*')])
            if file_name != '':
                self.restore_instance(instance_name, file_name)

    def execute(self):
        self.refresh()
        center(self, (700, 300))
        self.mainloop()
Esempio n. 44
0
class InstanceEditor(Toplevel):

    def __init__(self):
        Toplevel.__init__(self)
        self.focus_set()
        self.grab_set()

        self.result = None
        self.module_data = None
        self.mod_applis = None
        self.title(ugettext("Instance editor"))
        self.grid_columnconfigure(0, weight=1)
        self.grid_rowconfigure(0, weight=1)

        self.ntbk = ttk.Notebook(self)
        self.ntbk.grid(row=0, column=0, columnspan=1, sticky=(N, S, E, W))

        self.frm_general = Frame(self.ntbk, width=350, height=150)
        self.frm_general.grid_columnconfigure(0, weight=0)
        self.frm_general.grid_columnconfigure(1, weight=1)
        self._general_tabs()
        self.ntbk.add(self.frm_general, text=ugettext('General'))

        self.frm_database = Frame(self.ntbk, width=350, height=150)
        self.frm_database.grid_columnconfigure(0, weight=0)
        self.frm_database.grid_columnconfigure(1, weight=1)
        self._database_tabs()
        self.ntbk.add(self.frm_database, text=ugettext('Database'))

        btnframe = Frame(self, bd=1)
        btnframe.grid(row=1, column=0, columnspan=1)
        Button(btnframe, text=ugettext("OK"), width=10, command=self.apply).grid(
            row=0, column=0, sticky=(N, S, E))
        Button(btnframe, text=ugettext("Cancel"), width=10, command=self.destroy).grid(
            row=0, column=1, sticky=(N, S, W))

    def _database_tabs(self):
        Label(self.frm_database, text=ugettext("Type")).grid(
            row=0, column=0, sticky=(N, W), padx=5, pady=3)
        self.typedb = ttk.Combobox(
            self.frm_database, textvariable=StringVar(), state=READLONY)
        self.typedb.bind("<<ComboboxSelected>>", self.typedb_selection)
        self.typedb.grid(row=0, column=1, sticky=(N, S, E, W), padx=5, pady=3)
        Label(self.frm_database, text=ugettext("Name")).grid(
            row=1, column=0, sticky=(N, W), padx=5, pady=3)
        self.namedb = Entry(self.frm_database)
        self.namedb.grid(row=1, column=1, sticky=(N, S, E, W), padx=5, pady=3)
        Label(self.frm_database, text=ugettext("User")).grid(
            row=2, column=0, sticky=(N, W), padx=5, pady=3)
        self.userdb = Entry(self.frm_database)
        self.userdb.grid(row=2, column=1, sticky=(N, S, E, W), padx=5, pady=3)
        Label(self.frm_database, text=ugettext("Password")).grid(
            row=3, column=0, sticky=(N, W), padx=5, pady=3)
        self.pwddb = Entry(self.frm_database)
        self.pwddb.grid(row=3, column=1, sticky=(N, S, E, W), padx=5, pady=3)

    def _general_tabs(self):
        Label(self.frm_general, text=ugettext("Name")).grid(
            row=0, column=0, sticky=(N, W), padx=5, pady=3)
        self.name = Entry(self.frm_general)
        self.name.grid(row=0, column=1, sticky=(N, S, E, W), padx=5, pady=3)
        Label(self.frm_general, text=ugettext("Appli")).grid(
            row=1, column=0, sticky=(N, W), padx=5, pady=3)
        self.applis = ttk.Combobox(
            self.frm_general, textvariable=StringVar(), state=READLONY)
        self.applis.bind("<<ComboboxSelected>>", self.appli_selection)
        self.applis.grid(row=1, column=1, sticky=(N, S, E, W), padx=5, pady=3)
        Label(self.frm_general, text=ugettext("Modules")).grid(
            row=2, column=0, sticky=(N, W), padx=5, pady=3)
        self.modules = Listbox(self.frm_general, selectmode=EXTENDED)
        self.modules.configure(exportselection=False)
        self.modules.grid(row=2, column=1, sticky=(N, S, E, W), padx=5, pady=3)
        Label(self.frm_general, text=ugettext("Language")).grid(
            row=3, column=0, sticky=(N, W), padx=5, pady=3)
        self.language = ttk.Combobox(
            self.frm_general, textvariable=StringVar(), state=READLONY)
        self.language.grid(
            row=3, column=1, sticky=(N, S, E, W), padx=5, pady=3)
        Label(self.frm_general, text=ugettext("CORE-connectmode")
              ).grid(row=4, column=0, sticky=(N, W), padx=5, pady=3)
        self.mode = ttk.Combobox(
            self.frm_general, textvariable=StringVar(), state=READLONY)
        self.mode.bind("<<ComboboxSelected>>", self.mode_selection)
        self.mode.grid(row=4, column=1, sticky=(N, S, E, W), padx=5, pady=3)
        Label(self.frm_general, text=ugettext("Password")).grid(
            row=5, column=0, sticky=(N, W), padx=5, pady=3)
        self.password = Entry(self.frm_general, show="*")
        self.password.grid(
            row=5, column=1, sticky=(N, S, E, W), padx=5, pady=3)

    def typedb_selection(self, event):

        visible = list(self.typedb[VALUES]).index(self.typedb.get()) != 0
        for child_cmp in self.frm_database.winfo_children()[2:]:
            if visible:
                child_cmp.config(state=NORMAL)
            else:
                child_cmp.config(state=DISABLED)

    def appli_selection(self, event):
        if self.applis.get() != '':
            appli_id = list(self.applis[VALUES]).index(self.applis.get())
            luct_glo = LucteriosGlobal()
            current_inst_names = luct_glo.listing()
            appli_root_name = self.mod_applis[appli_id][0].split('.')[-1]
            default_name_idx = 1
            while appli_root_name + six.text_type(default_name_idx) in current_inst_names:
                default_name_idx += 1
            self.name.delete(0, END)
            self.name.insert(
                0, appli_root_name + six.text_type(default_name_idx))
            mod_depended = self.mod_applis[appli_id][2]
            self.modules.select_clear(0, self.modules.size())
            for mod_idx in range(len(self.module_data)):
                current_mod = self.module_data[mod_idx]
                if current_mod in mod_depended:
                    self.modules.selection_set(mod_idx)

    def mode_selection(self, event):
        visible = list(self.mode[VALUES]).index(self.mode.get()) != 2
        for child_cmp in self.frm_general.winfo_children()[-2:]:
            if visible:
                child_cmp.config(state=NORMAL)
            else:
                child_cmp.config(state=DISABLED)

    def apply(self):
        from lucterios.framework.settings import DEFAULT_LANGUAGES, get_locale_lang
        if self.name.get() == '':
            showerror(ugettext("Instance editor"), ugettext("Name empty!"))
            return
        if self.applis.get() == '':
            showerror(ugettext("Instance editor"), ugettext("No application!"))
            return
        db_param = "%s:name=%s,user=%s,password=%s" % (
            self.typedb.get(), self.namedb.get(), self.userdb.get(), self.pwddb.get())
        security = "MODE=%s" % list(
            self.mode[VALUES]).index(self.mode.get())
        if self.password.get() != '':
            security += ",PASSWORD=%s" % self.password.get()
        module_list = [
            self.module_data[int(item)] for item in self.modules.curselection()]
        appli_id = list(self.applis[VALUES]).index(self.applis.get())
        current_lang = get_locale_lang()
        for lang in DEFAULT_LANGUAGES:
            if lang[1] == self.language.get():
                current_lang = lang[0]
        self.result = (self.name.get(), self.mod_applis[appli_id][
                       0], ",".join(module_list), security, db_param, current_lang)
        self.destroy()

    def _load_current_data(self, instance_name):
        from lucterios.framework.settings import DEFAULT_LANGUAGES, get_locale_lang
        lct_inst = LucteriosInstance(instance_name)
        lct_inst.read()
        self.name.delete(0, END)
        self.name.insert(0, lct_inst.name)
        self.name.config(state=DISABLED)
        applis_id = 0
        for appli_iter in range(len(self.mod_applis)):
            if self.mod_applis[appli_iter][0] == lct_inst.appli_name:
                applis_id = appli_iter
                break
        self.applis.current(applis_id)
        if lct_inst.extra['']['mode'] is not None:
            self.mode.current(lct_inst.extra['']['mode'][0])
        else:
            self.mode.current(2)
        self.mode_selection(None)
        typedb_index = 0
        for typedb_idx in range(len(self.typedb[VALUES])):
            if self.typedb[VALUES][typedb_idx].lower() == lct_inst.database[0].lower():
                typedb_index = typedb_idx
                break
        self.typedb.current(typedb_index)
        self.typedb.config(state=DISABLED)
        self.typedb_selection(None)
        self.namedb.delete(0, END)
        if 'name' in lct_inst.database[1].keys():
            self.namedb.insert(0, lct_inst.database[1]['name'])
        self.userdb.delete(0, END)
        if 'user' in lct_inst.database[1].keys():
            self.userdb.insert(0, lct_inst.database[1]['user'])
        self.pwddb.delete(0, END)
        if 'password' in lct_inst.database[1].keys():
            self.pwddb.insert(0, lct_inst.database[1]['password'])
        self.modules.select_clear(0, self.modules.size())
        for mod_idx in range(len(self.module_data)):
            current_mod = self.module_data[mod_idx]
            if current_mod in lct_inst.modules:
                self.modules.select_set(mod_idx)
        current_lang = get_locale_lang()
        if 'LANGUAGE_CODE' in lct_inst.extra.keys():
            current_lang = lct_inst.extra['LANGUAGE_CODE']
        for lang in DEFAULT_LANGUAGES:
            if lang[0] == current_lang:
                self.language.current(self.language[VALUES].index(lang[1]))

    def execute(self, instance_name=None):
        from lucterios.framework.settings import DEFAULT_LANGUAGES, get_locale_lang
        self.mode[VALUES] = [ugettext(
            "CORE-connectmode.0"), ugettext("CORE-connectmode.1"), ugettext("CORE-connectmode.2")]
        self.language[VALUES] = [lang[1] for lang in DEFAULT_LANGUAGES]
        self.typedb[VALUES] = ["SQLite", "MySQL", "PostgreSQL"]
        lct_glob = LucteriosGlobal()
        _, self.mod_applis, mod_modules = lct_glob.installed()
        self.mod_applis.sort(key=lambda item: get_module_title(item[0]))
        self.modules.delete(0, END)
        self.module_data = []
        module_list = []
        for mod_module_item in mod_modules:
            module_list.append(
                (get_module_title(mod_module_item[0]), mod_module_item[0]))
        module_list.sort(key=lambda module: module[0])
        for module_title, module_name in module_list:
            self.modules.insert(END, module_title)
            self.module_data.append(module_name)
        appli_list = []
        for mod_appli_item in self.mod_applis:
            appli_list.append(get_module_title(mod_appli_item[0]))
        self.applis[VALUES] = appli_list
        if instance_name is not None:
            self._load_current_data(instance_name)
        else:
            self.typedb.current(0)
            self.mode.current(2)
            if len(appli_list) > 0:
                self.applis.current(0)
            self.appli_selection(None)
            self.mode_selection(None)
            self.typedb_selection(None)
            for lang in DEFAULT_LANGUAGES:
                if lang[0] == get_locale_lang():
                    self.language.current(self.language[VALUES].index(lang[1]))
        center(self)
Esempio n. 45
0
class GUIChecker(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master = master
        self.grid(columnspan=2000)

        self.__set_default_value()
        self.__create_widgets()
        self.__locate_widges()

        self.setting_on = False

    def __set_default_value(self):
        self.ALL_FIELDS = ["系號", "序號", "餘額", "課程名稱(連結課程地圖)",
                           "學分", "時間", "教師姓名*:主負責老師"
                           "已選課人數", "教室", "選必修", "限選條件",
                           "系所名稱", "年級", "組別", "類別", "班別",
                           "業界專家參與", "英語授課", "Moocs", "跨領域學分學程",
                           "備註", "課程碼", "分班碼", "屬性碼"]
        self.default_choosen_field = ["系號", "序號", "餘額", "課程名稱(連結課程地圖)",
                                      "學分", "時間", "教師姓名*:主負責老師"]
        self.choosen_field = deepcopy(self.default_choosen_field)

    def __create_widgets(self):
        self.input_label = Label(self, text="系所代碼 : ")
        self.input_field = Entry(self, width=30)
        self.search_btn = Button(self, text="搜尋", command=self.__search_method)
        self.clear_btn = Button(self, text="清除", command=self.__clear_method)
        self.setting_btn = Button(self, text="設定", command=self.__setting_method)
        self.msg_text = Label(self)

    def __locate_widges(self):
        self.input_label.grid(row=0, column=0)
        self.input_field.grid(row=0, column=1, columnspan=6)
        self.search_btn.grid(row=1, column=0)
        self.clear_btn.grid(row=1, column=2)
        self.setting_btn.grid(row=1, column=4)
        self.msg_text.grid(row=2, column=0)

    def __search_method(self):
        department_no = self.input_field.get()
        self.msg_text['text'] = '查詢中'
        try:
            self.__attach_course_table(department_no)
        except NoCourseAvailableError as e:
            logging.debug(e)
            self.msg_text['text'] = '沒有這個系所'
        except Exception as e:
            logging.exception('Seach Method: ')
            self.msg_text['text'] = '未知的錯誤'
        else:
            self.msg_text['text'] = ''

    def __attach_course_table(self, dept_no):
        courses = self.__search_courses(dept_no)
        title = list(courses.columns.values)

        self.__clear_method()
        self.__set_up_tree_widget(title, len(courses))

        for field in title:
            self.tree.heading(field, text=field)
            self.tree.column(field, width=font.Font().measure(field))

        for index, course in courses.iterrows():
            self.tree.insert('', 'end', values=tuple(course.values))
            for ix, val in enumerate(course.values):
                col_w = font.Font().measure(val) + 10
                if self.tree.column(title[ix], width=None) < col_w:
                    self.tree.column(title[ix], width=col_w)

    def __search_courses(self, dept_no):
        crawler = NckuCourseCrawler(dept_no=dept_no)
        html = crawler.get_raw_HTML()
        parser = NckuCourseParser(html)
        parser.include_fields = self.choosen_field
        logging.info("Choosen Field: {}".format(self.choosen_field))
        courses = parser.parse(sort=True)
        print(courses)
        courses['餘額'] = courses['餘額'].apply(int)
        return courses

    def __clear_method(self):
        try:
            self.__remove_tree_widget()
        except Exception as e:
            logging.debug(
                'Widget not yet created. Not and Error. {}'.format(e)
            )

    def __remove_tree_widget(self):
        self.tree_vsb.grid_remove()
        self.tree_hsb.grid_remove()
        self.tree.grid_remove()

    def __set_up_tree_widget(self, title, courses_num):
        tree_height = min(30, courses_num)
        self.tree = ttk.Treeview(columns=title, show="headings",
                                 height=tree_height)
        self.tree_vsb = ttk.Scrollbar(orient="vertical",
                                      command=self.tree.yview)
        self.tree_hsb = ttk.Scrollbar(orient="horizontal",
                                      command=self.tree.xview)
        self.tree.configure(yscrollcommand=self.tree_vsb.set,
                            xscrollcommand=self.tree_hsb.set)

        self.tree.grid(row=3, column=0)
        self.tree_vsb.grid(row=3, column=1, sticky="ns")
        self.tree_hsb.grid(row=4, column=0, sticky="we")

    def __setting_method(self):
        if not self.setting_on:
            self.setting_on = True

            self.setting_win = Toplevel(self)
            self.setting_win.wm_title("Settings")
            self.__create_setting_win_widget()

    def __create_setting_win_widget(self):
        choose_field_label = Label(self.setting_win, text="選擇欄位")
        self.__create_choose_field_listbox()
        confirm_btn = Button(self.setting_win, text="確定", command=self.__confirm_setting)
        default_btn = Button(self.setting_win, text="回復預設值", command=self.__restore_setting)

        confirm_btn.grid(row=0, column=0)
        default_btn.grid(row=0, column=1)
        choose_field_label.grid(row=1, column=0)

    def __create_choose_field_listbox(self):
        self.choose_field_listbox = Listbox(self.setting_win,
                                            selectmode=MULTIPLE,
                                            height=len(self.ALL_FIELDS))
        for i in self.ALL_FIELDS:
            self.choose_field_listbox.insert(END, i)
        for index, choosen in enumerate(self.choosen_field):
            if choosen:
                self.choose_field_listbox.select_set(index)

        self.choose_field_listbox.grid(row=1, column=1, sticky="nsew")

    def __confirm_setting(self):
        print(self.choose_field_listbox)
        print(self.choose_field_listbox.curselection())
        selections = self.choose_field_listbox.curselection()
        self.choosen_field = [
            self.ALL_FIELDS[col_index] for col_index in selections
        ]
        self.setting_win.withdraw()
        self.setting_on = False

    def __restore_setting(self):
        self.choose_field_listbox.grid_remove()
        self.choosen_field = self.default_choosen_field
        self.__create_choose_field_listbox()
Esempio n. 46
0
class LogUI(Frame):
  
    def __init__(self, parent):
        Frame.__init__(self, parent)   
        self.parent = parent
        
        self.filemodels = []
        filemodel1 = FileModel("C:/Users/chen_xi/test1.csv", searchconds=[], relation="and", joincondtuples=[])
        filemodel2 = FileModel("C:/Users/chen_xi/test2.csv", searchconds=[], relation="and", joincondtuples=[])
        self.filemodels = [filemodel1, filemodel2]
        
        self._initUI()
        self.selectedfileindex = -1
        
        
    def _initUI(self):
        self.parent.title("Log Processor")
        self.pack()
        
        self._initfilepanel()
        self._initsearchcondpanel()
        self._initjoincondpanel()
        self._initconsole()
        
        self._inflatefilelist()
        
        
    def _initfilepanel(self):
        frame = Frame(self)
        frame.grid(row=0, column=0, sticky=E + W + S + N)
        
        label = Label(frame, text="File List: ")
        label.grid(sticky=N + W)
        
        self.filelist = Listbox(frame, width=40)
        self.filelist.grid(row=1, column=0, rowspan=2, columnspan=3)
        
        vsl = Scrollbar(frame, orient=VERTICAL)
        vsl.grid(row=1, column=3, rowspan=2, sticky=N + S + W)
        
        hsl = Scrollbar(frame, orient=HORIZONTAL)
        hsl.grid(row=3, column=0, columnspan=3, sticky=W + E + N)
        
        self.filelist.config(yscrollcommand=vsl.set, xscrollcommand=hsl.set)
        self.filelist.bind('<<ListboxSelect>>', self._onfilelistselection)
        
        hsl.config(command=self.filelist.xview)
        vsl.config(command=self.filelist.yview)
        
        upbtn = Button(frame, text="Up", width=7, command=self._upfile)
        upbtn.grid(row=1, column=4, padx=5, pady=5)
        
        downbtn = Button(frame, text="Down", width=7, command=self._downfile)
        downbtn.grid(row=2, column=4, padx=5, pady=5)
        
        newbtn = Button(frame, text="New", width=7, command=self._addfile)
        newbtn.grid(row=4, column=1, pady=5, sticky=E + S)
        
        delbtn = Button(frame, text="Delete", width=7, command=self._deletefile)
        delbtn.grid(row=4, column=2, padx=5, pady=5, sticky=W + S)
        
            
    def _inflatefilelist(self):
        self.filelist.delete(0, END)
        for filemodel in self.filemodels:
            self.filelist.insert(END, filemodel.filename)
            
        
    def _initsearchcondpanel(self):
        frame = Frame(self)
        frame.grid(row=0, column=1, sticky=E + W + S + N, padx=5)
        
        label = Label(frame, text="Search Condition: ")
        label.grid(row=0, column=0, columnspan=1, sticky=W)
        
        relationlable = Label(frame, text="Relation")
        relationlable.grid(row=0, column=1, columnspan=1, sticky=E)
        
        self.condrelationvar = StringVar(frame)
        relationinput = Combobox(frame, textvariable=self.condrelationvar, values=["and", "or"])
        relationinput.grid(row=0, column=2, padx=5, sticky=E)
        relationinput.bind('<<ComboboxSelected>>', self._onrelationchange)
        
        self.searchcondlist = Listbox(frame)
        self.searchcondlist.grid(row=1, rowspan=1, columnspan=3, sticky=E + W + S + N)
        
        vsl = Scrollbar(frame, orient=VERTICAL)
        vsl.grid(row=1, column=3, rowspan=1, sticky=N + S + W)
        
        hsl = Scrollbar(frame, orient=HORIZONTAL)
        hsl.grid(row=2, column=0, columnspan=3, sticky=W + E + N)
        
        self.searchcondlist.config(yscrollcommand=vsl.set, xscrollcommand=hsl.set)
        
        hsl.config(command=self.searchcondlist.xview)
        vsl.config(command=self.searchcondlist.yview)
        
        newbtn = Button(frame, text="New", width=7, command=self._addsearchcondition)
        newbtn.grid(row=3, column=0, padx=5, pady=5, sticky=E)
        
        delbtn = Button(frame, text="Delete", width=7, command=self._deletesearchcondition)
        delbtn.grid(row=3, column=1, sticky=E)
        
        modbtn = Button(frame, text="Update", width=7, command=self._modifysearchcondition)
        modbtn.grid(row=3, column=2, padx=5, pady=5, sticky=W)
        
    
    def _onrelationchange(self, evt):
        selectedmodel = self._getselectedfile()
        selectedmodel.relation = self.condrelationvar.get()
    
            
    def _inflatesearchcondlist(self, filemodel):
        self.condrelationvar.set(filemodel.relation)
        conds = filemodel.searchconds
        self.searchcondlist.delete(0, END)
        for cond in conds:
            self.searchcondlist.insert(END, cond.tostring())
        
        
    def _initjoincondpanel(self):
        frame = Frame(self)
        frame.grid(row=0, column=2, sticky=E + W + S + N, padx=5)
        
        label = Label(frame, text="Join Condition: ")
        label.grid(sticky=N + W)
        
        self.joincondlist = Listbox(frame)
        self.joincondlist.grid(row=1, rowspan=1, columnspan=3, sticky=E + W + S + N)
        
        vsl = Scrollbar(frame, orient=VERTICAL)
        vsl.grid(row=1, column=3, rowspan=1, sticky=N + S + W)
        
        hsl = Scrollbar(frame, orient=HORIZONTAL)
        hsl.grid(row=2, column=0, columnspan=3, sticky=W + E + N)
        
        self.joincondlist.config(yscrollcommand=vsl.set, xscrollcommand=hsl.set)
        
        hsl.config(command=self.joincondlist.xview)
        vsl.config(command=self.joincondlist.yview)
        
        newbtn = Button(frame, text="New", width=7, command=self._addjoincondition)
        newbtn.grid(row=3, column=0, padx=5, pady=5, sticky=E)
        
        delbtn = Button(frame, text="Delete", width=7, command=self._deletejoincondition)
        delbtn.grid(row=3, column=1, sticky=E)
        
        modbtn = Button(frame, text="Update", width=7, command=self._modifyjoincondition)
        modbtn.grid(row=3, column=2, padx=5, pady=5, sticky=W)
        
    
    def _inflatejoincondlist(self, condtuples):
        self.joincondlist.delete(0, END)
        for condtuple in condtuples:
            cond = condtuple[0]
            tofilename = condtuple[1]
            self.joincondlist.insert(END, cond.tostring() + " in " + tofilename)
        
    
    def _initconsole(self):
        
        separator = Separator(self, orient=HORIZONTAL)
        separator.grid(row=1, columnspan=3, sticky=W + E, padx=5, pady=5)
        
        self.console = Text(self)
        self.console.grid(row=2, columnspan=3, sticky=W + E, padx=5, pady=5)
        
        vsl = Scrollbar(self, orient=VERTICAL)
        vsl.grid(row=2, column=3, sticky=N + S + W)
        
        hsl = Scrollbar(self, orient=HORIZONTAL)
        hsl.grid(row=3, column=0, columnspan=3, sticky=W + E + N)
        
        hsl.config(command=self.console.xview)
        vsl.config(command=self.console.yview)
        
        resbtn = Button(self, text="Search", width=7, comman=self._showsearchresult)
        resbtn.grid(row=4, column=2, padx=5, pady=5, sticky=E)
        
    
    def _showsearchresult(self):
        try:
            res = self._searchresult()
            formatres = self._formatsearchresult(res)
        except Exception:
            formatres = "Error!\r\n" + traceback.format_exc()
        
        self.console.delete("0.0", END)
        self.console.insert("0.0", formatres)
    
    
    def _searchresult(self):
        filesearchs = []
        joinsearchs = []
        for filemodel in self.filemodels:
            filename = filemodel.filename
            
            singlesearch = SingleFileSearch(filename, DictReader(open(filename)), filemodel.searchconds, filemodel.relation)
            filesearchs.append(singlesearch)
            
            joindict = {}
            for joincondtuple in filemodel.joincondtuples:
                tofilename = joincondtuple[1]
                joincond = joincondtuple[0]
                if tofilename not in joindict:
                    joindict[tofilename] = []
                joindict[tofilename].append(joincond)
            
            for tofilename in joindict:
                joinsearch = JoinFileSearch(filename, DictReader(open(filename)), tofilename, DictReader(open(tofilename)), joindict[tofilename])
                joinsearchs.append(joinsearch)
                
        search = Search(filesearchs, joinsearchs)
        return search.process()
    
    
    def _formatsearchresult(self, searchresult):
        formatres = self._formatsummary(searchresult) + "\r\n"
        fileresults = searchresult.results
        for filemodel in self.filemodels:
            filename = filemodel.filename
            fileresult = fileresults[filename]
            formatres += self._formatfileresult(fileresult)
            formatres += "\r\n"
        return formatres
    
    
    def _formatsummary(self, searchresult):
        res = "Summary\r\n"
        res += "Time Cost: " + str(searchresult.timecost) + " Seconds\r\n"
        fileresults = searchresult.results
        for filemodel in self.filemodels:
            filename = filemodel.filename
            fileresult = fileresults[filename]
            res += filename + "    Size: " + str(len(fileresult.result)) + "\r\n"
        return res
        
    
    def _formatfileresult(self, fileresult):
        res = ""
        filename = fileresult.filename
        res += filename + "    Size: " + str(len(fileresult.result)) + "\r\n"
        fields = csvhandler.getfields(filename)
        
        for (i, field) in enumerate(fields):
            res += field
            if i < (len(fields) - 1):
                res += ","
            else:
                res += "\r\n"
                
        for rowdict in fileresult.result:
            for (i, field) in enumerate(fields):
                res += rowdict[field]
                if i < (len(fields) - 1):
                    res += ","
                else:
                    res += "\r\n"
        return res
                
             
        
    def _addfile(self):
        filetypes = [('csv files', '*.csv'), ('All files', '*')]
        
        selectedfile = askopenfilename(filetypes=filetypes)
        if selectedfile is not None:
            newmodel = FileModel(selectedfile, searchconds=[], relation="and", joincondtuples=[])
            self.filemodels.append(newmodel)
            self.filelist.insert(END, newmodel.filename)
            self._setselectedfileindex(len(self.filemodels) - 1)
        
        
    def _deletefile(self):
        index = self._getselectedfileindex()
        if index >= 0:
            self.filelist.delete(index)
            del self.filemodels[index]
            self._setselectedfileindex(-1)
        
        
    def _upfile(self):
        if self._getselectedfileindex() <= 0:
            return
        index = self._getselectedfileindex()
        selectedfilename = self._getselectedfile().filename
        
        if index > 0:
            self.filelist.insert((index - 1), selectedfilename)
            self.filelist.delete(index + 1)
            
            self.filemodels[index - 1], self.filemodels[index] = self.filemodels[index], self.filemodels[index - 1]
            self._setselectedfileindex(index - 1)
            
            
    def _downfile(self):
        if self._getselectedfileindex() < 0:
            return
        index = self._getselectedfileindex()
        selectedfilename = self._getselectedfile().filename
        
        if index < (len(self.filemodels) - 1):
            self.filelist.insert((index + 2), selectedfilename)
            self.filelist.delete(index)
            
            self.filemodels[index], self.filemodels[index + 1] = self.filemodels[index + 1], self.filemodels[index]
            self._setselectedfileindex(index + 1)
         
         
    def _onfilelistselection(self, evt):
        if len(self.filelist.curselection()) == 0:
            return
        
        self._setselectedfileindex(self.filelist.curselection()[0])
        selectedfile = self._getselectedfile()
        
        self._inflatesearchcondlist(selectedfile)
        
        joincondtuples = selectedfile.joincondtuples
        self._inflatejoincondlist(joincondtuples)
        
    def _addsearchcondition(self):
        if self._getselectedfileindex() < 0:
            return
        
        self._popupsearchcondwindow()
        
    
    def _deletesearchcondition(self):
        index = self._getselectedsearchcondindex()
        if index < 0:
            return
        
        selectedfile = self._getselectedfile()
        del selectedfile.searchconds[index]
        self.searchcondlist.delete(index)
    
    
    def _modifysearchcondition(self):
        if self._getselectedsearchcondindex() < 0:
            return
        
        self._popupsearchcondwindow(self._getselectedsearchcondindex())
        
    
    def _addjoincondition(self):
        if self._getselectedfileindex() < 0:
            return
        
        self._popupjoincondwindow()
        
    
    def _deletejoincondition(self):
        index = self._getselectedjoincondindex()
        if index < 0:
            return
        
        selectedfile = self._getselectedfile()
        del selectedfile.joincondtuples[index]
        self.joincondlist.delete(index)
    
    
    def _modifyjoincondition(self):
        if self._getselectedjoincondindex() < 0:
            return
        
        self._popupjoincondwindow(self._getselectedjoincondindex())
         
         
    def _popupsearchcondwindow(self, index=-1):
        if index < 0:
            cond = ValueSearchCondition("", "")
        else:
            cond = self._getselectedfile().searchconds[index]
        
        window = Toplevel(self)
        
        title = Label(window, text="New Search Condition")
        title.grid(row=0, column=0, padx=5, pady=5, sticky=W + N)
        
        fieldlabel = Label(window, text="Field Name: ")
        fieldlabel.grid(row=1, column=0, padx=5, pady=5, sticky=W)
        
        fields = csvhandler.getfields(self._getselectedfile().filename)
        fieldvar = StringVar(window)
        fieldinput = Combobox(window, textvariable=fieldvar, values=fields, width=20)
        fieldinput.grid(row=1, column=1, columnspan=2, padx=5, pady=5, sticky=W)
        
        valuelabel = Label(window, text="Value: ")
        valuelabel.grid(row=3, column=0, padx=5, pady=5, sticky=W)
        
        valueinput = Entry(window)
        valueinput.grid(row=3, column=1, columnspan=2, padx=5, pady=5, sticky=W)
        
        minlabel = Label(window, text="Min Value: ")
        minlabel.grid(row=4, column=0, padx=5, pady=5, sticky=W)
        
        mininput = Entry(window)
        mininput.grid(row=4, column=1, columnspan=2, padx=5, pady=5, sticky=W)
        
        maxlabel = Label(window, text="Max Value: ")
        maxlabel.grid(row=5, column=0, padx=5, pady=5, sticky=W)
        
        maxinput = Entry(window)
        maxinput.grid(row=5, column=1, columnspan=2, padx=5, pady=5, sticky=W)
        
        sarchkind = IntVar()
        
        def _enablesingle():
            valueinput.config(state=NORMAL)
            mininput.config(state=DISABLED)
            maxinput.config(state=DISABLED)
            singlebutton.select()
            
        def _enablejoin():
            valueinput.config(state=DISABLED)
            mininput.config(state=NORMAL)
            maxinput.config(state=NORMAL)
            joinbutton.select()
            
        typelabel = Label(window, text="Search Type: ")
        typelabel.grid(row=2, column=0, padx=5, pady=5, sticky=W)
        
        singlebutton = Radiobutton(window, text="Single", variable=sarchkind, value=1, command=_enablesingle)
        singlebutton.grid(row=2, column=1, columnspan=1, padx=5, pady=5, sticky=W)
        
        joinbutton = Radiobutton(window, text="Range", variable=sarchkind, value=2, command=_enablejoin)
        joinbutton.grid(row=2, column=2, columnspan=1, padx=5, pady=5, sticky=W)
        
        # init value
        fieldvar.set(cond.field)
        if isinstance(cond, ValueSearchCondition):
            valueinput.insert(0, cond.val)
            _enablesingle()
        elif isinstance(cond, RangeSearchCondition):
            mininput.insert(0, cond.valmin)
            maxinput.insert(0, cond.valmax)
            _enablejoin()
            
        def _newcond():
            '''create new condition
            '''
            if sarchkind.get() == 1:
                cond = ValueSearchCondition(fieldvar.get(), valueinput.get())
            else:
                cond = RangeSearchCondition(fieldvar.get(), mininput.get(), maxinput.get())
            selectedfile = self._getselectedfile()
            if index < 0:
                selectedfile.searchconds.append(cond)
                
            else:
                del selectedfile.searchconds[index]
                selectedfile.searchconds[index:index] = [cond]
                
            self._inflatesearchcondlist(selectedfile)
            
            window.destroy()
        
        okbtn = Button(window, text="Confirm", width=7, command=_newcond)
        okbtn.grid(row=6, column=1, rowspan=1, columnspan=1, sticky=E, padx=5, pady=5)
        
        clsbtn = Button(window, text="Close", width=7, command=lambda: window.destroy())
        clsbtn.grid(row=6, column=2, rowspan=1, columnspan=1, sticky=E, padx=5, pady=5)
        
        
    def _popupjoincondwindow(self, index=-1):
        if index < 0:
            cond = JoinSearchCondition(("", ""))
            tofilename = ""
        else:
            condtuple = self._getselectedfile().joincondtuples[index]
            cond = condtuple[0]
            tofilename = condtuple[1]
            
        window = Toplevel(self)
        
        title = Label(window, text="New Search Condition")
        title.grid(row=0, column=0, padx=5, pady=5, sticky=W + N)
        
        filenamelabel = Label(window, text="Target Field Name: ")
        filenamelabel.grid(row=1, column=0, padx=5, pady=5, sticky=W)
        
        filevar = StringVar(window)
        filenameinput = Combobox(window, textvariable=filevar, values=self.filelist.get(0, END), width=30)
        filenameinput.grid(row=1, column=1, columnspan=2, padx=5, pady=5, sticky=W)
        
        fromfieldlabel = Label(window, text="Field in From File: ")
        fromfieldlabel.grid(row=3, column=0, padx=5, pady=5, sticky=W)
        
        fromfields = csvhandler.getfields(self._getselectedfile().filename)
        fromfieldvar = StringVar(window)
        fieldinput = Combobox(window, textvariable=fromfieldvar, values=fromfields, width=20)
        fieldinput.grid(row=3, column=1, columnspan=2, padx=5, pady=5, sticky=W)
        
        tofieldlabel = Label(window, text="Field in Target File: ")
        tofieldlabel.grid(row=4, column=0, padx=5, pady=5, sticky=W)
        
        tofields = []
        tofieldvar = StringVar(window)
        tofieldinput = Combobox(window, textvariable=tofieldvar, values=tofields, width=20)
        tofieldinput.grid(row=4, column=1, columnspan=2, padx=5, pady=5, sticky=W)
        
        def updatetofieldinput(evt):
            if filevar.get() is not None and len(filevar.get()) > 0:
                tofields = csvhandler.getfields(filevar.get())
                window.grid_slaves(4, 1)[0].grid_forget()
                tofieldinput = Combobox(window, textvariable=tofieldvar, values=tofields, width=20)
                tofieldinput.grid(row=4, column=1, columnspan=2, padx=5, pady=5, sticky=W)
        
        filenameinput.bind('<<ComboboxSelected>>', updatetofieldinput)
        
        # init value
        filevar.set(tofilename)
        fromfieldvar.set(cond.fieldtuple[0])
        updatetofieldinput(None)
        tofieldvar.set(cond.fieldtuple[1])
        
        def _newcond():
            '''create new condition
            '''
            cond = JoinSearchCondition((fromfieldvar.get(), tofieldvar.get()))
            tofilename = filevar.get()
            
            selectedfile = self._getselectedfile()
            if index < 0:
                selectedfile.joincondtuples.append((cond, tofilename))
                
            else:
                del selectedfile.joincondtuples[index]
                selectedfile.joincondtuples[index:index] = [(cond, tofilename)]
                
            self._inflatejoincondlist(selectedfile.joincondtuples)
            
            window.destroy()
        
        okbtn = Button(window, text="Confirm", width=7, command=_newcond)
        okbtn.grid(row=6, column=1, rowspan=1, columnspan=1, sticky=E, padx=5, pady=5)
        
        clsbtn = Button(window, text="Close", width=7, command=lambda: window.destroy())
        clsbtn.grid(row=6, column=2, rowspan=1, columnspan=1, sticky=W, padx=5, pady=5)
        
        
    def _getselectedfile(self):
        if self._getselectedfileindex() < 0:
            return None
        return self.filemodels[self._getselectedfileindex()]
    
    
    def _getselectedfileindex(self):
        return self.selectedfileindex
    
    
    def _setselectedfileindex(self, index):
        self.selectedfileindex = index
        if index >= 0:
            self.filelist.selection_set(index)
            
    def _getselectedsearchcondindex(self):
        if len(self.searchcondlist.curselection()) > 0:
            return self.searchcondlist.curselection()[0]
        return -1
    
    def _getselectedjoincondindex(self):
        if len(self.joincondlist.curselection()) > 0:
            return self.joincondlist.curselection()[0]
        return -1
Esempio n. 47
0
class AutocompleteEntry(Entry):

    def __init__(self, *args, **kwargs):
        Entry.__init__(self, width=100, *args, **kwargs)

        self.focus_set()
        self.pack()

        self.var = self["textvariable"]
        if self.var == '':
            self.var = self["textvariable"] = StringVar()

        self.var.trace('w', self.changed)
        self.bind("<Right>", self.selection)
        self.bind("<Up>", self.up)
        self.bind("<Down>", self.down)
        self.bind("<Return>", self.enter)
        self.lb_up = False
        self.lb = None

    def enter(self, event):
        print(event)

    def changed(self, name, index, mode):

        if self.var.get() == '':
            if self.lb:
                self.lb.destroy()
            self.lb_up = False
        else:
            words = self.comparison()
            if words:
                if not self.lb_up:
                    self.lb = Listbox(master=root, width=100)

                    self.lb.bind("<Double-Button-1>", self.selection)
                    self.lb.bind("<Right>", self.selection)
                    self.lb.place(x=self.winfo_x(), y=self.winfo_y()+self.winfo_height())
                    self.lb_up = True

                self.lb.delete(0, END)
                for w in words:
                    self.lb.insert(END,w)
            else:
                if self.lb_up:
                    self.lb.destroy()
                    self.lb_up = False

    def selection(self, _):

        if self.lb_up:
            self.var.set(self.lb.get(ACTIVE))
            self.lb.destroy()
            self.lb_up = False
            self.icursor(END)

    def up(self, _):

        if self.lb_up:
            if self.lb.curselection() == ():
                index = '0'
            else:
                index = self.lb.curselection()[0]
            if index != '0':
                self.lb.selection_clear(first=index)
                index = str(int(index)-1)
                self.lb.selection_set(first=index)
                self.lb.activate(index)

    def down(self, _):

        if self.lb_up:
            if self.lb.curselection() == ():
                index = '0'
            else:
                index = self.lb.curselection()[0]
            if index != END:
                self.lb.selection_clear(first=index)
                index = str(int(index)+1)
                self.lb.selection_set(first=index)
                self.lb.activate(index)

    def comparison(self):
        q = self.var.get()
        q = str(q.decode('utf8'))
        for hit in searcher.search(qp.parse(q), limit=50):
            if hit['author']:
                yield '%s. "%s"' % (hit['author'], hit['title'])
            else:
                yield hit['title']
class DrtGlueDemo(object):
    def __init__(self, examples):
        # Set up the main window.
        self._top = Tk()
        self._top.title("DRT Glue Demo")

        # Set up key bindings.
        self._init_bindings()

        # Initialize the fonts.self._error = None
        self._init_fonts(self._top)

        self._examples = examples
        self._readingCache = [None for example in examples]

        # The user can hide the grammar.
        self._show_grammar = IntVar(self._top)
        self._show_grammar.set(1)

        # Set the data to None
        self._curExample = -1
        self._readings = []
        self._drs = None
        self._drsWidget = None
        self._error = None

        self._init_glue()

        # Create the basic frames.
        self._init_menubar(self._top)
        self._init_buttons(self._top)
        self._init_exampleListbox(self._top)
        self._init_readingListbox(self._top)
        self._init_canvas(self._top)

        # Resize callback
        self._canvas.bind("<Configure>", self._configure)

    #########################################
    ##  Initialization Helpers
    #########################################

    def _init_glue(self):
        tagger = RegexpTagger(
            [
                ("^(David|Mary|John)$", "NNP"),
                ("^(walks|sees|eats|chases|believes|gives|sleeps|chases|persuades|tries|seems|leaves)$", "VB"),
                ("^(go|order|vanish|find|approach)$", "VB"),
                ("^(a)$", "ex_quant"),
                ("^(every)$", "univ_quant"),
                ("^(sandwich|man|dog|pizza|unicorn|cat|senator)$", "NN"),
                ("^(big|gray|former)$", "JJ"),
                ("^(him|himself)$", "PRP"),
            ]
        )

        depparser = MaltParser(tagger=tagger)
        self._glue = DrtGlue(depparser=depparser, remove_duplicates=False)

    def _init_fonts(self, root):
        # See: <http://www.astro.washington.edu/owen/ROTKFolklore.html>
        self._sysfont = Font(font=Button()["font"])
        root.option_add("*Font", self._sysfont)

        # TWhat's our font size (default=same as sysfont)
        self._size = IntVar(root)
        self._size.set(self._sysfont.cget("size"))

        self._boldfont = Font(family="helvetica", weight="bold", size=self._size.get())
        self._font = Font(family="helvetica", size=self._size.get())
        if self._size.get() < 0:
            big = self._size.get() - 2
        else:
            big = self._size.get() + 2
        self._bigfont = Font(family="helvetica", weight="bold", size=big)

    def _init_exampleListbox(self, parent):
        self._exampleFrame = listframe = Frame(parent)
        self._exampleFrame.pack(fill="both", side="left", padx=2)
        self._exampleList_label = Label(self._exampleFrame, font=self._boldfont, text="Examples")
        self._exampleList_label.pack()
        self._exampleList = Listbox(
            self._exampleFrame,
            selectmode="single",
            relief="groove",
            background="white",
            foreground="#909090",
            font=self._font,
            selectforeground="#004040",
            selectbackground="#c0f0c0",
        )

        self._exampleList.pack(side="right", fill="both", expand=1)

        for example in self._examples:
            self._exampleList.insert("end", ("  %s" % example))
        self._exampleList.config(height=min(len(self._examples), 25), width=40)

        # Add a scrollbar if there are more than 25 examples.
        if len(self._examples) > 25:
            listscroll = Scrollbar(self._exampleFrame, orient="vertical")
            self._exampleList.config(yscrollcommand=listscroll.set)
            listscroll.config(command=self._exampleList.yview)
            listscroll.pack(side="left", fill="y")

        # If they select a example, apply it.
        self._exampleList.bind("<<ListboxSelect>>", self._exampleList_select)

    def _init_readingListbox(self, parent):
        self._readingFrame = listframe = Frame(parent)
        self._readingFrame.pack(fill="both", side="left", padx=2)
        self._readingList_label = Label(self._readingFrame, font=self._boldfont, text="Readings")
        self._readingList_label.pack()
        self._readingList = Listbox(
            self._readingFrame,
            selectmode="single",
            relief="groove",
            background="white",
            foreground="#909090",
            font=self._font,
            selectforeground="#004040",
            selectbackground="#c0f0c0",
        )

        self._readingList.pack(side="right", fill="both", expand=1)

        # Add a scrollbar if there are more than 25 examples.
        listscroll = Scrollbar(self._readingFrame, orient="vertical")
        self._readingList.config(yscrollcommand=listscroll.set)
        listscroll.config(command=self._readingList.yview)
        listscroll.pack(side="right", fill="y")

        self._populate_readingListbox()

    def _populate_readingListbox(self):
        # Populate the listbox with integers
        self._readingList.delete(0, "end")
        for i in range(len(self._readings)):
            self._readingList.insert("end", ("  %s" % (i + 1)))
        self._readingList.config(height=min(len(self._readings), 25), width=5)

        # If they select a example, apply it.
        self._readingList.bind("<<ListboxSelect>>", self._readingList_select)

    def _init_bindings(self):
        # Key bindings are a good thing.
        self._top.bind("<Control-q>", self.destroy)
        self._top.bind("<Control-x>", self.destroy)
        self._top.bind("<Escape>", self.destroy)
        self._top.bind("n", self.next)
        self._top.bind("<space>", self.next)
        self._top.bind("p", self.prev)
        self._top.bind("<BackSpace>", self.prev)

    def _init_buttons(self, parent):
        # Set up the frames.
        self._buttonframe = buttonframe = Frame(parent)
        buttonframe.pack(fill="none", side="bottom", padx=3, pady=2)
        Button(buttonframe, text="Prev", background="#90c0d0", foreground="black", command=self.prev).pack(side="left")
        Button(buttonframe, text="Next", background="#90c0d0", foreground="black", command=self.next).pack(side="left")

    def _configure(self, event):
        self._autostep = 0
        (x1, y1, x2, y2) = self._cframe.scrollregion()
        y2 = event.height - 6
        self._canvas["scrollregion"] = "%d %d %d %d" % (x1, y1, x2, y2)
        self._redraw()

    def _init_canvas(self, parent):
        self._cframe = CanvasFrame(
            parent,
            background="white",
            # width=525, height=250,
            closeenough=10,
            border=2,
            relief="sunken",
        )
        self._cframe.pack(expand=1, fill="both", side="top", pady=2)
        canvas = self._canvas = self._cframe.canvas()

        # Initially, there's no tree or text
        self._tree = None
        self._textwidgets = []
        self._textline = None

    def _init_menubar(self, parent):
        menubar = Menu(parent)

        filemenu = Menu(menubar, tearoff=0)
        filemenu.add_command(label="Exit", underline=1, command=self.destroy, accelerator="q")
        menubar.add_cascade(label="File", underline=0, menu=filemenu)

        actionmenu = Menu(menubar, tearoff=0)
        actionmenu.add_command(label="Next", underline=0, command=self.next, accelerator="n, Space")
        actionmenu.add_command(label="Previous", underline=0, command=self.prev, accelerator="p, Backspace")
        menubar.add_cascade(label="Action", underline=0, menu=actionmenu)

        optionmenu = Menu(menubar, tearoff=0)
        optionmenu.add_checkbutton(
            label="Remove Duplicates",
            underline=0,
            variable=self._glue.remove_duplicates,
            command=self._toggle_remove_duplicates,
            accelerator="r",
        )
        menubar.add_cascade(label="Options", underline=0, menu=optionmenu)

        viewmenu = Menu(menubar, tearoff=0)
        viewmenu.add_radiobutton(label="Tiny", variable=self._size, underline=0, value=10, command=self.resize)
        viewmenu.add_radiobutton(label="Small", variable=self._size, underline=0, value=12, command=self.resize)
        viewmenu.add_radiobutton(label="Medium", variable=self._size, underline=0, value=14, command=self.resize)
        viewmenu.add_radiobutton(label="Large", variable=self._size, underline=0, value=18, command=self.resize)
        viewmenu.add_radiobutton(label="Huge", variable=self._size, underline=0, value=24, command=self.resize)
        menubar.add_cascade(label="View", underline=0, menu=viewmenu)

        helpmenu = Menu(menubar, tearoff=0)
        helpmenu.add_command(label="About", underline=0, command=self.about)
        menubar.add_cascade(label="Help", underline=0, menu=helpmenu)

        parent.config(menu=menubar)

    #########################################
    ##  Main draw procedure
    #########################################

    def _redraw(self):
        canvas = self._canvas

        # Delete the old DRS, widgets, etc.
        if self._drsWidget is not None:
            self._drsWidget.clear()

        if self._drs:
            self._drsWidget = DrsWidget(self._canvas, self._drs)
            self._drsWidget.draw()

        if self._error:
            self._drsWidget = DrsWidget(self._canvas, self._error)
            self._drsWidget.draw()

    #########################################
    ##  Button Callbacks
    #########################################

    def destroy(self, *e):
        self._autostep = 0
        if self._top is None:
            return
        self._top.destroy()
        self._top = None

    def prev(self, *e):
        selection = self._readingList.curselection()
        readingListSize = self._readingList.size()

        # there are readings
        if readingListSize > 0:
            # if one reading is currently selected
            if len(selection) == 1:
                index = int(selection[0])

                # if it's on (or before) the first item
                if index <= 0:
                    self._select_previous_example()
                else:
                    self._readingList_store_selection(index - 1)

            else:
                # select its first reading
                self._readingList_store_selection(readingListSize - 1)

        else:
            self._select_previous_example()

    def _select_previous_example(self):
        # if the current example is not the first example
        if self._curExample > 0:
            self._exampleList_store_selection(self._curExample - 1)
        else:
            # go to the last example
            self._exampleList_store_selection(len(self._examples) - 1)

    def next(self, *e):
        selection = self._readingList.curselection()
        readingListSize = self._readingList.size()

        # if there are readings
        if readingListSize > 0:
            # if one reading is currently selected
            if len(selection) == 1:
                index = int(selection[0])

                # if it's on (or past) the last item
                if index >= (readingListSize - 1):
                    self._select_next_example()
                else:
                    self._readingList_store_selection(index + 1)

            else:
                # select its first reading
                self._readingList_store_selection(0)

        else:
            self._select_next_example()

    def _select_next_example(self):
        # if the current example is not the last example
        if self._curExample < len(self._examples) - 1:
            self._exampleList_store_selection(self._curExample + 1)
        else:
            # go to the first example
            self._exampleList_store_selection(0)

    def about(self, *e):
        ABOUT = "NLTK Discourse Representation Theory (DRT) Glue Semantics Demo\n" + "Written by Daniel H. Garrette"
        TITLE = "About: NLTK DRT Glue Demo"
        try:
            from tkMessageBox import Message

            Message(message=ABOUT, title=TITLE).show()
        except:
            ShowText(self._top, TITLE, ABOUT)

    def postscript(self, *e):
        self._autostep = 0
        self._cframe.print_to_file()

    def mainloop(self, *args, **kwargs):
        """
        Enter the Tkinter mainloop.  This function must be called if
        this demo is created from a non-interactive program (e.g.
        from a secript); otherwise, the demo will close as soon as
        the script completes.
        """
        if in_idle():
            return
        self._top.mainloop(*args, **kwargs)

    def resize(self, size=None):
        if size is not None:
            self._size.set(size)
        size = self._size.get()
        self._font.configure(size=-(abs(size)))
        self._boldfont.configure(size=-(abs(size)))
        self._sysfont.configure(size=-(abs(size)))
        self._bigfont.configure(size=-(abs(size + 2)))
        self._redraw()

    def _toggle_remove_duplicates(self):
        self._glue.remove_duplicates = not self._glue.remove_duplicates

        self._exampleList.selection_clear(0, "end")
        self._readings = []
        self._populate_readingListbox()
        self._readingCache = [None for ex in self._examples]
        self._curExample = -1
        self._error = None

        self._drs = None
        self._redraw()

    def _exampleList_select(self, event):
        selection = self._exampleList.curselection()
        if len(selection) != 1:
            return
        self._exampleList_store_selection(int(selection[0]))

    def _exampleList_store_selection(self, index):
        self._curExample = index
        example = self._examples[index]

        self._exampleList.selection_clear(0, "end")
        if example:
            cache = self._readingCache[index]
            if cache:
                if isinstance(cache, list):
                    self._readings = cache
                    self._error = None
                else:
                    self._readings = []
                    self._error = cache
            else:
                try:
                    self._readings = self._glue.parse_to_meaning(example)
                    self._error = None
                    self._readingCache[index] = self._readings
                except Exception as e:
                    self._readings = []
                    self._error = DrtVariableExpression(Variable("Error: " + str(e)))
                    self._readingCache[index] = self._error

                    # add a star to the end of the example
                    self._exampleList.delete(index)
                    self._exampleList.insert(index, ("  %s *" % example))
                    self._exampleList.config(height=min(len(self._examples), 25), width=40)

            self._populate_readingListbox()

            self._exampleList.selection_set(index)

            self._drs = None
            self._redraw()

    def _readingList_select(self, event):
        selection = self._readingList.curselection()
        if len(selection) != 1:
            return
        self._readingList_store_selection(int(selection[0]))

    def _readingList_store_selection(self, index):
        reading = self._readings[index]

        self._readingList.selection_clear(0, "end")
        if reading:
            self._readingList.selection_set(index)

            self._drs = reading.simplify().normalize().resolve_anaphora()

            self._redraw()
Esempio n. 49
0
class LintGui:
    """Build and control a window to interact with pylint"""

    def __init__(self, root=None):
        """init"""
        self.root = root or Tk()
        self.root.title("Pylint")
        # reporter
        self.reporter = None
        # message queue for output from reporter
        self.msg_queue = queue.Queue()
        self.msgs = []
        self.filenames = []
        self.rating = StringVar()
        self.tabs = {}
        self.report_stream = BasicStream(self)
        # gui objects
        self.lbMessages = None
        self.showhistory = None
        self.results = None
        self.btnRun = None
        self.information_box = None
        self.convention_box = None
        self.refactor_box = None
        self.warning_box = None
        self.error_box = None
        self.fatal_box = None
        self.txtModule = None
        self.status = None
        self.msg_type_dict = None
        self.init_gui()

    def init_gui(self):
        """init helper"""
        # setting up frames
        top_frame = Frame(self.root)
        mid_frame = Frame(self.root)
        radio_frame = Frame(self.root)
        res_frame = Frame(self.root)
        msg_frame = Frame(self.root)
        check_frame = Frame(self.root)
        history_frame = Frame(self.root)
        btn_frame = Frame(self.root)
        rating_frame = Frame(self.root)
        top_frame.pack(side=TOP, fill=X)
        mid_frame.pack(side=TOP, fill=X)
        history_frame.pack(side=TOP, fill=BOTH, expand=True)
        radio_frame.pack(side=TOP, fill=BOTH, expand=True)
        rating_frame.pack(side=TOP, fill=BOTH, expand=True)
        res_frame.pack(side=TOP, fill=BOTH, expand=True)
        check_frame.pack(side=TOP, fill=BOTH, expand=True)
        msg_frame.pack(side=TOP, fill=BOTH, expand=True)
        btn_frame.pack(side=TOP, fill=X)

        # Message ListBox
        rightscrollbar = Scrollbar(msg_frame)
        rightscrollbar.pack(side=RIGHT, fill=Y)
        bottomscrollbar = Scrollbar(msg_frame, orient=HORIZONTAL)
        bottomscrollbar.pack(side=BOTTOM, fill=X)
        self.lbMessages = Listbox(
            msg_frame, yscrollcommand=rightscrollbar.set, xscrollcommand=bottomscrollbar.set, bg="white"
        )
        self.lbMessages.pack(expand=True, fill=BOTH)
        rightscrollbar.config(command=self.lbMessages.yview)
        bottomscrollbar.config(command=self.lbMessages.xview)

        # History ListBoxes
        rightscrollbar2 = Scrollbar(history_frame)
        rightscrollbar2.pack(side=RIGHT, fill=Y)
        bottomscrollbar2 = Scrollbar(history_frame, orient=HORIZONTAL)
        bottomscrollbar2.pack(side=BOTTOM, fill=X)
        self.showhistory = Listbox(
            history_frame, yscrollcommand=rightscrollbar2.set, xscrollcommand=bottomscrollbar2.set, bg="white"
        )
        self.showhistory.pack(expand=True, fill=BOTH)
        rightscrollbar2.config(command=self.showhistory.yview)
        bottomscrollbar2.config(command=self.showhistory.xview)
        self.showhistory.bind("<Double-Button-1>", self.select_recent_file)
        self.set_history_window()

        # status bar
        self.status = Label(self.root, text="", bd=1, relief=SUNKEN, anchor=W)
        self.status.pack(side=BOTTOM, fill=X)

        # labels
        self.lblRatingLabel = Label(rating_frame, text="Rating:")
        self.lblRatingLabel.pack(side=LEFT)
        self.lblRating = Label(rating_frame, textvariable=self.rating)
        self.lblRating.pack(side=LEFT)
        Label(mid_frame, text="Recently Used:").pack(side=LEFT)
        Label(top_frame, text="Module or package").pack(side=LEFT)

        # file textbox
        self.txtModule = Entry(top_frame, background="white")
        self.txtModule.bind("<Return>", self.run_lint)
        self.txtModule.pack(side=LEFT, expand=True, fill=X)

        # results box
        rightscrollbar = Scrollbar(res_frame)
        rightscrollbar.pack(side=RIGHT, fill=Y)
        bottomscrollbar = Scrollbar(res_frame, orient=HORIZONTAL)
        bottomscrollbar.pack(side=BOTTOM, fill=X)
        self.results = Listbox(
            res_frame, yscrollcommand=rightscrollbar.set, xscrollcommand=bottomscrollbar.set, bg="white", font="Courier"
        )
        self.results.pack(expand=True, fill=BOTH, side=BOTTOM)
        rightscrollbar.config(command=self.results.yview)
        bottomscrollbar.config(command=self.results.xview)

        # buttons
        Button(top_frame, text="Open", command=self.file_open).pack(side=LEFT)
        Button(top_frame, text="Open Package", command=(lambda: self.file_open(package=True))).pack(side=LEFT)

        self.btnRun = Button(top_frame, text="Run", command=self.run_lint)
        self.btnRun.pack(side=LEFT)
        Button(btn_frame, text="Quit", command=self.quit).pack(side=BOTTOM)

        # radio buttons
        self.information_box = IntVar()
        self.convention_box = IntVar()
        self.refactor_box = IntVar()
        self.warning_box = IntVar()
        self.error_box = IntVar()
        self.fatal_box = IntVar()
        i = Checkbutton(
            check_frame,
            text="Information",
            fg=COLORS["(I)"],
            variable=self.information_box,
            command=self.refresh_msg_window,
        )
        c = Checkbutton(
            check_frame,
            text="Convention",
            fg=COLORS["(C)"],
            variable=self.convention_box,
            command=self.refresh_msg_window,
        )
        r = Checkbutton(
            check_frame, text="Refactor", fg=COLORS["(R)"], variable=self.refactor_box, command=self.refresh_msg_window
        )
        w = Checkbutton(
            check_frame, text="Warning", fg=COLORS["(W)"], variable=self.warning_box, command=self.refresh_msg_window
        )
        e = Checkbutton(
            check_frame, text="Error", fg=COLORS["(E)"], variable=self.error_box, command=self.refresh_msg_window
        )
        f = Checkbutton(
            check_frame, text="Fatal", fg=COLORS["(F)"], variable=self.fatal_box, command=self.refresh_msg_window
        )
        i.select()
        c.select()
        r.select()
        w.select()
        e.select()
        f.select()
        i.pack(side=LEFT)
        c.pack(side=LEFT)
        r.pack(side=LEFT)
        w.pack(side=LEFT)
        e.pack(side=LEFT)
        f.pack(side=LEFT)

        # check boxes
        self.box = StringVar()
        # XXX should be generated
        report = Radiobutton(
            radio_frame, text="Report", variable=self.box, value="Report", command=self.refresh_results_window
        )
        rawMet = Radiobutton(
            radio_frame, text="Raw metrics", variable=self.box, value="Raw metrics", command=self.refresh_results_window
        )
        dup = Radiobutton(
            radio_frame, text="Duplication", variable=self.box, value="Duplication", command=self.refresh_results_window
        )
        ext = Radiobutton(
            radio_frame,
            text="External dependencies",
            variable=self.box,
            value="External dependencies",
            command=self.refresh_results_window,
        )
        stat = Radiobutton(
            radio_frame,
            text="Statistics by type",
            variable=self.box,
            value="Statistics by type",
            command=self.refresh_results_window,
        )
        msgCat = Radiobutton(
            radio_frame,
            text="Messages by category",
            variable=self.box,
            value="Messages by category",
            command=self.refresh_results_window,
        )
        msg = Radiobutton(
            radio_frame, text="Messages", variable=self.box, value="Messages", command=self.refresh_results_window
        )
        report.select()
        report.grid(column=0, row=0, sticky=W)
        rawMet.grid(column=1, row=0, sticky=W)
        dup.grid(column=2, row=0, sticky=W)
        msg.grid(column=3, row=0, sticky=E)
        stat.grid(column=0, row=1, sticky=W)
        msgCat.grid(column=1, row=1, sticky=W)
        ext.grid(column=2, row=1, columnspan=2, sticky=W)

        # dictionary for check boxes and associated error term
        self.msg_type_dict = {
            "I": lambda: self.information_box.get() == 1,
            "C": lambda: self.convention_box.get() == 1,
            "R": lambda: self.refactor_box.get() == 1,
            "E": lambda: self.error_box.get() == 1,
            "W": lambda: self.warning_box.get() == 1,
            "F": lambda: self.fatal_box.get() == 1,
        }
        self.txtModule.focus_set()

    def select_recent_file(self, event):
        """adds the selected file in the history listbox to the Module box"""
        if not self.showhistory.size():
            return

        selected = self.showhistory.curselection()
        item = self.showhistory.get(selected)
        # update module
        self.txtModule.delete(0, END)
        self.txtModule.insert(0, item)

    def refresh_msg_window(self):
        """refresh the message window with current output"""
        # clear the window
        self.lbMessages.delete(0, END)
        for msg in self.msgs:
            if self.msg_type_dict.get(msg[0])():
                msg_str = self.convert_to_string(msg)
                self.lbMessages.insert(END, msg_str)
                fg_color = COLORS.get(msg_str[:3], "black")
                self.lbMessages.itemconfigure(END, fg=fg_color)

    def refresh_results_window(self):
        """refresh the results window with current output"""
        # clear the window
        self.results.delete(0, END)
        try:
            for res in self.tabs[self.box.get()]:
                self.results.insert(END, res)
        except:
            pass

    def convert_to_string(self, msg):
        """make a string representation of a message"""
        if msg[2] != "":
            return "(" + msg[0] + ") " + msg[1] + "." + msg[2] + " [" + msg[3] + "]: " + msg[4]
        else:
            return "(" + msg[0] + ") " + msg[1] + " [" + msg[3] + "]: " + msg[4]

    def process_incoming(self):
        """process the incoming messages from running pylint"""
        while self.msg_queue.qsize():
            try:
                msg = self.msg_queue.get(0)
                if msg == "DONE":
                    self.report_stream.output_contents()
                    return False

                # adding message to list of msgs
                self.msgs.append(msg)

                # displaying msg if message type is selected in check box
                if self.msg_type_dict.get(msg[0])():
                    msg_str = self.convert_to_string(msg)
                    self.lbMessages.insert(END, msg_str)
                    fg_color = COLORS.get(msg_str[:3], "black")
                    self.lbMessages.itemconfigure(END, fg=fg_color)

            except queue.Empty:
                pass
        return True

    def periodic_call(self):
        """determine when to unlock the run button"""
        if self.process_incoming():
            self.root.after(100, self.periodic_call)
        else:
            # enabling button so it can be run again
            self.btnRun.config(state=NORMAL)

    def mainloop(self):
        """launch the mainloop of the application"""
        self.root.mainloop()

    def quit(self, _=None):
        """quit the application"""
        self.root.quit()

    def halt(self):
        """program halt placeholder"""
        return

    def file_open(self, package=False, _=None):
        """launch a file browser"""
        if not package:
            filename = askopenfilename(
                parent=self.root, filetypes=[("pythonfiles", "*.py"), ("allfiles", "*")], title="Select Module"
            )
        else:
            filename = askdirectory(title="Select A Folder", mustexist=1)

        if filename == ():
            return

        self.txtModule.delete(0, END)
        self.txtModule.insert(0, filename)

    def update_filenames(self):
        """update the list of recent filenames"""
        filename = self.txtModule.get()
        if not filename:
            filename = os.getcwd()
        if filename + "\n" in self.filenames:
            index = self.filenames.index(filename + "\n")
            self.filenames.pop(index)

        # ensure only 10 most recent are stored
        if len(self.filenames) == 10:
            self.filenames.pop()
        self.filenames.insert(0, filename + "\n")

    def set_history_window(self):
        """update the history window with info from the history file"""
        # clear the window
        self.showhistory.delete(0, END)
        # keep the last 10 most recent files
        try:
            view_history = open(HOME + HISTORY, "r")
            for hist in view_history.readlines():
                if not hist in self.filenames:
                    self.filenames.append(hist)
                self.showhistory.insert(END, hist.split("\n")[0])
            view_history.close()
        except IOError:
            # do nothing since history file will be created later
            return

    def run_lint(self, _=None):
        """launches pylint"""
        self.update_filenames()
        self.root.configure(cursor="watch")
        self.reporter = GUIReporter(self, output=self.report_stream)
        module = self.txtModule.get()
        if not module:
            module = os.getcwd()

        # cleaning up msgs and windows
        self.msgs = []
        self.lbMessages.delete(0, END)
        self.tabs = {}
        self.results.delete(0, END)
        self.btnRun.config(state=DISABLED)

        # setting up a worker thread to run pylint
        worker = Thread(target=lint_thread, args=(module, self.reporter, self))
        self.periodic_call()
        worker.start()

        # Overwrite the .pylint-gui-history file with all the new recently added files
        # in order from filenames but only save last 10 files
        write_history = open(HOME + HISTORY, "w")
        write_history.writelines(self.filenames)
        write_history.close()
        self.set_history_window()

        self.root.configure(cursor="")