Exemple #1
0
class SearchWindow:
   "Window for file search using tags"

   def __init__(self, master, fileOp):
      self.slave = Toplevel(master)
      self.fo = fileOp
      # widgets
      self.files = Frame(self.slave)
      self.file_lst = Listbox(self.files, selectmode='single', height=20, font='Verdanda 10')
      self.scroll = Scrollbar(self.files, command=self.file_lst.yview)
      self.file_lst.configure(yscrollcommand=self.scroll.set)
      self.files.pack(side='top', fill='x', expand=1)
      self.file_lst.pack(side='left', fill='both', expand=1)
      self.scroll.pack(side='right', fill='y')
      self.var = StringVar()
      self.tags_edt = Entry(self.slave, textvariable=self.var, fg='red', font='Verdanda 10')
      self.tags_edt.pack(fill='x', expand=1)      
      self.btn_frame = Frame(self.slave)
      self.search_btn = Button(self.btn_frame, text='Search', width=14)
      self.reset_btn = Button(self.btn_frame, text='Reset', width=14)
      self.search_btn.pack(side='left')
      self.reset_btn.pack(side='right')
      self.btn_frame.pack()
      self.c_menu = Menu(self.slave, tearoff=0)
      self.c_menu.add_command(label='Add', command=lambda: self.exec(1))
      self.c_menu.add_command(label='Rename', command=self.tagRename)
      self.c_menu.add_command(label='Delete', command=self.tagDelete)
      #self.slave.title(self.fo.baseInfo())
      # bind
      self.reset_btn.bind('<Button-1>', self.reset)
      self.slave.bind('<Control-r>', self.reset)
      self.search_btn.bind('<Button-1>', self.printFiles)
      self.slave.bind('<Control-s>', self.printFiles)
      self.tags_edt.bind('<Return>', self.printFiles)
      self.tags_edt.bind('<Control-Right>', self.wordComplete)
      self.file_lst.bind('<ButtonRelease-1>', self.fileInfo)
      self.file_lst.bind('<Double-ButtonRelease-1>', self.exec)
      self.file_lst.bind('<Return>', self.exec)
      self.file_lst.bind('<ButtonRelease-3>', self.callMenu)
      self.slave.bind('<Escape>', lambda x: self.slave.destroy())
      self.slave.bind('<Control-o>', self.openPath)
      # state
      self.open_path = None
      self.state = SHOW_TAGS
      self.last_tag = ""
      self.last_tag_lst = []
      self.last_tag_num = 0
      # show all tags
      self.tags = self.fo.tagList()
      self.reset(1)

   def run(self):
      "Start execution for search window"
      self.slave.grab_set()
      #self.slave.focus_set()
      self.tags_edt.focus_set()
      self.slave.wait_window()
      return self.open_path

   def reset(self, ev):
      "Clear window, add list of tags"
      self.state = SHOW_TAGS
      self.files = []
      self.file_lst.delete(0, 'end')
      self.var.set("")
      self.file_lst['fg'] = 'red'
      for tag in self.tags:
         self.file_lst.insert('end', tag)
      self.slave.title(self.fo.baseInfo())
      #self.file_lst.focus_set()

   def printFiles(self, ev):
      "Show result of search"
      self.state = SHOW_FILES
      # get tags
      tag_str = self.var.get()
      tag_lst = [s.strip() for s in tag_str.split(',')]
      if not tag_lst: return
      # find and show files
      tmp = tag_lst[0]
      if tmp.startswith('?'):    # try to find files with given part of name
         self.files = self.fo.findByName(tmp.strip('? '))
      else:
         self.files = self.fo.findFiles(tag_lst)
      self.slave.title("Found: %d" % len(self.files))
      self.file_lst.delete(0, 'end')
      self.file_lst['fg'] = 'black'
      for grp in self.files:
         self.file_lst.insert('end', self.represent(grp))

   def represent(self, grp):
      "String with file representation"
      return "{0:32} /{1}".format(grp[1], os.path.split(grp[0])[-1])

   def fileInfo(self, ev):
      "Tags for current file"
      if self.state == SHOW_FILES and self.files:
         current = self.files[self.file_lst.index('active')]
         tags = self.fo.getTags(*current)
         self.var.set(', '.join(tags))

   def exec(self, ev):
      "Execute file or add tags to entry"
      if self.state == SHOW_FILES and self.files:
         current = self.files[self.file_lst.index('active')]
         return self.fo.execute(os.path.join(*current))
      else:
         add_tag = self.file_lst.get('active')
         cur_tag = self.var.get()
         self.var.set((cur_tag + ', ' + add_tag) if cur_tag else add_tag)

   def openPath(self, ev):
      "Open directory for current file in the file manager"
      if self.state == SHOW_FILES and self.files:
         current = self.files[self.file_lst.index('active')]
         self.open_path = current[0]
         self.slave.destroy()

   def callMenu(self, ev):
      "Create context menu for tags"
      if self.state == SHOW_TAGS:
         self.c_menu.post(ev.x_root, ev.y_root)

   def tagRename(self):
      "Change name of tag under cursor"
      current = self.file_lst.get('active')
      name = dlg.askstring("Rename tag", current)
      if name:
         self.fo.tagRename(name, current)
         self.tags = self.fo.tagList()
         self.reset(1)

   def tagDelete(self):
      "Delete tag under cursor from base"
      current = self.file_lst.get('active')
      if msg.askyesno("Delete?", current):
         self.fo.tagDelete(current)
         self.tags = self.fo.tagList()
         self.reset(1)

   def wordComplete(self, ev):
      "Complete tag name"
      # get last tag
      tag_str = self.var.get()
      tag_lst = [s.strip() for s in tag_str.split(',')]
      start = tag_lst[-1]
      if start == "": return
      # find equial tags in base
      if start.startswith(self.last_tag) and len(self.last_tag_lst) > 0:
         self.last_tag_num = (self.last_tag_num + 1) % len(self.last_tag_lst)
      else:
         self.last_tag_lst = self.fo.tagsStartsWith(start)
         if len(self.last_tag_lst) == 0: return
         self.last_tag, self.last_tag_num = start, 0
      # update string
      tag_lst[-1] = self.last_tag_lst[self.last_tag_num]
      self.var.set(', '.join(tag_lst))
