Hello. I am creating a program that opens, saves or deletes links. However, although I can remove the selected item from the list of links, I can not delete the line from the file that corresponds to the selected item. I researched SOEN and found no enlightening results, and fileinput did not work either. Code:
# -*- coding: cp1252 -*-
from __future__ import print_function
from Tkinter import *
import threading
import urllib
import webbrowser
import os
import fileinput
root = Tk()
root.focus_force()
#define o tamanho da janela:
root.minsize(700,500)
root.maxsize(700,500)
#variaveis globais:
global texto_link
global meter_links
global listbox
#variavel de continuação de funçoes:
hiperlink = True
hipertrue = True
supertrue = True
#vaiavel de atualizacao:
atualiza = True
#lista temporária de links:
lista = []
#titulo da janela
root.title("Links Úteis - FastLabPyTool")
#icone do canto:
root.wm_iconbitmap('icone.ico')
#texto auxiliar:
def texto_auxiliar():
texto_link = Label(root, text = "Insere aqui o Link:", font = "Arial 10")
texto_link.place(x = 30, y = 50)
texto_auxiliar()
#insere caixas de texto:
meter_links = Entry(root, width = 70, fg = "blue")
meter_links.place(x = 150, y = 50)
#Ficheiro:
filename = "linkers.txt"
#coloca a lista de links:
def lista_links(f, line, item):
listbox = Listbox(root, width = 105, height = 20, bg = "cyan", fg = "blue")
with open(filename, "r") as f:
f = f.readlines()
for line in f:
listbox.insert(END, line)
listbox.bind('<Button-3>', lambda event: eventos_linkers(event, line, f, listbox, line, item))
listbox.place(x = 20, y = 100)
lista_links(hiperlink, hipertrue, supertrue)
#conecta o link à internet:
def link_conections(listbox):
items = listbox.curselection()
for item in items:
item = listbox.get(item)
webbrowser.get("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s").open(item)
#apaga os links seleccionados do arquivo:
def delete_links_arq(listbox, line, item):
f = open(filename, "r")
lines = f.readlines()
f.close()
f = open(filename, "w")
for line in lines:
if item in line:
f.write(item.replace(line, ''))
f.close()
"""for line in fileinput.input(filename, inplace=True):
if ( line):
continue
print (line, end='')"""
#apaga os links:
def delete_links(listbox, items, f, line, item):
items = listbox.curselection()
for item in items:
listbox.delete(item, item)
items = listbox.curselection()
item = listbox.get(item)
delete_links_arq(listbox, line, item)
#abre opções de linker através de eventos:
def eventos_linkers(event, items, f, listbox, line, item):
link_options = ["Abrir Hiperligação", "Eliminar Hiperligação"]
menu_options = Menu(root)
menubar = Menu(root, tearoff = 0)
menubar.add_cascade(label = link_options[0], command = lambda: link_conections(listbox))
menubar.add_cascade(label = link_options[1], command = lambda: delete_links(listbox, items, f, line, item))
menubar.tk_popup(event.x_root, event.y_root)
#decide se a caixa de texto contem um link:
def links_name(f, line, item):
texto = meter_links.get()
abrir = open(filename, "a")
if (len(str(texto))) > 0:
abrir.write((texto) + "\n")
abrir.close()
texto = 0
lista_links(f, line, item)
links_name(hiperlink, hipertrue, supertrue)
#coloca os botoes:
def botoes(f, line, item):
botao_submit = Button(root, text = "Guardar Link", bg = "cyan", fg = "black", command = lambda: links_name(f, line, item))
botao_submit.place(x = 600, y = 45)
botoes(hiperlink, hipertrue, supertrue)
#Apenas se valer mesmo a pena :)
"""
#cria um proceso no CPU:
def processo():
try:
processar = thread.start_new_thread( print_time, ("Thread-1", 2, ) )
processar.start()
except:
print "ERRO: Impossível criar processo!"
processo()
"""
#acaba o programa:
root.lift()
root.mainloop()