首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的笔记记录程序迁移到了一个类

我的笔记记录程序迁移到了一个类
EN

Code Review用户
提问于 2017-06-07 15:39:30
回答 2查看 302关注 0票数 8

我写了一个很好的笔记程序,但是我想把这个程序转换成一个更OOP风格的程序。当我正在学习OOP的概念和类的工作方式时,我确实成功地将我的程序转移到了一个类中,但这感觉是错误的。

我在这里不能正确使用self.。我的意思是在密码里到处都是。这个程序确实有效,但我只是不认为我需要几乎每一行都有self.

我希望看到下面的代码,因为我仍然不能100%确定如何正确构建类。

代码语言:javascript
复制
import tkinter as tk
from string import ascii_letters, digits
import tkinter.simpledialog
import tkinter.messagebox
from MINT.myScrollBar import MyScrollbar #customer scroll bar
import psutil
import json
import os
from os import listdir

class MintApp(tk.Frame):

    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, root, *args, **kwargs)
        self.root = root
        self.py_bg_color = "#{:02x}{:02x}{:02x}".format(0,34,64)
        self.py_frame_color = "#{:02x}{:02x}{:02x}".format(0, 23, 45)
        self.root.title("MINT: Mobile Information & Note-taking Tool")
        self.root.config(bg = self.py_frame_color)
        self.root.columnconfigure(0, weight = 0)
        self.root.columnconfigure(1, weight = 1)
        self.root.rowconfigure(0, weight = 0)
        self.root.rowconfigure(1, weight = 1)
        #~~~~~~~~~~~~~~~~~~~< Windows stuff >~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        self.row0label = tk.Label(self.root)
        self.row0label.grid(row = 0, column = 0)
        self.row0label.configure(bg = self.py_frame_color, text = "                                                                              ")
        #~~~~~~~~~~~~~~~~~~~< Variables Defaults >~~~~~~~~~~~~~~~~~~~~~~~
        self.path = "./NotesKeys/"
        self.color_path = "./Colors/"
        self.notebook = dict()
        self.current_working_lib = ""
        self.current_working_keys = ""
        self.list_of_all_filenames = []
        self.current_working_button_color = "orange"
        self.selected_text_color = "orange"
        self.selected_bg_color = "#%02x%02x%02x"
        self.post_update = False
        self.text_wordwrap = False
        self.valid_filename = ""
        self.current_keyword = ""
        #~~~~~~~~~~~~~~~~~~~< USE TO open all files in Directory >~~~~~~

        with open("{}{}".format(self.path, "list_of_all_filenames"), "r") as listall:
            self.list_of_all_filenames = json.load(listall)



        self.openAllFilesInPath(self.path)
        self.base_bg_image = tk.PhotoImage(file="./Colors/pybgbase.png")
        self.bgLable = tk.Label(self.root, image= self.base_bg_image)
        self.bgLable.place(x = 0, y = 0)

        self.bgLable.config(image = self.base_bg_image)
        self.bgLable.image = self.base_bg_image
        self.current_text_color = 'orange'

        self.textFrame = tk.Frame(root, borderwidth = 0, highlightthickness = 0)
        self.textFrame.grid(row = 0, column = 1, columnspan = 1, rowspan = 2, padx = 0, pady = 0, sticky = 'nsew')
        self.textFrame.columnconfigure(0, weight = 1)
        self.textFrame.rowconfigure(0, weight = 1)
        self.textFrame.columnconfigure(1, weight = 0)
        self.textFrame.rowconfigure(1, weight = 0)

        self.entryFrame = tk.Frame(self.root)
        self.entryFrame.grid(row = 0, column = 0, rowspan = 1, columnspan = 1, padx = 0, pady = 0, sticky = 'nsew')
        self.entryFrame.columnconfigure(0, weight = 0)
        self.entryFrame.columnconfigure(1, weight = 0)
        self.entryFrame.rowconfigure(0, weight = 0)
        self.entryFrame.rowconfigure(1, weight = 0)
        self.entryFrame.rowconfigure(2, weight = 0)
        self.entrybg_image = tk.Label(self.entryFrame, image = self.base_bg_image, borderwidth = 0, highlightthickness = 0)
        self.entrybg_image.image = self.base_bg_image
        self.entrybg_image.place(x = 0, y = 0)
        self.entrybg_image.config(image = self.base_bg_image)

        self.kwListFrame = tk.Frame(self.root, borderwidth = 0, highlightthickness = 0)
        self.kwListFrame.grid(row = 1, column = 0, rowspan = 1, columnspan = 1, padx = 0, pady = 0, sticky = 'nsew')
        self.kwListFrame.columnconfigure(0, weight = 1)
        self.kwbg_image = tk.Label(self.kwListFrame, image= self.base_bg_image, borderwidth = 0, highlightthickness = 0)
        self.kwbg_image.image = self.base_bg_image
        self.kwbg_image.place(x = 0, y = 0)
        self.kwbg_image.config(image = self.base_bg_image)

        self.root.textSideL = tk.Text(self.kwListFrame, width = 10, height = 20)
        self.root.textSideL.place( x = 5, y = 5)
        self.root.textSideL.config(wrap = 'none')
        self.root.textSideR = tk.Text(self.kwListFrame,  width = 10, height = 20)
        self.root.textSideR.place( x = 95, y = 5)
        self.root.textSideR.config(wrap = 'none')

        self.statusFrame = tk.Frame(root)
        self.statusFrame.config(bg = self.py_frame_color)
        self.statusFrame.grid(row = 3, column = 0, rowspan = 3, columnspan = 2, padx =0, pady =0, sticky = 'nsew')
        self.statusFrame.columnconfigure(0, weight = 1)
        self.statusFrame.columnconfigure(1, weight = 1)
        self.statusFrame.rowconfigure(0, weight = 0)

        self.root.text = tk.Text(self.textFrame, undo = True)
        self.root.text.grid(row = 0, column = 0, rowspan = 1, columnspan = 1, padx = 0, pady = 0, sticky = 'nsew')
        self.root.text.config(bg = self.py_frame_color, fg = "white", font = ('times', 16), insertbackground = "orange")
        self.root.text.config(wrap = 'none')
        #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        self.statusW = tk.Label(self.statusFrame, font=("times", 16, "bold"), fg = "white", bg = "black", relief = 'sunken', anchor = 'w')
        self.statusW.grid(row = 0, column = 0, padx = 1, pady = 1, sticky = 'sw')
        self.statusW.config(text = "Operation Status", bg = "#%02x%02x%02x" % (0, 23, 45))

        self.statusE = tk.Label(self.statusFrame, font=("times", 16, "bold"), fg = "white", bg = "black", relief = 'sunken', anchor = 'e')
        self.statusE.grid(row = 0, column = 1, padx = 1, pady = 1, sticky = 'se')
        self.statusE.config(bg = "#%02x%02x%02x" % (0, 23, 45))
        #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        self.searchLabel = tk.Label(self.entryFrame)
        self.searchLabel.grid(row = 1, column = 0, padx = 5, pady = 5)
        self.searchLabel.config(text = "Search Text Field")

        self.searchEntry = tk.Entry(self.entryFrame, width = 20)
        self.searchEntry.bind("<Return>", self.searchTextbox)
        self.searchEntry.bind("<Shift-Return>", self.next_match)
        self.searchEntry.bind("<Control-Return>", self.prev_match)
        self.searchEntry.grid(row = 1, column = 1, padx = 5, pady = 5)

        self.keywordLabel = tk.Label(self.entryFrame)
        self.keywordLabel.grid(row = 0, column = 0, padx = 5, pady = 5)
        self.keywordLabel.config(text = "Keyword Search")

        self.keywordEntry = tk.Entry(self.entryFrame, width = 20)
        self.keywordEntry.bind("<Return>", self.kw_entry)
        self.keywordEntry.grid(row = 0, column = 1, padx = 5, pady = 5)

        self.UpdateKeywordsButton = tkinter.Button(self.entryFrame, fg = 'Black', bg = 'Orange', text = "Update Notes", command = self.appendNotes)
        self.UpdateKeywordsButton.grid(row = 2, column = 0, padx = 5, pady = 5)


        self.libraryMenu()
        self.MintThemeDefault("#%02x%02x%02x" % (64,89,82), "#%02x%02x%02x" % (0, 23, 45), "#%02x%02x%02x" % (175, 167, 157), tk.PhotoImage(file = "./Colors/pybgbase.png"))


    def openAllFilesInPath(self, path):
        for filename in listdir(path):
            with open("{}{}".format(path, filename), "r") as f:
                self.notebook[filename] = json.load(f)

    def new_lib_prompt(self):
        a_name = tk.simpledialog.askstring("Create New Note Library", "Alphanumeric and '_' only", initialvalue = "Name_Here")
        valid_chars = "-_.() {}{}".format(ascii_letters, digits)
        self.valid_filename = ("".join(c for c in a_name if c in valid_chars)).replace(" ", "_").lower()
        if self.valid_filename != "" and self.valid_filename != "name_here":
            if self.valid_filename not in self.list_of_all_filenames:
                self.createNewNotesAndKeys(self.valid_filename)
                self.list_of_all_filenames.append(self.valid_filename)
                with open("%s%s"%(self.path, "list_of_all_filenames"), "r+" ) as f:
                        json.dump(self.list_of_all_filenames, f, indent = "")
                self.libraryMenu()
            else:
                print ("Library already exist")
        else:
            print ("No Name Given")

    def createNewNotesAndKeys(self, name):
        n_name = name+"_notes"
        k_name = name+"_keys"
        with open("./NotesKeys/default_notes", "r") as n:
            n_base = json.load(n)
        with open("./NotesKeys/default_keys", "r") as k:
            k_base = json.load(k)
        with open("%s%s" % (self.path,n_name), "w") as new_n:
            json.dump(n_base, new_n, indent = "")
        with open("%s%s" % (self.path,k_name), "w") as new_k:
            json.dump(k_base, new_k, indent = "")
        self.openAllFilesInPath(self.path, self.notebook, "list_of_all_filenames")
    #~~~~~~~~~~~~~~~~~~~< UPDATE keyword display >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def update_kw_display(self):
        list_to_pass = ["chose a library"," chose a library_keys", "chose a library_notes", ""]
        if self.current_working_keys not in list_to_pass:
            keys_to_be_updated = self.notebook[self.current_working_keys]
            self.root.textSideL.delete(1.0, "end-1c")
            self.root.textSideR.delete(1.0, "end-1c")
            contr = 0
            for item in keys_to_be_updated:
                if contr == 0:
                    self.root.textSideL.insert("end-1c",item + "\n")
                    self.root.textSideL.see("end-1c")
                    contr += 1
                else:
                    self.root.textSideR.insert("end-1c",item + "\n")
                    self.root.textSideR.see("end-1c")
                    contr = 0
        else:
            print("In the list to pass")
    #~~~~~~~~~~~~~~~~~~~< Search for words and highlight >~~~~~~~~~~~~~~~~~~~~~~~~
    def searchTextbox(self, event = None):
        self.root.text.tag_delete("search")
        self.root.text.tag_configure("search", background="green")
        start = "1.0"
        if len(self.searchEntry.get()) > 0:
            self.root.text.mark_set("insert", self.root.text.search(self.searchEntry.get(), start, nocase = None))
            self.root.text.see("insert")
            while True:
                pos = self.root.text.search(self.searchEntry.get(), start, 'end', nocase = None) 
                if pos == "": 
                    break       
                start = pos + "+%dc" % len(self.searchEntry.get()) 
                self.root.text.tag_add("search", pos, "%s + %dc" % (pos, len(self.searchEntry.get())))
        else:
            pass

    def next_match(self, event = None):
        # move cursor to end of current match
        while (self.root.text.compare("insert", "<", "end") and "search" in self.root.text.tag_names("insert")):
            self.root.text.mark_set("insert", "insert+1c")
        # find next character with the tag
        next_match = self.root.text.tag_nextrange("search", "insert")
        if next_match:
            self.root.text.mark_set("insert", next_match[0])
            self.root.text.see("insert")
        # prevent default behavior, in case this was called
        # via a key binding
        return "break"

    def prev_match(self, event = None):
        # move cursor to end of current match
        while (self.root.text.compare("insert", "<", "end") and "search" in self.root.text.tag_names("insert")):
            self.root.text.mark_set("insert", "insert-1c")
        # find previous character with the tag
        prev_match = self.root.text.tag_prevrange("search", "insert")
        if prev_match:
            self.root.text.mark_set("insert", prev_match[0])
            self.root.text.see("insert")
        # prevent default behavior, in case this was called
        # via a key binding
        return "break"
    #~~~~~~~~~~~~~~~~~~~< UPDATE selected_notes! >~~~~~~~~~~~~~~~~~~~
    def appendNotes(self):
        e1_current = self.keywordEntry.get().lower()
        e1_all_case = self.keywordEntry.get()
        e2_current = self.root.text.get(1.0, "end-1c")
        answer = tkinter.messagebox.askquestion("Update Notes!","Are you sure you want update your Notes for " + e1_all_case + " This cannot be undone!")
        if answer == "yes":
            if e1_current in self.notebook[self.current_working_lib]:
                self.statusE.config(text = "Updating Keyword & Notes for the " + self.current_working_lib + " Library!")
                dict_to_be_updated = self.notebook[self.current_working_lib]
                dict_to_be_updated[e1_current] = e2_current
                with open("%s%s" % (self.path, self.current_working_lib),"w") as working_temp_var:
                    json.dump(dict_to_be_updated, working_temp_var, indent = "")
                self.statusE.config(text = "Update Complete")          
            else:
                self.statusE.config(text= "Creating New Keyword & Notes for the " + self.current_working_lib + " Library!")
                dict_to_be_updated = self.notebook[self.current_working_lib]
                dict_to_be_updated[e1_current] = e2_current
                with open("%s%s" % (self.path, self.current_working_lib), "w" ) as working_temp_var:
                    json.dump(dict_to_be_updated, working_temp_var, indent = "")
                keys_to_be_updated = self.notebook[self.current_working_keys]
                keys_to_be_updated.append(e1_all_case)
                with open("%s%s" % (self.path, self.current_working_keys), "w" ) as working_temp_keys:
                    json.dump(keys_to_be_updated, working_temp_keys, indent = "")
                self.statusE.config(text = "Update Complete")
            self.update_kw_display()            
        else:
            tkinter.messagebox.showinfo("...", "That was close!")      
    #~~~~~~~~~~~~~~~~~~~< Entry Widget >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def kw_entry(self, event = None):
        e1_current = self.keywordEntry.get().lower()
        if self.current_keyword == e1_current:
            print("Already editing current keyword")
            pass
        else:
            answer = tkinter.messagebox.askquestion("Changing Notes!","Are you sure you want change the current Notes section to " + e1_current + "? Any unsaved changed will be lost!")
            if answer == "yes":
                if self.current_working_lib in self.notebook:
                    note_var = self.notebook[self.current_working_lib]
                    if e1_current in note_var:
                        self.root.text.delete(1.0, "end-1c")
                        self.root.text.insert("end-1c", note_var[e1_current])
                        self.root.text.see("end-1c")
                        self.current_keyword = e1_current
                    else:
                        self.root.text.delete(1.0, "end-1c")
                        self.root.text.insert("end-1c", "Not a Keyword")
                        self.root.text.see("end-1c")
                        self.current_keyword = e1_current
                else:
                    self.root.text.delete(1.0, "end-1c")
                    self.root.text.insert("end-1c", "No Library Selected")
                    self.root.text.see("end-1c")
            else:
                pass
    #~~~~~~~~~~~~~~~~~~~< Preset Themes >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


    def MintThemeDefault(self, main_bg, text_bg, txt_color, bg_image):
        self.currentTextColor = txt_color
        themebg_image = bg_image
        self.textFrame.config(bg = text_bg)
        self.entrybg_image.config(image = themebg_image)
        self.entrybg_image.image = themebg_image
        self.kwbg_image.config(image = themebg_image)
        self.kwbg_image.image = themebg_image
        self.bgLable.config(image = themebg_image)
        self.bgLable.image = themebg_image
        self.root.config(bg = main_bg)
        self.root.text.config(bg = text_bg, fg = txt_color)
        self.root.textSideL.config(bg = text_bg, fg = txt_color)
        self.root.textSideR.config(bg = text_bg, fg = txt_color)
        self.searchEntry.config(fg = txt_color, bg = text_bg)
        self.keywordEntry.config(fg = txt_color, bg = text_bg)
        self.statusFrame.config(bg = text_bg)
        self.statusE.config(fg = txt_color, bg = text_bg)
        self.statusW.config(fg = txt_color, bg = text_bg)
        self.searchLabel.config(fg = txt_color, bg = text_bg)
        self.keywordLabel.config(fg = txt_color, bg = text_bg)
        self.UpdateKeywordsButton.config(fg = txt_color, bg = text_bg)
    #~~~~~~~~~~~~~~~~~~< Custom Scroll Bar >~~~~~~~~~~~~~~~~~~~
        self.vScrollBar = MyScrollbar(self.textFrame, width = 15, command = root.text.yview, troughcolor = text_bg,
                                                buttontype = 'square', thumbcolor = txt_color, buttoncolor = main_bg)
        self.vScrollBar.grid(row = 0, column = 2, columnspan = 1, rowspan = 1, padx = 0, pady = 0, sticky = 'nse')
        self.root.text.configure(yscrollcommand = self.vScrollBar.set)
        self.vScrollBar.config(background = main_bg)
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        self.hScrollBar = MyScrollbar(self.textFrame, height = 15, command = root.text.xview, orient = 'horizontal', troughcolor = text_bg,
                                                buttontype = 'square', thumbcolor = txt_color, buttoncolor = main_bg)
        self.hScrollBar.grid(row = 1 , column = 0, columnspan = 1, rowspan = 1, padx = 0, pady = 0, sticky = 'sew')
        self.root.text.configure(xscrollcommand = self.hScrollBar.set)
        self.hScrollBar.config(background = main_bg)
    #~~~~~~~~~~~~~~~~~~< Theme Manager >~~~~~~~~~~~~~~~~~~~~~~~~
    def MintTheme1(self):
        main_bg_color = "#%02x%02x%02x" % (64, 89, 82)
        text_bg_color = "#%02x%02x%02x" % (17, 41, 41)
        txt_color = "#%02x%02x%02x" % (175, 167, 157)
        bg_image = tk.PhotoImage(file = "./Colors/theme1bg.png")
        self.MintThemeDefault(main_bg_color, text_bg_color, txt_color, bg_image)
    def MintTheme2(self):
        main_bg_color = "#%02x%02x%02x" % (14, 51, 51)
        text_bg_color = "#%02x%02x%02x" % (4, 22, 22)
        txt_color = "#%02x%02x%02x" % (223, 171, 111)
        bg_image = tk.PhotoImage(file="./Colors/theme2bg.png")
        self.MintThemeDefault(main_bg_color, text_bg_color, txt_color, bg_image)
    #~~~~~~~~~~~~~~~~~~~< Toggle Wordwrap >~~~~~~~~~~~~~~~~~~~~~~~~~~~    
    def toggleWordWrap(self):
        if self.text_wordwrap == False:
            self.root.text.config(wrap = 'char')
            self.text_wordwrap = True
        else:
            self.root.text.config(wrap = 'none')
            self.text_wordwrap = False
    #~~~~~~~~~~~~~~~~~~~< Menu function >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def updateWorkingLibKeys(self, filename):
        self.current_working_lib = "{}_notes".format(filename).lower()
        self.current_working_keys = "{}_keys".format(filename).lower()
        self.update_kw_display()
    def doNothing(self):
        pass
    def libraryMenu(self):
        self.menu = tk.Menu(self.root)
        self.root.config(menu = self.menu)
        self.fileMenu = tk.Menu(self.menu, tearoff = 0)
        self.menu.add_cascade(label = "File", menu = self.fileMenu)
        self.fileMenu.add_command(label = "Save", command = self.doNothing)
        self.fileMenu.add_command(label = "Save As", command = self.doNothing)
        self.fileMenu.add_separator()
        self.fileMenu.add_command(label = "Exit", command = lambda: self.closeProgram(self.root))

        self.libMenu = tk.Menu(self.menu)
        self.menu.add_cascade(label = "Note Libraries", menu = self.libMenu)
        self.libMenu.add_command(label = "Library Help Page - Not Implemented Yet", command = self.doNothing)
        self.libMenu.add_separator()
        self.libMenu.add_command(label = "New Library", command = self.new_lib_prompt)
        self.libMenu.add_command(label = "Lock Library - Not Implemented Yet", command = self.doNothing)
        self.libMenu.add_command(label = "Delete Library! - Not Implemented Yet", command = self.doNothing)
        self.libMenu.add_separator()

        self.prefMenu = tk.Menu(self.menu, tearoff = 0)
        self.menu.add_cascade(label = "Preferences", menu = self.prefMenu)
        self.prefMenu.add_command(label = "Mint Theme 1", command = self.MintTheme1)
        self.prefMenu.add_command(label = "Mint Theme 2", command = self.MintTheme2)
        self.libMenu.add_separator()
        self.prefMenu.add_command(label = "Toggle Word-Wrap", command = self.toggleWordWrap)

        self.helpMenu = tk.Menu(self.menu, tearoff = 0)
        self.menu.add_cascade(label = "Help", menu = self.helpMenu)
        self.helpMenu.add_command(label = "Info", command = self.doNothing)

        for filename in self.list_of_all_filenames:
            self.libMenu.add_command(label = "%s" % (filename), command = lambda filename = filename: self.updateWorkingLibKeys(filename))
    #~~~~~~~~~~~~~~~~~~~< Close >~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def closeProgram(self):
        answer = tkinter.messagebox.askquestion("Leaving MINT?", "Are you sure you want to leave MINT")
        if answer == "yes":
            answer = tkinter.messagebox.askquestion("Save work?", "Would you like to save before you exit MINT?")
            if answer == "yes":
                self.appendNotes
                self.root.destroy()
            else:
                self.root.destroy()
        else:
            tkinter.messagebox.showinfo("MINTy Fresh!", "Welcome Back XD")


