【问题标题】:How to pass the selected filename from tkfiledialog GUI to another function如何将选定的文件名从 tkfiledialog GUI 传递给另一个函数
【发布时间】:2017-05-16 22:40:10
【问题描述】:

我正在使用 Tkinter 构建一个简单的桌面应用程序,它有一个浏览按钮,用户可以从他们的计算机中选择文件(下面的代码在一个名为 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" 变量和该程序的输出应在“labelvariable”的 gui 中返回:

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,所以任何形式的帮助都将不胜感激。

【问题讨论】:

  • “传递给”到底是什么意思?你的意思是 gui 应用程序会导入文件并在其中运行一个函数,还是 predict.py 实例化 simpleapp_tk 并使其设置一个变量?
  • @Uzzee 所以,我在上面添加了 GUI 的外观。当一个人单击“浏览”按钮并选择一个文件时,我希望该文件路径进入上面“主”函数中的“文件名”变量。忘记他们在不同的文件中。我现在把它们放在同一个文件中。
  • 好的,我在这方面发布了一个涵盖多个场景的答案,如果您有任何问题,请给我评论!
  • 从不使用 lambda 开始。调试适当的功能要容易得多。

标签: python user-interface tkinter backend


【解决方案1】:

我认为你的意思

这是一个解决方案,当您说要将所选文件的名称传递给 predict.py 时,我假设您的意思是

在 predict.py 中,进行这些更改;

首先导入GUI

from gui import simpleapp_tk

其次,给main函数传一个参数

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

最后,在你的运行例程中,实例化 GUI,获取文件名,然后运行 ​​main 函数

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 导入 predict.py 并更改“Go”按钮的方法调用。

或者,

如果你让 button_browse 成为 simpleapp_tk 的一个属性,你可以很容易地在外部设置它的命令,如果你想在别处导入 GUI,这是在 predict.py 里面

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 2016-07-08
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多