如何将所选文件名从tkfiledialog GUI传递到另一个函数 [英] How to pass the selected filename from tkfiledialog GUI to another function

查看:114
本文介绍了如何将所选文件名从tkfiledialog GUI传递到另一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Tkinter构建一个简单的桌面应用程序,该应用程序具有一个浏览按钮,供用户从其计算机中选择文件(下面的代码位于gui.py文件中):

I'm building a simple desktop application using Tkinter that has a browse button for the user to be able to select file from their computer (below code is in a file called gui.py):

import Tkinter
import tkFileDialog
import tkMessageBox


class simpleapp_tk(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()

        self.entryVariable = Tkinter.StringVar()

        self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
        self.entry.grid(column=0,row=0,sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable.set(u"Enter text here.")

        button_browse = Tkinter.Button(self, text = u"Browse", command = lambda:self.entryVariable.set(tkFileDialog.askopenfilename()))


        button = Tkinter.Button(self,text=u"Go",
                                 command=self.OnButtonClick)
        button_browse.grid(column=1,row=0)
        button.grid(column=2,row=0)

        self.labelVariable = Tkinter.StringVar()
        label = Tkinter.Label(self,textvariable=self.labelVariable,
                              anchor="w",fg="black",bg="white")
        label.grid(column=0,row=1,columnspan=3,sticky='EW')
        self.labelVariable.set(u"Hello !")

        self.grid_columnconfigure(0,weight=1)
        self.resizable(True,False)
        self.update()
        self.geometry(self.geometry())       
        self.entry.focus_set()
        self.entry.selection_range(0, Tkinter.END)


    def OnButtonClick(self):
        self.labelVariable.set( self.entryVariable.get()+" (You clicked the button)" )
        self.entry.focus_set()
        self.entry.selection_range(0, Tkinter.END)

    def OnPressEnter(self,event):
        self.labelVariable.set( self.entryVariable.get()+" (You pressed ENTER)" )
        self.entry.focus_set()
        self.entry.selection_range(0, Tkinter.END)

if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.title('my application')
    app.mainloop()

现在,当用户单击"Go"按钮时,我希望将选定的文件名传递给以下函数(在类外部),以代替"filename"变量和该程序的输出应该在gui中的"labelvariable"中返回:

Now, when the user clicks on the button "Go", I want the selected filename to be passed to the following function (which is outside the class), in place of the "filename" variable and the output of this program should be returned in the gui in the "labelvariable":

def main():
    data=[]
    total_top5=[]
    book = xlrd.open_workbook(filename)
    sheet = book.sheet_by_index(0)
    for row_index in xrange(1, sheet.nrows): # skip heading row
        text = sheet.row_values(row_index, end_colx=1)   
        data.append(text)

    #data = unicode(x).encode('UTF8') for x in data
    new_data=[]
    for x in data:    
        new_data.append(unicode(x[0]).encode('UTF8'))

我以前从未使用过Tkinter或在python中创建GUI,因此请提供任何帮助.

I have never worked with Tkinter or creating GUI in python before, so any kind of help will be appreciated please.

推荐答案

我认为你的意思

这是一种解决方案,可以解决我假设您希望将所选文件的名称传递给predict.py时的意思.

What i think you meant

This is a solution for what I assume that you mean when you say that you want the name of the selected file passed to predict.py

在predict.py内进行这些更改;

Inside predict.py, make these changes;

首先导入GUI

from gui import simpleapp_tk

第二,使主函数传递一个参数

Second, make the main function be passed an argument

def main(filename="test.foo"):

最后,在您的运行例程中,实例化GUI,获取文件名,然后运行主要功能

Lastly, in your run routine, instantiate the GUI, get the filename, and run the main function

if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.title('my application')
    app.mainloop()
    # Here it will wait until the GUI finishes and exits
    filename = app.labelVariable.get()
    main(filename)

还有什么可以做的

您可以从gui.py导入预报.py并更改开始"按钮的方法调用.

What also can be done

You could from the gui.py import predict.py and change the method call of the "Go" button.

如果将button_browse设置为simpleapp_tk的属性,则可以轻松地从外部设置其命令(如果您将GUI导入其他地方),则这是在predict.py内部

If you make button_browse an attribute of simpleapp_tk, you could easily set the command of it externally if you would import the GUI elsewhere, this is inside predict.py

if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.title('my application')
    app.configure(command=main(tkFileDialog.askopenfilename()))
    app.mainloop()

这篇关于如何将所选文件名从tkfiledialog GUI传递到另一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