if __name__ == "__main__":
    root = tk.Tk()

    MyApp = MintApp(root)
    print(MyApp.notebook["py_keys"])

    root.mainloop()

我不认为这很重要,但作为参考,这是我决定将所有内容转移到类之前开始的代码。

代码语言:javascript
复制
from tkinter import Tk, Label, PhotoImage, Menu, Frame, Text, Entry
from MINT.MintPack import doNothing, openAllFilesInPath, militaryTime  # my module
from string import ascii_letters, digits
import tkinter.simpledialog
import tkinter.messagebox
from MINT.myScrollBar import MyScrollbar
import psutil
import json
import os
# Created on Mar 21, 2017
# @author: Michael A McDonnal
# py_bg_color =  "#%02x%02x%02x" % (0, 34, 64)
py_bg_color =  "#%02x%02x%02x".format((0, '02x'), (34, '02x'), (64, '02x'))
py_frame_color =  "#%02x%02x%02x" % (0, 23, 45)
root = Tk()
root.title("MINT:    Mobile Information & Note-taking Tool")
# root.geometry("1050x900")
# root.minsize(800, 600)

root.config(bg = py_frame_color)
root.columnconfigure(0, weight = 0)
root.columnconfigure(1, weight = 1)
root.rowconfigure(0, weight = 0)
root.rowconfigure(1, weight = 1)
#root.rowconfigure(2, weight = 1)
#~~~~~~~~~~~~~~~~~~~< Windows stuff >~~~~~~~~~~~~~~~~~~~~~~~~~~~~
row0label = Label(root)
row0label.grid(row = 0, column = 0)
row0label.configure(text = "                                                                              ")
#~~~~~~~~~~~~~~~~~~~< Variables Defaults >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
path = "./NotesKeys/"
color_path = "./Colors/"
notebook = dict()
current_working_lib = ""
current_working_keys = ""
list_of_all_filenames = []
current_working_button_color = "orange"
selected_text_color = "orange"
selected_bg_color = "#%02x%02x%02x"
post_update = False
text_wordwrap = False
#~~~~~~~~~~~~~~~~~~~< USE TO open all files in Directory >~~~~~~~~~~~~~~~~~~~
with open("{}{}".format(path, "list_of_all_filenames"), "r") as listall:
    list_of_all_filenames = json.load(listall)

