2015-02-01 3 views
2

나는 휘발성 프레임 워크를위한 GUI 기반을위한 파이썬 프로그램을 작성 중이다. 파이썬 용 초보자인데 아직 초기 단계이다. "askopnefilename"에서 파일 가져 오기를 사용하고있다. 파일을 선택하지 않고 닫으십시오. 그러면 해결할 수 있도록 도와주세요.Python3 askopenfilename 오류

코드 :

from tkinter import * 
from tkinter import filedialog 
from tkinter.filedialog import askopenfilename 
from mtTkinter import * 
import subprocess 


def donothing(): 
    filewin = Toplevel(root) 
    button = Button(filewin, text="Do nothing button") 
    button.pack() 

def OpenFile(): 
    file = open(askopenfilename(),'r') 
    print(root.file) 

def Quit(): 
    root.destroy() 

def Test(): 
    print("Hello world") 
    input("Press return to exit") 

def Shell(): 
# print("Below is the output") 
    subprocess.call('./home/shanaka/bash_lerning/function1.sh',shell=True) 


root = Tk() 
root.title("Volatility") 
root.geometry("600x400") 

menubar = Menu(root) 
startmenu = Menu(menubar, tearoff=0) 
startmenu.add_command(label="Import", command=OpenFile) 
startmenu.add_separator() 
startmenu.add_command(label="Exit", command=Quit) 
menubar.add_cascade(label="Start", menu=startmenu) 

searchmenu = Menu(menubar, tearoff=0) 

submenu = Menu(searchmenu) 
submenu.add_command(label="New feed", command=Shell) 
submenu.add_command(label="Bookmarks") 
submenu.add_command(label="Mail") 
searchmenu.add_cascade(label='Plugins', menu=submenu, underline=0) 

searchmenu.add_separator() 
#searchmenu.add_command(label="Plugins", command=Test) 
searchmenu.add_command(label="Copy", command=donothing) 
searchmenu.add_command(label="Paste", command=donothing) 
searchmenu.add_command(label="Delete", command=donothing) 
searchmenu.add_command(label="Select All", command=donothing) 
menubar.add_cascade(label="Search", menu=searchmenu) 

reportmenu = Menu(menubar, tearoff=0) 
reportmenu.add_command(label="Generate Reports", command=donothing) 
menubar.add_cascade(label="Reports", menu=reportmenu) 


helpmenu = Menu(menubar, tearoff=0) 
helpmenu.add_command(label="Help Index", command=donothing) 
helpmenu.add_command(label="About...", command=donothing) 
menubar.add_cascade(label="Help", menu=helpmenu) 

root.config(menu=menubar) 
root.mainloop() 

오류로

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "/usr/lib/python3.4/tkinter/__init__.py", line 1490, in __call__ 
    return self.func(*args) 
    File "/home/shanaka/Project1.py", line 13, in OpenFile 
    file = open(askopenfilename(),'r') 
FileNotFoundError: [Errno 2] No such file or directory: '' 

답변

4

파일을 열고 아래에만 비 falsy 값 askopenfilename 반환하는 경우 :

def OpenFile(): 
    filepath = askopenfilename() 
    if filepath: 
     file = open(filepath, 'r') 
     ... 

BTW 코드하지 않았다 할당 된 root.file; root.file에 액세스하면 AttributeError가됩니다.

+0

감사합니다. 작동 중입니다. – SLS

+0

@SLS, Welcome to Stack Overflow! 이것이 도움이된다면, [답변 수락] (http://meta.stackoverflow.com/a/5235)을 통해 커뮤니티에 알릴 수 있습니다. – falsetru