使用TKinter创建浏览按钮

时间:2013-05-28 18:28:43

标签: python tkinter

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

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()  

5 个答案:

答案 0 :(得分:6)

你也可以使用tkFileDialog ..

import Tkinter,tkFileDialog

root = Tkinter.Tk()
file = tkFileDialog.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)

答案 1 :(得分:4)

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

有关更多示例,请参阅this site

    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()  

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

答案 2 :(得分:2)

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

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

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

答案 3 :(得分:2)

# 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() 

答案 4 :(得分: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))