openAllFilesInPath(path, notebook)
#~~~~~~~~~~~~~~~~~~~< Prompt For New Library >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
valid_filename = ""
def new_lib_prompt():
    global list_of_all_filenames, path
    a_name = tkinter.simpledialog.askstring("Create New Note Library", "Alphanumeric and '_' only", initialvalue = "Name_Here")
    valid_chars = "-_.() {}{}".format(ascii_letters, digits)
    valid_filename = ("".join(c for c in a_name if c in valid_chars)).replace(" ", "_").lower()
    if valid_filename != "" and valid_filename != "name_here":
        if valid_filename not in list_of_all_filenames:
            createNewNotesAndKeys(valid_filename)
            list_of_all_filenames.append(valid_filename)
            with open("%s%s"%(path, "list_of_all_filenames"), "r+" ) as f:
                    json.dump(list_of_all_filenames, f, indent = "")
            libraryMenu()
        else:
            print ("Library already exist")
    else:
        print ("No Name Given")

def createNewNotesAndKeys(name):
    n_name = name+"_notes"
    k_name = name+"_keys"
    with open("./NotesKeys/default_notes", "r") as n:
        n_base = json.load(n)
    with open("./NotesKeys/default_keys", "r") as k:
        k_base = json.load(k)
    with open("%s%s" % (path,n_name), "w") as new_n:
        json.dump(n_base, new_n, indent = "")
    with open("%s%s" % (path,k_name), "w") as new_k:
        json.dump(k_base, new_k, indent = "")
    openAllFilesInPath(path, notebook, "list_of_all_filenames")