Exemple #2
0
class unilexika(
):  #-----------------------------------------------Lexika Klasse----------------------------------------------------------------------------------------------------------
    def __init__(self):
        self.dateinamenliste = []
        self.eingabe = ""
        self.main = Tk()
        self.main.title("Unilexika")
        self.main.geometry("700x450")
        self.main.minsize(700, 450)
        self.main.configure(bg="#00e6e6")
        self.box = Listbox(self.main, relief="flat", bg="#e6ffff")
        self.box.place(relx=0, rely=0, relheight=1, relwidth=0.3)
        self.anzeige = Label(self.main, relief="flat", font=40, bg="#e6ffff")
        self.anzeige.place(relx=0.32, rely=0.3, relheight=0.7, relwidth=0.7)
        self.lesen = Button(self.main,
                            text="Lesen",
                            command=self.datei_öffnen,
                            font=25,
                            relief="flat",
                            bg="#0ca692",
                            fg="white",
                            activeforeground="white",
                            activebackground="#0ca692")
        self.lesen.place(relx=0.35, rely=0.05, relwidth=0.2, relheight=0.15)
        self.eintragen = Button(self.main,
                                text="Eintrag erstellen",
                                command=self.erstellen,
                                font=25,
                                relief="flat",
                                bg="#0ca692",
                                fg="white",
                                activeforeground="white",
                                activebackground="#0ca692")
        self.eintragen.place(relx=0.60,
                             rely=0.05,
                             relwidth=0.2,
                             relheight=0.15)
        self.beenden = Button(self.main,
                              text="Beenden",
                              command=lambda: self.main.destroy(),
                              font=25,
                              relief="flat",
                              bg="#0ca692",
                              fg="white",
                              activeforeground="white",
                              activebackground="#0ca692")
        self.beenden.place(relx=0.85, rely=0.05, relwidth=0.13, relheight=0.15)
        self.pruefung()
        self.main.mainloop()

    def pruefung(self):
        if len(self.dateinamenliste) == 0:
            self.f = open("Systemspeicher/Dateinamen.txt", "r")
            self.datei = open("Systemspeicher/Dateinamen.txt", "r")
            self.liste = open("Systemspeicher/Listennamen.txt", "r")
            self.zeile = 1
            self.zeilen = len(self.f.readlines())
            while self.zeile <= self.zeilen:
                self.dateinamenliste.append(self.datei.readline().strip())
                self.box.insert(END, self.liste.readline())
                self.zeile += 1
            self.f.close()
            self.liste.close()
            self.datei.close()

    def datei_öffnen(self):
        self.datei = open(
            "Speicher/" + self.dateinamenliste[self.box.index("active")], "r")
        self.infos = str(*self.datei.readlines())
        self.datei.close()
        self.anzeige.configure(text=self.infos, font=40)

    def erstellen(self):
        self.new = Toplevel()
        self.new.configure(bg="#00e6e6")
        self.lbl = Label(self.new, text="Listenname:", font=25, bg="#00e6e6")
        self.lbl.place(relx=0.1, rely=0, relheight=0.3, relwidth=0.8)
        self.listeneintragung = Entry(self.new, font=100, bg="#e6ffff")
        self.listeneintragung.place(relx=0.2,
                                    rely=0.3,
                                    relheight=0.3,
                                    relwidth=0.6)
        self.button = Button(self.new,
                             text="Weiter",
                             command=self.texteintrag,
                             relief="flat",
                             bg="#0ca692",
                             fg="white",
                             activeforeground="white",
                             activebackground="#0ca692")
        self.button.place(relx=0.2, rely=0.65, relwidth=0.6, relheight=0.3)

    def texteintrag(self):
        self.info = Toplevel()
        self.info.configure(bg="#00e6e6")
        self.info.geometry("1000x600")
        self.text = Text(self.info, bg="#e6ffff")
        self.text.place(relx=0, rely=0, relheight=1, relwidth=0.4)
        self.lbl = Label(self.info, text="Dateiname:", font=50, bg="#00e6e6")
        self.lbl.place(relx=0.45, rely=0, relheight=0.3, relwidth=0.5)
        self.eintragung = Entry(self.info, bg="#e6ffff")
        self.eintragung.place(relx=0.45,
                              rely=0.35,
                              relheight=0.3,
                              relwidth=0.5)
        self.bestätigung = Button(self.info,
                                  text="Fertig",
                                  command=self.einlesen,
                                  relief="flat",
                                  bg="#0ca692",
                                  fg="white",
                                  activeforeground="white",
                                  activebackground="#0ca692")
        self.bestätigung.place(relx=0.45,
                               rely=0.7,
                               relwidth=0.5,
                               relheight=0.25)

    def einlesen(self):
        self.dateinamenliste.append(self.eintragung.get() + ".txt")
        self.box.insert(END, self.listeneintragung.get())
        self.liste = open("Systemspeicher/Listennamen.txt", "a")
        self.liste.write(self.listeneintragung.get() + "\n")
        self.liste.close()
        self.eintrag = open("Speicher/" + self.eintragung.get() + ".txt", "a")
        self.eintrag.write(self.text.get("1.0", END))
        self.eintrag.close()
        self.documentname = open("Systemspeicher/Dateinamen.txt", "a")
        self.documentname.write(self.eintragung.get() + ".txt\n")
        self.documentname.close()
        self.new.destroy()
        self.info.destroy()
