Jump to content

Procurador de texto em .txt


Gust

Recommended Posts

Tou a fazer um prgm que anliza ficheiros .txt e devolve uma tring com a palavra procurada.

from Tkinter import *

def editbox (lixo):
    scroll2.grid(row = 1, column = 5)
    text.grid(row = 0, column = 5)
    text.insert(0, (finalresult.get(finalresult.curselection())))
    
def search():
    result = []
    logfile = open((entry.get() + ".txt"), "r")
    log = logfile.read().split("\n")
    logfile.close()
    for x in log:
        x.split(" ")
        result.append(x)
    for x in result:
        x.replace("[Alt]2", "@")
        a = x.find(entry2.get())
        if a != -1:
            finalresult.insert(END, x)
    finalresult.grid(row = 3, column = 0, columnspan = 4, sticky = N+S+W+E, padx = 5, pady = 10)
    scroll.grid(row = 3, column = 4, sticky = N+S+W, pady = 10)  

if __name__ == "__main__":
    tk = Tk()
    main = Frame(tk)
    main.pack()
    scroll = Scrollbar(main)
    scroll2 = Scrollbar(main, orient = HORIZONTAL)
    text = Entry(main, xscrollcommand = scroll2.set)
    finalresult = Listbox(main, yscrollcommand = scroll.set, selectmode = SINGLE)
    entry = Entry(main)
    entry2 = Entry(main)
    entry2.grid(row = 1, column = 3, columnspan = 2)
    entry.grid(row = 0, column = 3, columnspan = 2)
    entry.insert(0, "*")
    entry.selection_range(0, END)
    Button(main, text="Sair", command=main.quit).grid(row = 2, column = 2, padx = 5, pady = 10, columnspan = 2)
    Button(main, text="Procurar", command=search).grid(row = 2, column = 0, padx = 5, pady = 10, columnspan = 2)
    Label(main, text="Ficheiro de log:").grid(row = 0, column = 0, columnspan = 2, pady = 10)
    Label(main, text="Palavra que deseja:").grid(row = 1, column = 0, columnspan = 2)
    finalresult.bind("<Double-Button-1>", editbox)
    scroll.config(command = finalresult.yview)
    scroll2.config(command = text.xview)
    tk.mainloop()

Mas gera o seguinte erro:

Exception in Tkinter callback

Traceback (most recent call last):

  File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__

    return self.func(*args)

TypeError: xview() takes exactly 2 arguments (4 given)

Não faço a minima ideia porque.

Alguem podia dar uma olhadela.

"A computer program it's a world, the programmer its God"

Link to comment
Share on other sites

Já resolvi o erro.

Utilizei o link que me deste Triton.

Obr.

Fica o source:

from Tkinter import *

def scrolling(*L): #esta funcao foi copiada daqui: http://infohost.nmt.edu/tcc/help/pubs/tkinter/entry-scrolling.html
        op, howMany = L[0], L[1]
        if op == "scroll":
            units  =  L[2]
            text.xview_scroll (howMany, units)
        elif op == "moveto":
            text.xview_moveto (howMany)

def gravar ():
    savfile = open((entry3.get()+".txt"), "w")
    savfile.write(text.get())
    savfile.close()

def editbox (lixo):
    scroll2.grid(row = 1, column = 5)
    text.grid(row = 0, column = 5)
    entry3.grid(row = 2, column = 5, sticky = N)
    savebutton.grid(row = 2, column = 5, sticky = S)
    text.insert(0, (finalresult.get(finalresult.curselection())))
    
def search():
    result = []
    logfile = open((entry.get() + ".txt"), "r")
    log = logfile.read().split("\n")
    logfile.close()
    for x in log:
        x.split(" ")
        result.append(x)
    for x in result:
        x.replace("[Alt]2", "@")
        a = x.find(entry2.get())
        if a != -1:
            finalresult.insert(END, x)
    finalresult.grid(row = 3, column = 0, columnspan = 4, sticky = N+S+W+E, padx = 5, pady = 10)
    scroll.grid(row = 3, column = 4, sticky = N+S+W, pady = 10)  

if __name__ == "__main__":
    tk = Tk()
    main = Frame(tk)
    main.pack()
    scroll = Scrollbar(main)
    scroll2 = Scrollbar(main, orient = HORIZONTAL)
    text = Entry(main, xscrollcommand = scroll2.set)
    finalresult = Listbox(main, yscrollcommand = scroll.set, selectmode = SINGLE)
    entry = Entry(main)
    entry2 = Entry(main)
    entry3 = Entry(main)
    entry2.grid(row = 1, column = 3, columnspan = 2)
    entry.grid(row = 0, column = 3, columnspan = 2)
    entry.insert(0, "*")
    entry.selection_range(0, END)
    savebutton = Button(main, text = "Gravar", command = gravar)
    Button(main, text="Sair", command=main.quit).grid(row = 2, column = 2, padx = 5, pady = 10, columnspan = 2)
    Button(main, text="Procurar", command=search).grid(row = 2, column = 0, padx = 5, pady = 10, columnspan = 2)
    Label(main, text="Ficheiro de log:").grid(row = 0, column = 0, columnspan = 2, pady = 10)
    Label(main, text="Palavra que deseja:").grid(row = 1, column = 0, columnspan = 2)
    finalresult.bind("<Double-Button-1>", editbox)
    scroll.config(command = finalresult.yview)
    scroll2.config(command = scrolling)
    tk.mainloop()

"A computer program it's a world, the programmer its God"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.