#~~~~~~~~~~~~~~~~~~~< UPDATE keyword display >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def update_kw_display():
    list_to_pass = ["chose a library"," chose a library_keys", "chose a library_notes", ""]
    if current_working_keys not in list_to_pass:
        keys_to_be_updated = notebook[current_working_keys]
        root.textSideL.delete(1.0, "end-1c")
        root.textSideR.delete(1.0, "end-1c")
        contr = 0
        for item in keys_to_be_updated:
            if contr == 0:
                root.textSideL.insert("end-1c",item + "\n")
                root.textSideL.see("end-1c")
                contr += 1
            else:
                root.textSideR.insert("end-1c",item + "\n")
                root.textSideR.see("end-1c")
                contr = 0
    else:
        print("In the list to pass")
#~~~~~~~~~~~~~~~~~~~< Search for words and highlight >~~~~~~~~~~~~~~~~~~~~~~~~
def searchTextbox(event = None):
    root.text.tag_delete("search")
    root.text.tag_configure("search", background="green")
    start = "1.0"
    if len(searchEntry.get()) > 0:
        root.text.mark_set("insert", root.text.search(searchEntry.get(), start, nocase = None))
        root.text.see("insert")
        while True:
            pos = root.text.search(searchEntry.get(), start, 'end', nocase = None) 
            if pos == "": 
                break       
            start = pos + "+%dc" % len(searchEntry.get()) 
            root.text.tag_add("search", pos, "%s + %dc" % (pos, len(searchEntry.get())))
    else:
        pass