Exemple #3
0
 def indexkey(self,index):
     return sort(self.dic.keys())[Listbox.index(self,index)]
Exemple #4
0
class myListBox(Frame):
    def __init__(self,
                 root,
                 items=[],
                 id='',
                 item_select_handler=default_item_select_handler,
                 smode=tki.EXTENDED):
        self.item_count = 0
        self.root = root
        self.item_select_handler = item_select_handler
        Frame.__init__(self, self.root)
        self.id = id
        vscrollbar = Scrollbar(self, orient=tki.VERTICAL)
        vscrollbar.pack(side=tki.RIGHT, fill=tki.Y)
        hscrollbar = Scrollbar(self, orient=tki.HORIZONTAL)
        hscrollbar.pack(side=tki.BOTTOM, fill=tki.X)
        ## mode can be: SINGLE, BROWSE, MULTIPLE, EXTENDED
        ##        selectmode
        ##
        ## Determines how many items can be selected, and how mouse drags affect the selection −
        ##
        ##    BROWSE − Normally, you can only select one line out of a listbox. If you click on an item and then drag to a different line, the selection will follow the mouse. This is the default.
        ##    SINGLE − You can only select one line, and you can't drag the mouse.wherever you click button 1, that line is selected.
        ##    MULTIPLE − You can select any number of lines at once. Clicking on any line toggles whether or not it is selected.
        ##    EXTENDED − You can select any adjacent group of lines at once by clicking on the first line and dragging to the last line.

        self.list = Listbox(self,
                            selectmode=smode,
                            exportselection=0,
                            xscrollcommand=hscrollbar.set,
                            yscrollcommand=vscrollbar.set)
        for i in items:
            assert (type(i) is str)
            self.list.insert(items.index(i), i)
        self.list.pack(fill=tki.BOTH, expand=1)
        self.list.bind('<Double-Button-1>', self.item_select_handler)
        self.list.bind('<1>', self.item_select_handler)
        self.list.bind('<Return>', self.item_select_handler)
        ## DO NOT catch ListboxSelect event, because:
        ##        a) it is not associated with (x_root, y_root) and (x,y) coordinates, so the popup appears always at (0,0) of the main root window
        ##        b) it duplicates the click event catching  self.list.bind( '<1>', self.item_select_handler ) and generates a second event
        ## self.list.bind( '<<ListboxSelect>>', self.item_select_handler )
        hscrollbar.config(command=self.list.xview)
        vscrollbar.config(command=self.list.yview)
        self.pack(side=tki.LEFT, fill=tki.BOTH, expand=1)
        self.current = self.list.curselection()

    def insert_item(self, pos, item):
        self.list.insert(pos, item)
        self.item_count += 1
        self.activate(pos)
        self.index(pos)

    def delete(self, start, end=None):
        assert (type(start) is int)
        if (end is None):
            self.list.delete(start)
            self.item_count -= 1
        else:
            assert ((type(end) is int) or (end == tki.END))
            if (type(end) is str):
                self.list.delete(start, (self.item_count - 1))
                self.item_count -= (self.item_count - start)
            else:
                self.list.delete(start, end)
                self.item_count -= (end - start) + 1

    def select_set(self, i):
        self.list.selection_clear(0, tki.END)
        self.list.select_set(i)

    def activate(self, i):
        self.list.activate(i)

    def index(self, i):
        self.list.index(i)

    def generate_select_event(self, pos=None):
        assert (pos is not None)
        if (pos is not None):
            self.activate(pos)
            self.index(pos)
            self.select_set(pos)
            self.list.event_generate("<<ListboxSelect>>")

