问题
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()
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'))
I have never worked with Tkinter or creating GUI in python before, so any kind of help will be appreciated please.
回答1:
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
Inside predict.py, make these changes;
First import the GUI
from gui import simpleapp_tk
Second, make the main function be passed an argument
def main(filename="test.foo"):
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)
What also can be done
You could from the gui.py import predict.py and change the method call of the "Go" button.
or,
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()
来源:https://stackoverflow.com/questions/44012740/how-to-pass-the-selected-filename-from-tkfiledialog-gui-to-another-function