def next_match(event = None):
    # move cursor to end of current match
    while (root.text.compare("insert", "<", "end") and "search" in root.text.tag_names("insert")):
        root.text.mark_set("insert", "insert+1c")
    # find next character with the tag
    next_match = root.text.tag_nextrange("search", "insert")
    if next_match:
        root.text.mark_set("insert", next_match[0])
        root.text.see("insert")
    # prevent default behavior, in case this was called
    # via a key binding
    return "break"

def prev_match(event = None):
    # move cursor to end of current match
    while (root.text.compare("insert", "<", "end") and "search" in root.text.tag_names("insert")):
        root.text.mark_set("insert", "insert-1c")
    # find previous character with the tag
    prev_match = root.text.tag_prevrange("search", "insert")
    if prev_match:
        root.text.mark_set("insert", prev_match[0])
        root.text.see("insert")
    # prevent default behavior, in case this was called
    # via a key binding
    return "break"
#~~~~~~~~~~~~~~~~~~~< UPDATE selected_notes! >~~~~~~~~~~~~~~~~~~~
def appendNotes():
    global current_working_lib, current_working_keys, path
    e1_current = keywordEntry.get().lower()
    e1_all_case = keywordEntry.get()
    e2_current = root.text.get(1.0, "end-1c")
    answer = tkinter.messagebox.askquestion("Update Notes!","Are you sure you want update your Notes for " + e1_all_case + " This cannot be undone!")
    if answer == "yes":
        if e1_current in notebook[current_working_lib]:
            statusE.config(text = "Updating Keyword & Notes for the " + current_working_lib + " Library!")
            dict_to_be_updated = notebook[current_working_lib]
            dict_to_be_updated[e1_current] = e2_current
            with open("%s%s" % (path, current_working_lib),"w") as working_temp_var:
                json.dump(dict_to_be_updated, working_temp_var, indent = "")
            statusE.config(text = "Update Complete")          
        else:
            statusE.config(text= "Creating New Keyword & Notes for the " + current_working_lib + " Library!")
            dict_to_be_updated = notebook[current_working_lib]
            dict_to_be_updated[e1_current] = e2_current
            with open("%s%s" % (path, current_working_lib), "w" ) as working_temp_var:
                json.dump(dict_to_be_updated, working_temp_var, indent = "")
            keys_to_be_updated = notebook[current_working_keys]
            keys_to_be_updated.append(e1_all_case)
            with open("%s%s" % (path, current_working_keys), "w" ) as working_temp_keys:
                json.dump(keys_to_be_updated, working_temp_keys, indent = "")
            statusE.config(text = "Update Complete")
        update_kw_display()            
    else:
        tkinter.messagebox.showinfo("...", "That was close!")      
