2010-02-16 101 views
1
from Tkinter import * 
import socket, sys, os 
import tkMessageBox 

root = Tk() 
root.title("File Deleter v1.0") 
root.config(bg='black') 
root.resizable(0, 0) 

text = Text() 
text3 = Text() 

frame = Frame(root) 
frame.config(bg="black") 
frame.pack(pady=10, padx=5) 

frame1 = Frame(root) 
frame1.config(bg="black") 
frame1.pack(pady=10, padx=5) 


text.config(width=35, height=1, bg="black", fg="white") 
text.pack(padx=5) 

def button1(): 
    try: 
     x = text.get("1.0", END) 
     os.remove(x) 
    except WindowsError: 
     text3.insert(END, "File Not Found... Try Again\n")  

def clear(): 
    text.delete("1.0", END) 

c = Button(frame1, text="Clear", width=10, height=2, command=clear) 
c.config(fg="white", bg="black") 
c.pack(side=LEFT, padx=5) 

scrollbar = Scrollbar(root) 
scrollbar.pack(side=RIGHT, fill=Y) 
text3.config(width=35, height=15, bg="black", fg="white") 
text3.pack(side=LEFT, fill=Y) 
scrollbar.config(command=text3.yview) 
text3.config(yscrollcommand=scrollbar.set) 

w = Label(frame, text="Delete A File") 
w.config(bg='black', fg='white') 
w.pack(side=TOP, padx=5) 


b = Button(frame1, text="Enter", width=10, height=2, command=button1) 
b.config(fg="white", bg="black") 
b.pack(side=LEFT, padx=5) 

root.mainloop() 

我不明白为什么删除代码不工作,即使文件存在,我也会得到“文件未找到”。删除文件

+0

“文件未找到”几乎不是删除文件时可能出错的唯一的东西。异常实际上报告了什么? (看看'strerror'场。) – badp 2010-02-16 23:46:40

回答

0

我相信gnibbler的轨道上与空白是问题。文本小工具为您提供了最终的字符\ n。尝试添加.strip()到您的text.get结束,或者你可以使用输入窗口小部件,因为你的文本小部件,而不是一个文本控件只有一行anways。

x = text.get('1.0', END).strip() 
0

或许x是不是你想的是,只是一种猜测,但也许有一个一些空白那里或财产以后,试试这个检查

def button1(): 
    try: 
     x = text.get("1.0", END) 
     print repr(x) 
     os.remove(x) 
    except WindowsError, e: 
     print e 
     text3.insert(END, "File Not Found... Try Again\n") 
3

当我在Linux上运行此代码,并放置一个断点button1(),我看到的x的值包括一个尾随换行符。这意味着os.remove()通话将无法工作,因为我输入文件名并没有实际包含一个换行符。如果我删除尾随的换行符,代码将起作用。

+0

正确的:文本组件是保证始终有一个结尾的新行。原始代码应该是“x = text.get(”1.0“,”end-1c“)” – 2010-02-17 12:04:18