7

我正在编写一些代码,用户需要能够选择程序将在其上运行的文件。我创建了一个浏览按钮,允许用户选择一个文件,但是当你点击“好的”时,程序的其余部分并没有意识到有输入。选择文件后,还应在浏览栏中自动输入文件名。有什么建议么?

from Tkinter import *

class Window:       

def __init__(self, master):     

    #Browse Bar
    csvfile=Label(root, text="File").grid(row=1, column=0)
    bar=Entry(master).grid(row=1, column=1) 

    #Buttons  
    y=7
    self.cbutton= Button(root, text="OK", command=master.destroy)       #closes window
    y+=1
    self.cbutton.grid(row=10, column=3, sticky = W + E)
    self.bbutton= Button(root, text="Browse", command=self.browsecsv)
    self.bbutton.grid(row=1, column=3)

#-------------------------------------------------------------------------------------#
def browsecsv(self):
    from tkFileDialog import askopenfilename

    Tk().withdraw() 
    filename = askopenfilename()

#-------------------------------------------------------------------------------------#
import csv

with open('filename', 'rb') as csvfile:
    logreader = csv.reader(csvfile, delimiter=',', quotechar='|')
    rownum=0

    for row in logreader:    
        NumColumns = len(row)        
        rownum += 1

    Matrix = [[0 for x in xrange(NumColumns)] for x in xrange(rownum)] 

csvfile.close()


root = Tk()
window=Window(root)
root.mainloop()  
4

5 回答 5

7

你也可以使用 tkFileDialog..

import Tkinter,tkFileDialog

root = Tkinter.Tk()
file = tkFileDialog.askopenfile(parent=root,mode='rb',title='Choose a file')
if file:
    data = file.read()
    file.close()
    print "I got %d bytes from this file." % len(data)
于 2013-05-28T19:08:54.010 回答
4

filename = askopenfilename()仅在此范围内已知,您必须返回它或以任何方式使用它。

有关更多示例,请参见此站点:

    Tkinter.Button(self, text='Browse', command=self.askopenfile)

...

    def askopenfile(self):
        return tkFileDialog.askopenfile(mode='r', **self.file_opt)

编辑

布莱恩·奥克利当然是对的!当我说“以任何方式使用它”时,这就是我的意思;)有一次你选择一个文件名,另一方面你只需使用filename.

这个怎么样?

from Tkinter import *
import csv

class Window:       
def __init__(self, master):     
    self.filename=""
    csvfile=Label(root, text="File").grid(row=1, column=0)
    bar=Entry(master).grid(row=1, column=1) 

    #Buttons  
    y=7
    self.cbutton= Button(root, text="OK", command=self.process_csv)
    y+=1
    self.cbutton.grid(row=10, column=3, sticky = W + E)
    self.bbutton= Button(root, text="Browse", command=self.browsecsv)
    self.bbutton.grid(row=1, column=3)

def browsecsv(self):
    from tkFileDialog import askopenfilename

    Tk().withdraw() 
    self.filename = askopenfilename()

def process_csv(self):
    if self.filename:
        with open(self.filename, 'rb') as csvfile:
            logreader = csv.reader(csvfile, delimiter=',', quotechar='|')
            rownum=0

            for row in logreader:    
                NumColumns = len(row)        
                rownum += 1

            Matrix = [[0 for x in xrange(NumColumns)] for x in xrange(rownum)] 

root = Tk()
window=Window(root)
root.mainloop()  

还有很多事情要做,但至少在确定文件名之前不要尝试打开文件。

于 2013-05-28T18:40:11.397 回答
3
# importing tkinter and tkinter.ttk 
# and all their functions and classes 
from tkinter import * 
from tkinter.ttk import *

# importing askopenfile function 
# from class filedialog 
from tkinter.filedialog import askopenfile 

root = Tk() 
root.geometry('200x100') 

# This function will be used to open 
# file in read mode and only Python files 
# will be opened 
def open_file(): 
    file = askopenfile(mode ='r', filetypes =[('Python Files', '*.docx')]) 
    if file is not None: 
        content = file.read() 
        print(content) 

btn = Button(root, text ='Open', command = lambda:open_file()) 
btn.pack(side = TOP, pady = 10) 

mainloop() 
于 2020-02-03T17:58:51.933 回答
2

问题的根源是您试图在用户有机会选择文件之前处理文件。

您需要将代码块with open('filename', 'rb') as csvfile:以函数开头,然后在用户按下按钮时调用该函数。例如,您可以从browsecsv函数中调用它。

此外,您不需要csv.close(), 使用with语句时免费提供的。

于 2013-05-28T19:34:29.013 回答
0

我已经编辑了上面的代码以在 python 3.6 中使用。仅包名称更改

    import tkinter
    from tkinter import filedialog
    file = filedialog.askopenfile(parent=root,mode='rb',title='Choose a file')
    if file != None:
        data = file.read()
        file.close()
        print("I got %d bytes from this file." % len(data))
于 2019-03-04T06:02:54.553 回答