#~~~~~~~~~~~~~~~~~~~< Entry Widget >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
current_keyword = ""
def kw_entry(event = None):
    global current_working_lib, current_keyword
    e1_current = keywordEntry.get().lower()
    if current_keyword == e1_current:
        print("Already editing current keyword")
        pass
    else:
        answer = tkinter.messagebox.askquestion("Changing Notes!","Are you sure you want change the current Notes section to " + e1_current + "? Any unsaved changed will be lost!")
        if answer == "yes":
            if current_working_lib in notebook:
                note_var = notebook[current_working_lib]
                if e1_current in note_var:
                    root.text.delete(1.0, "end-1c")
                    root.text.insert("end-1c", note_var[e1_current])
                    root.text.see("end-1c")
                    current_keyword = e1_current
                else:
                    root.text.delete(1.0, "end-1c")
                    root.text.insert("end-1c", "Not a Keyword")
                    root.text.see("end-1c")
                    current_keyword = e1_current
            else:
                root.text.delete(1.0, "end-1c")
                root.text.insert("end-1c", "No Library Selected")
                root.text.see("end-1c")
        else:
            pass
#~~~~~~~~~~~~~~~~~~~< Preset Themes >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
base_bg_image = PhotoImage(file="./Colors/pybgbase.png")
bgLable = Label(root, image= base_bg_image)
bgLable.place(x = 0, y = 0)

bgLable.config(image = base_bg_image)
bgLable.image = base_bg_image
current_text_color = 'orange'

def MintThemeDefault(main_bg, text_bg, txt_color, bg_image):
    global currentTextColor
    currentTextColor = txt_color
    themebg_image = bg_image
    textFrame.config(bg = text_bg)
    entrybg_image.config(image = themebg_image)
    entrybg_image.image = themebg_image
    kwbg_image.config(image = themebg_image)
    kwbg_image.image = themebg_image
    bgLable.config(image = themebg_image)
    bgLable.image = themebg_image
    root.config(bg = main_bg)
    root.text.config(bg = text_bg, fg = txt_color)
    root.textSideL.config(bg = text_bg, fg = txt_color)
    root.textSideR.config(bg = text_bg, fg = txt_color)
    searchEntry.config(fg = txt_color, bg = text_bg)
    keywordEntry.config(fg = txt_color, bg = text_bg)
    statusFrame.config(bg = text_bg)
    statusE.config(fg = txt_color, bg = text_bg)
    statusW.config(fg = txt_color, bg = text_bg)
    searchLabel.config(fg = txt_color, bg = text_bg)
    keywordLabel.config(fg = txt_color, bg = text_bg)
    UpdateKeywordsButton.config(fg = txt_color, bg = text_bg)
#~~~~~~~~~~~~~~~~~~< Custome Scroll Bar >~~~~~~~~~~~~~~~~~~~
    vScrollBar = MyScrollbar(textFrame, width = 15, command = root.text.yview, troughcolor = text_bg,
                                            buttontype = 'square', thumbcolor = txt_color, buttoncolor = main_bg)
    vScrollBar.grid(row = 0, column = 2, columnspan = 1, rowspan = 1, padx = 0, pady = 0, sticky = 'nse')
    root.text.configure(yscrollcommand = vScrollBar.set)
    vScrollBar.config(background = main_bg)
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    hScrollBar = MyScrollbar(textFrame, height = 15, command = root.text.xview, orient = 'horizontal', troughcolor = text_bg,
                                            buttontype = 'square', thumbcolor = txt_color, buttoncolor = main_bg)
    hScrollBar.grid(row = 1 , column = 0, columnspan = 1, rowspan = 1, padx = 0, pady = 0, sticky = 'sew')
    root.text.configure(xscrollcommand = hScrollBar.set)
    hScrollBar.config(background = main_bg)
#~~~~~~~~~~~~~~~~~~< Theme Manager >~~~~~~~~~~~~~~~~~~~~~~~~
def MintTheme1():
    main_bg_color = "#%02x%02x%02x" % (64, 89, 82)
    text_bg_color = "#%02x%02x%02x" % (17, 41, 41)
    txt_color = "#%02x%02x%02x" % (175, 167, 157)
    bg_image = PhotoImage(file = "./Colors/theme1bg.png")
    MintThemeDefault(main_bg_color, text_bg_color, txt_color, bg_image)
