Шустрый

Профиль
Группа: Участник
Сообщений: 74
Регистрация: 29.1.2010
Репутация: 3 Всего: 2
|
Спустя ~3 недели с момента начало изучения Питона написал свою первую, небольшую программку по хранению букмарков. Может кому из новичков в виде референса пригодиться.  Код | from tkinter import * import tkinter.messagebox import webbrowser
mainwin = Tk() mainwin.title("BOOKMARKS 101")
mainwin.geometry('310x410+600+350') mainwin.wm_iconbitmap('Comment.ico') bottomurl = StringVar() accdec = StringVar() accdec.set("acc")
bookhash = {}
def addnewbookmark():
def closeapp(): addnewapp.destroy()
def savenew(): addmark( bm1.get() , bm2.get() ) addnewapp.destroy()
addnewapp = Tk() addnewapp.title("Add new bookmark") addnewapp.geometry('300x150+615+365') addnewapp.wm_iconbitmap('Comment.ico') bl1 = Label( addnewapp, text = "bookmark name:" ) bl1.pack(side=TOP)
bm1 = Entry( addnewapp, width = 35 ) bm1.pack(side=TOP)
bl2 = Label( addnewapp, text = "bookmark itself:" ) bl2.pack(side=TOP)
bm2 = Entry( addnewapp, width = 35 ) bm2.pack(side=TOP) bm2.insert(0,"http://")
lx = bl1 = Label( addnewapp, text = " " ) lx.pack(side=LEFT, padx = 15 )
bb1 = Button ( addnewapp, text = "ADD", command = savenew ) bb1.pack(side=LEFT, padx = 5 ) bb2 = Button ( addnewapp, text = "CANCEL", command = closeapp ) bb2.pack(side=LEFT, padx = 5 )
addnewapp.mainloop()
def restorefile(): bookhash = {} bookhash['zinas']='http://www.apollo.lv' bookhash['radio']='http://www.latvijasradio.lv' bookhash['banka']='http://www.hanzanet.lv' bookhash['info']='http://www.1188.lv' bookhash['karte']='http://www.uzkartes.lv' bookhash['google']='http://www.google.com' workfile = open("bookmarks.txt","w") for s in bookhash: workfile.write("%s^^^%s\n" % (s , bookhash[s]) ) workfile.close() print("file restored!")
def readfromfile(): try: bookhash.clear() workfile = open("bookmarks.txt") print("\n* READING FILE: *") for each_line in workfile: if len(each_line) > 1: a = each_line c = a.find("^^^") print( "%s = %s" % ( a[0:c],a[c+3:len(a)-1]) ) bookhash[a[0:c]]=a[c+3:len(a)-1] workfile.close() except Exception as x: print(x) restorefile() readfromfile() readfromfile()
def addmark(a,b): print("\n'%s' added!" % a ) bookhash[a]=b savetofile() readfromfile() populate()
def deletebookmark(a): del bookhash[a] savetofile() def savetofile(): workfile = open("bookmarks.txt","w") for s in sorted(bookhash.keys()): workfile.write("%s^^^%s\n" % (s , bookhash[s]) ) workfile.close() print("file saved!")
def delete(): if tkinter.messagebox.askquestion("WARNING!","Are you sure you want to delete '%s' ?" % biglist.get( biglist.curselection() ) ) == "yes": print("\n'%s' deleted!" % biglist.get( biglist.curselection() ) ) deletebookmark(biglist.get( biglist.curselection() )) populate()
def goto(): url = bookhash[ biglist.get( biglist.curselection() ) ] print("Goto %s" % url ) webbrowser.open_new(url)
def about(): messagebox.showinfo("BOOKMARKS 101", "© Robert Mikelson \n 2010")
def populate(): biglist.delete(0, biglist.size() ) if accdec.get() == "acc": for s in sorted(bookhash.keys()): biglist.insert(END, s) else: for s in sorted(bookhash.keys(), reverse = True ): biglist.insert(END, s)
def readnpopulate(): readfromfile() populate()
def localrestorefile(): restorefile() readnpopulate()
# icons for the buttons iconadd=PhotoImage(file='Add.gif') icondel=PhotoImage(file='Delete.gif') icongoto=PhotoImage(file='Goto.gif')
# topmenu topmenu = Menu(mainwin) filemenu = Menu(topmenu, tearoff=0) topmenu.add_cascade(label="File", menu=filemenu) filemenu.add_command(label="Add", command = addnewbookmark) filemenu.add_command(label="Delete", command = delete) filemenu.add_command(label="Goto", command = goto) filemenu.add_separator() filemenu.add_command(label="Save to file", command = savetofile ) filemenu.add_command(label="Restore file", command = localrestorefile ) filemenu.add_separator() filemenu.add_command(label="Exit", command = mainwin.destroy) editmenu = Menu(topmenu, tearoff=0) topmenu.add_cascade(label="Sort", menu=editmenu) editmenu.add_radiobutton(label="Ascending (up)", variable = accdec , value = "acc", command = populate ) editmenu.add_radiobutton(label="Descending (down)", variable = accdec , value = "dec", command = populate ) helpmenu = Menu(topmenu, tearoff=0) topmenu.add_cascade(label="Help", menu=helpmenu) helpmenu.add_command(label="Read from file", command = readnpopulate) helpmenu.add_separator() helpmenu.add_command(label="About", command = about)
# buttons addbtn = Button (mainwin, image=iconadd, command = addnewbookmark ) addbtn.grid(row=1, column=0, padx=8, pady=5) delbtn = Button (mainwin, image=icondel, command = delete ) delbtn.grid(row=1, column=1, padx=8, pady=5) gotobtn = Button (mainwin, image=icongoto, command = goto ) gotobtn.grid(row=1, column=2, padx=8, pady=5)
# listbox biglist = Listbox ( mainwin, heigh = 18, width = 45, ) biglist.grid(row=2, column=0, columnspan=2000, padx=5 ,pady=5) scrollbar = Scrollbar(mainwin) scrollbar.grid(row=2, column=2000, sticky=(N,S))
# attach listbox to scrollbar biglist.config(yscrollcommand=scrollbar.set) scrollbar.config(command=biglist.yview)
def dolistbox(a): # when clicked on the listbox bottomurl.set(bookhash[ biglist.get( biglist.curselection() ) ])
# populate & operate listbox populate() biglist.bind("<Double-Button-1>", dolistbox ) # when clicked, do "dolistbox" func.
# bottom label bottomlabel = Label ( mainwin, textvariable = bottomurl ) bottomlabel.grid(row=3, column=0, columnspan=2125, padx=5 )
# closing up mainwin.config(menu=topmenu) mainwin.mainloop()
|
Присоединённый файл ( Кол-во скачиваний: 11 )
BM.zip 15,61 Kb
|