##    def poll(self):
##        now = self.list.curselection()
##        if now != self.current:
##            self.list_has_changed( now )
##            self.current = now
##        self.after( 250, self.poll )

    def list_has_changed(self, selection):
        print('widget {0} selection is {1}'.format(self.id, selection))
Exemple #5
0
class MultiDorf:
    def __init__(self, master: Tk):
        self.controls = []
        self.instances = []

        self.master = master
        self.master.title('MultiDorf ' + version + ' ' + platform)

        self.header_frame = Frame(master)
        self.header_frame.pack(side=tk.TOP, fill=tk.X)

        self.btn_frame = Frame(self.header_frame)
        self.btn_frame.pack(side=tk.LEFT, fill=tk.X)

        self.instance_btn = Button(self.btn_frame,
                                   text='New Instance',
                                   command=self.new_instance)
        self.instance_btn.pack(side=tk.LEFT)
        self.controls.append(self.instance_btn)

        self.reload_btn = Button(self.btn_frame,
                                 text='Reload instances',
                                 command=self.reload_instances)
        self.reload_btn.pack(side=tk.LEFT)
        self.controls.append(self.reload_btn)

        self.folders_btn = Button(self.btn_frame, text='Folders')
        self.folders_btn.pack(side=tk.LEFT)
        self.controls.append(self.folders_btn)

        self.settings_btn = Button(self.btn_frame,
                                   text='Settings',
                                   command=self.settings)
        self.settings_btn.pack(side=tk.LEFT)
        self.controls.append(self.settings_btn)

        self.help_btn = Button(self.btn_frame, text='Help', command=self.help)
        self.help_btn.pack(side=tk.LEFT)
        self.controls.append(self.help_btn)

        self.content_frame = Frame(master)
        self.content_frame.pack(side=tk.BOTTOM, expand=True, fill=tk.BOTH)

        self.instance_frame = Frame(self.content_frame)
        self.instance_frame.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)

        self.instance_lb = Listbox(self.instance_frame, selectmode=tk.SINGLE)
        self.instance_lb.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)

        self.instance_opt_frame = Frame(self.content_frame)
        self.instance_opt_frame.pack(side=tk.RIGHT)

        self.instance_launch_btn = Button(self.instance_opt_frame,
                                          text='Launch',
                                          command=self.launch_instance)
        self.instance_launch_btn.pack(side=tk.TOP)
        self.controls.append(self.instance_launch_btn)

        self.instance_edit_btn = Button(self.instance_opt_frame,
                                        text='Edit',
                                        command=self.edit_instance)
        self.instance_edit_btn.pack(side=tk.TOP)
        self.controls.append(self.instance_edit_btn)

        self.instance_saves_btn = Button(self.instance_opt_frame,
                                         text='View saves',
                                         command=self.view_saves)
        self.instance_saves_btn.pack(side=tk.TOP)
        self.controls.append(self.instance_saves_btn)

        self.instance_folder_btn = Button(self.instance_opt_frame,
                                          text='Instance folder',
                                          command=self.instance_folder)
        self.instance_folder_btn.pack(side=tk.TOP)
        self.controls.append(self.instance_folder_btn)

        self.instance_copy_btn = Button(self.instance_opt_frame,
                                        text='Copy instance',
                                        command=self.copy_instance)
        self.instance_copy_btn.pack(side=tk.TOP)
        self.controls.append(self.instance_copy_btn)

        self.instance_delete_btn = Button(self.instance_opt_frame,
                                          text='Delete instance',
                                          command=self.delete_instance)
        self.instance_delete_btn.pack(side=tk.TOP)
        self.controls.append(self.instance_delete_btn)

        self.instance_export_btn = Button(self.instance_opt_frame,
                                          text='Export instance',
                                          command=self.export_instance)
        self.instance_export_btn.pack(side=tk.TOP)
        self.controls.append(self.instance_export_btn)

        self.reload_instances()

    def export_instance(self) -> None:
        pass

    def delete_instance(self) -> None:
        instance = self.active_instance()
        if instance is not None:
            result = messagebox.askquestion('Delete',
                                            'Are you sure?',
                                            icon='warning')
            if result == 'yes':
                instance.delete_instance()
                self.reload_instances()

    def copy_instance(self) -> None:
        raise NotImplementedError

    def help(self) -> None:
        raise NotImplementedError

    def active_instance(self) -> Instance:
        cursel = self.instance_lb.curselection()
        if len(cursel) == 1:
            index = self.instance_lb.index(cursel[0])
            return self.instances[index]

    def view_saves(self) -> None:
        instance = self.active_instance()
        if instance is not None:
            files.browse(instance.directory / 'data' / 'save')

    def enable_controls(self):
        for c in self.controls:
            c.configure(state=tk.NORMAL)

    def disable_controls(self):
        for c in self.controls:
            c.configure(state=tk.DISABLED)

    def instance_folder(self) -> None:
        instance = self.active_instance()
        if instance is not None:
            files.browse(instance.directory)

    def reload_instances(self) -> None:
        self.instance_lb.delete(0, tk.END)
        self.instances = []
        for instance_dir in files.instance_dir.iterdir():
            if instance_dir.exists() and instance_dir.is_dir():
                instance = Instance(instance_dir)
                self.instances.append(instance)
        for instance in self.instances:
            self.instance_lb.insert(tk.END, instance)

    def edit_instance(self) -> None:
        raise NotImplementedError

    def new_instance(self) -> None:
        raise NotImplementedError

    def settings(self) -> None:
        raise NotImplementedError

    def launch_instance(self) -> None:
        instance = self.active_instance()
        if instance is not None:
            instance.launch(quit_callback=self.enable_controls())