def MintTheme2():
    global currentTextColor
    main_bg_color = "#%02x%02x%02x" % (14, 51, 51)
    text_bg_color = "#%02x%02x%02x" % (4, 22, 22)
    txt_color = "#%02x%02x%02x" % (223, 171, 111)
    bg_image = PhotoImage(file="./Colors/theme2bg.png")
    MintThemeDefault(main_bg_color, text_bg_color, txt_color, bg_image)
#~~~~~~~~~~~~~~~~~~~< Toggle Wordwrap >~~~~~~~~~~~~~~~~~~~~~~~~~~~    
def toggleWordWrap():
    global text_wordwrap
    if text_wordwrap == False:
        root.text.config(wrap = 'char')
        text_wordwrap = True
    else:
        root.text.config(wrap = 'none')
        text_wordwrap = False
#~~~~~~~~~~~~~~~~~~~< Menu function >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def updateWorkingLibKeys(filename):
    global current_working_lib,current_working_keys
    current_working_lib = "{}_notes".format(filename).lower()
    current_working_keys = "{}_keys".format(filename).lower()
    update_kw_display()
def libraryMenu():
    menu = Menu(root)
    root.config(menu = menu)
    fileMenu = Menu(menu, tearoff = 0)
    menu.add_cascade(label = "File", menu = fileMenu)
    fileMenu.add_command(label = "Save", command = doNothing)
    fileMenu.add_command(label = "Save As", command = doNothing)
    fileMenu.add_separator()
    fileMenu.add_command(label = "Exit", command = lambda: closeProgram(root))

    libMenu = Menu(menu, tearoff = 0)
    menu.add_cascade(label = "Note Libraries", menu = libMenu)
    libMenu.add_command(label = "Library Help Page - Not Implemented Yet", command = doNothing)
    libMenu.add_separator()
    libMenu.add_command(label = "New Library", command = new_lib_prompt)
    libMenu.add_command(label = "Lock Library - Not Implemented Yet", command = doNothing)
    libMenu.add_command(label = "Delete Library! - Not Implemented Yet", command = doNothing)
    libMenu.add_separator()

    prefMenu = Menu(menu, tearoff = 0)
    menu.add_cascade(label = "Preferences", menu = prefMenu)
    prefMenu.add_command(label = "Mint Theme 1", command = MintTheme1)
    prefMenu.add_command(label = "Mint Theme 2", command = MintTheme2)
    libMenu.add_separator()
    prefMenu.add_command(label = "Toggle Word-Wrap", command = toggleWordWrap)

    helpMenu = Menu(menu, tearoff = 0)
    menu.add_cascade(label = "Help", menu = helpMenu)
    helpMenu.add_command(label = "Info", command = doNothing)

    for filename in list_of_all_filenames:
        libMenu.add_command(label = "%s" % (filename), command = lambda filename = filename: updateWorkingLibKeys(filename))

libraryMenu()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
textFrame = Frame(root, borderwidth = 0, highlightthickness = 0)
textFrame.grid(row = 0, column = 1, columnspan = 1, rowspan = 2, padx = 0, pady = 0, sticky = 'nsew')
textFrame.columnconfigure(0, weight = 1)
textFrame.rowconfigure(0, weight = 1)
textFrame.columnconfigure(1, weight = 0)
textFrame.rowconfigure(1, weight = 0)

entryFrame = Frame(root)
entryFrame.grid(row = 0, column = 0, rowspan = 1, columnspan = 1, padx = 0, pady = 0, sticky = 'nsew')
entryFrame.columnconfigure(0, weight = 0)
entryFrame.columnconfigure(1, weight = 0)
entryFrame.rowconfigure(0, weight = 0)
entryFrame.rowconfigure(1, weight = 0)
entryFrame.rowconfigure(2, weight = 0)
entrybg_image = Label(entryFrame, image = base_bg_image, borderwidth = 0, highlightthickness = 0)
entrybg_image.image = base_bg_image
entrybg_image.place(x = 0, y = 0)
entrybg_image.config(image = base_bg_image)

kwListFrame = Frame(root, borderwidth = 0, highlightthickness = 0)
kwListFrame.grid(row = 1, column = 0, rowspan = 1, columnspan = 1, padx = 0, pady = 0, sticky = 'nsew')
kwListFrame.columnconfigure(0, weight = 1)
kwbg_image = Label(kwListFrame, image= base_bg_image, borderwidth = 0, highlightthickness = 0)
kwbg_image.image = base_bg_image
kwbg_image.place(x = 0, y = 0)
kwbg_image.config(image = base_bg_image)

root.textSideL = Text(kwListFrame, width = 10, height = 20)
root.textSideL.place( x = 5, y = 5)
root.textSideL.config(wrap = 'none')
root.textSideR = Text(kwListFrame,  width = 10, height = 20)
root.textSideR.place( x = 95, y = 5)
root.textSideR.config(wrap = 'none')

statusFrame = Frame(root)
statusFrame.config(bg = py_frame_color)
statusFrame.grid(row = 3, column = 0, rowspan = 3, columnspan = 2, padx =0, pady =0, sticky = 'nsew')
statusFrame.columnconfigure(0, weight = 1)
statusFrame.columnconfigure(1, weight = 1)
statusFrame.rowconfigure(0, weight = 0)

root.text = Text(textFrame, undo = True)
root.text.grid(row = 0, column = 0, rowspan = 1, columnspan = 1, padx = 0, pady = 0, sticky = 'nsew')
root.text.config(bg = py_frame_color, fg = "white", font = ('times', 16), insertbackground = "orange")
root.text.config(wrap = 'none')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
statusW = Label(statusFrame, font=("times", 16, "bold"), fg = "white", bg = "black", relief = 'sunken', anchor = 'w')
statusW.grid(row = 0, column = 0, padx = 1, pady = 1, sticky = 'sw')
statusW.config(text = "Operation Status", bg = "#%02x%02x%02x" % (0, 23, 45))

