import tkinter as tk from tkinter import ttk import re from tem_pip import runpip # following structure that was recommended on http://stackoverflow.com/questions/17466561/best-way-to-structure-a-tkinter-application # I also review the code from https://gist.github.com/mikofski/5851633 about how to type clean the notebook app code class Pip_interface(tk.Frame): def __init__(self, parent): tk.Frame.__init__(self, parent) self.parent = parent self.package_list = [] self.search_result = {} self.parent.title("PIP Package Manager") self._create_panel() def _create_panel(self): nb = ttk.Notebook(self.parent, name="notebook") nb.pack(fill="both", expand="Y", padx=2, pady=3) self._create_search_tab(nb) self._create_installed_package_tab(nb) # I can make more tab function here def _create_installed_package_tab(self, nb): """ Function for installed package tab """ frame = tk.Frame(nb, name="installed package") frame.grid_rowconfigure(0, weight=1) frame.grid_rowconfigure(0, weight=1) installed_package_scrollbar = ttk.Scrollbar(frame, orient="vertical") installed_package_scrollbar.grid(column=1, row=2, sticky="S E") installed_package_listbox = tk.Listbox(frame, height=20, width=80, bg="black", fg="white", yscrollcommand=installed_package_scrollbar) installed_package_listbox.grid(column=0, row=2, columnspan=4, sticky="N S E W") self.get_and_display_pip_list(installed_package_listbox) update_button = ttk.Button(frame, text="Update")#command = self.update_package update_button.grid(column=2, row=12, sticky="N S W E") uninstall_button = ttk.Button(frame, text="Uninstall")#command = self.uninstall_package uninstall_button.grid(column=3, row=12, sticky="N S W E") installed_package_listbox.bind("", lambda x: self.show_installed_package_information(installed_package_listbox)) nb.add(frame, text="Installed Package") def _get_pip_list(self): status, out, err = runpip("list") package_list = out.split("\n") self.package_list = package_list def _display_pip_list(self, listbox): for i in range(0, len(self.package_list)): listbox.insert(i, self.package_list[i]) def get_and_display_pip_list(self, listbox): self._get_pip_list() self._display_pip_list(listbox) def show_installed_package_information(self, listbox): """ the output will display in a pop up window """ index = int(listbox.curselection()[0]) package_name = re.search(r"(\w+.*?)\(", self.package_list[index]).group(1) status, out, err = runpip("show {}".format(package_name)) toplevel = tk.Toplevel() toplevel.title(package_name) label = tk.Label(toplevel, text=out) label.pack() def _create_search_tab(self, nb): """ Function for searching tab """ frame = tk.Frame(nb, name="search") search_text = tk.StringVar() ttk.Entry(frame, width=10, textvariable=search_text).grid(column=1, row=1, columnspan=2, sticky="W E") search_scrollbar = ttk.Scrollbar(frame, orient="vertical") search_scrollbar.grid(column=1, row=2, sticky="S E") search_listbox = tk.Listbox(frame, height=20, width=40, bg="black", fg="white", yscrollcommand=search_scrollbar) search_listbox.grid(column=0, row=2, columnspan=2, sticky="N S E W") search_button = ttk.Button(frame, text="Search", command=lambda: self.get_and_display_data(search_text, search_listbox)) search_button.grid(column=3, row=1, sticky="W") selected_item_information = tk.StringVar() information_label = ttk.Label(frame, width=40, textvariable=selected_item_information) information_label.grid(column=2, row=2, columnspan=2, sticky="N") search_listbox.bind("", lambda x: self.show_item_information(search_listbox, selected_item_information)) install_button = ttk.Button(frame, text="Install")#command = self.install_package install_button.grid(column=3, row=12, sticky="N S W E") nb.add(frame, text="Seach Package") def _get_search_data(self, package_name): """ run the search and get the list of relevance package name and its version """ status, out, err = runpip("search {}".format(package_name.get())) # only extract the search package name with its version # and the description of package pattern = re.compile("(\w+.*?\(.*?\))\s+\-\s+(\w+.*?)\\n") out_to_package_name = pattern.findall(out) self.search_result = out_to_package_name def _display_search_data(self, listbox): """ display the list of data in self.search_result """ if listbox is None: for i in range(0, len(self.search_result)): listbox.insert(i, self.search_result[i][0]) else: listbox.delete(0, "end") for i in range(0, len(self.search_result)): listbox.insert(i, self.search_result[i][0]) def get_and_display_data(self, package_name, listbox): self._get_search_data(package_name) self._display_search_data(listbox) def show_item_information(self, listbox, selected_item_information): """ show the information of the selected package in list box """ index = int(listbox.curselection()[0]) information = self.sentence_process(self.search_result[index][1]) selected_item_information.set(information) def sentence_process(self, sentence): """ make the sentence fit into the label frame """ new_sentence = "" count = 0 for i in range(0, len(sentence)): if i % 40 == 0 and i != 0: new_sentence += sentence[i-40:i]+"\n" count += 1 new_sentence += sentence[count*40:len(sentence)] return new_sentence if __name__ == "__main__": root = tk.Tk() Pip_interface(root) root.mainloop()

AltStyle によって変換されたページ (->オリジナル) /