statusE = Label(statusFrame, font=("times", 16, "bold"), fg = "white", bg = "black", relief = 'sunken', anchor = 'e')
statusE.grid(row = 0, column = 1, padx = 1, pady = 1, sticky = 'se')
statusE.config(bg = "#%02x%02x%02x" % (0, 23, 45))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
searchLabel = Label(entryFrame)
searchLabel.grid(row = 1, column = 0, padx = 5, pady = 5)
searchLabel.config(text = "Search Text Field")

searchEntry = Entry(entryFrame, width = 20)
searchEntry.bind("<Return>", searchTextbox)
searchEntry.bind("<Shift-Return>", next_match)
searchEntry.bind("<Control-Return>", prev_match)
searchEntry.grid(row = 1, column = 1, padx = 5, pady = 5)

keywordLabel = Label(entryFrame)
keywordLabel.grid(row = 0, column = 0, padx = 5, pady = 5)
keywordLabel.config(text = "Keyword Search")

keywordEntry = Entry(entryFrame, width = 20)
keywordEntry.bind("<Return>", kw_entry)
keywordEntry.grid(row = 0, column = 1, padx = 5, pady = 5)

UpdateKeywordsButton = tkinter.Button(entryFrame, fg = 'Black', bg = 'Orange', text = "Update Notes", command = appendNotes)
UpdateKeywordsButton.grid(row = 2, column = 0, padx = 5, pady = 5)

MintThemeDefault("#%02x%02x%02x" % (64,89,82), "#%02x%02x%02x" % (0, 23, 45), "#%02x%02x%02x" % (175, 167, 157), PhotoImage(file = "./Colors/pybgbase.png"))
#~~~~~~~~~~~~~~~~~~~< Close Program >~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def statusClock():
    statusE.config(text = "{}".format(militaryTime()) + "  Preparing to do nothing...")
    statusE.after(200, statusClock)
statusClock()
#~~~~~~~~~~~~~~~~~~~< Close Program >~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def closeProgram():
    answer = tkinter.messagebox.askquestion("Leaving MINT?", "Are you sure you want to leave MINT")
    if answer == "yes":
        answer = tkinter.messagebox.askquestion("Save work?", "Would you like to save before you exit MINT?")
        if answer == "yes":
            appendNotes
            root.destroy()
        else:
            root.destroy()
    else:
        tkinter.messagebox.showinfo("MINTy Fresh!", "Welcome Back XD")
#~~~~~~~~~~~~~~~~~~~< root Main Loop >~~~~~~~~~~~~~~~~~~~~~~~~~~~~
process = psutil.Process(os.getpid())
print(process.memory_info().rss)
root.protocol("WM_DELETE_WINDOW", closeProgram)
root.mainloop()
EN

回答 2

Code Review用户

回答已采纳

发布于 2017-06-10 16:57:08

我也刚开始用Python编程,两个月前,我还在试图用tkinter :)构建应用程序时遇到了同样的问题。当我理解这个概念的时候,我会尝试用术语来解释,并希望它能为你努力实现的目标提供一些启示,但我远不是专家:)。

使用self.variable = value的思想是将该值添加为对象(self)的属性,然后能够在类中的任何地方回忆该值。如果不将其作为对象的属性添加,则无法在声明变量的位置以外的任何函数中访问该值。

因此,您必须问自己一个问题:在定义变量的作用域以外的任何地方,我是否需要访问这个变量?这意味着在类内的另一个函数中,也在主脚本中,通过调用实例属性。

如果您不需要访问它,那么让它具有对象属性是没有意义的。

至于tkinter,您创建了一些对象,如果不将其作为类实例的属性添加,它们将被垃圾收集和销毁(它们将被创建,并将立即消失)。

在我的头顶上,来自tkinter的这些对象是: StringVar、IntVar和其他Var类型的特殊变量。另外,如果您创建了显示图像的对象--它们(图像)必须作为属性添加,否则它们将不显示。

例如,tkinter的其他元素,如entry或按钮,如果不像我的测试显示的那样作为属性添加,就不会消失,并且可以在不将它们作为属性添加的情况下创建它们。如果按钮中有一个命令,但无论如何都需要访问按钮对象本身,那么首先必须将按钮作为实例属性。(例如,其中一个函数更改按下的按钮的配置以更改它的外观-尽管该按钮将正常显示,并且不会收到垃圾收集,但您必须将其作为实例属性添加到更改其外观的函数中。)

希望它至少对你有一点帮助。编码愉快!

托马什

票数 3
EN

Code Review用户

发布于 2017-06-07 19:40:09

这篇评论将是一张便条小马..。

我查看了您之前提交的评论,其中一个非常一致的主题是Python约定,它从pep8开始。显然,您正在努力成为一个更好的程序员,但是pep8的建议并没有被坚持。所以我要假设这是因为它的效用并不明显。

以下是一些随机的想法试图动摇你..。:-)

关于软件质量的

维基百科

我们将从维基百科的引语开始:

质量软件同行评审经常涉及到读取源代码。这种类型的同行评审主要是一种缺陷检测活动。根据定义,在提交代码供审阅之前,只有代码的原始作者读取了源文件。使用一致准则编写的代码更容易让其他审查员理解和吸收,从而提高缺陷检测过程的效率。

问:黑客和SW工程师有什么区别?

答:黑客编写的代码工作,SW工程师编写的代码,其他人可以工作。

这是我尝试的一个笑话,试图说明风格一致性的重要性。接下来的笑话是,当你回到自己的代码时,当你离开一段时间后,其他人常常会成为你自己,然后看着它,然后开始感叹。

,但这有点痛。

是的是..。直到你擅长于此。最好的方法就是练习。

我的第一条建议是买一个样式/棉线检查器。我使用吡咯烷酮,它将显示编辑器中的样式和编译问题。如果有任何违规行为,它会在右边的栏中加上一个小标记,而把这些标记敲掉也不像让代码起作用那样令人满意,但它同样能给人一种成就感。

票数 4
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/165176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档