1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
   | import Tkinter, Tkconstants, tkFileDialog
 
class TkFileDialogExample(Tkinter.Frame):
 
	def __init__(self, root):
 
		Tkinter.Frame.__init__(self, root)
 
		# define buttons
		Tkinter.Label(self, text="Input file").grid(row=0, column=0, sticky='W')
		input_tb = Tkinter.Entry(self, width=20)
		input_tb.grid(row=0, column=1)
		Tkinter.Button(self, text='browse', command=self.askopenfilename, width=3).grid(row=0, column=2)
 
		Tkinter.Label(self, text="Output file").grid(row=1, column=0, sticky='W')
		output_tb = Tkinter.Entry(self, width=20)
		output_tb.grid(row=1, column=1)
		Tkinter.Button(self, text='browse', command=self.asksaveasfilename, width=3).grid(row=1, column=2)
 
		Tkinter.Button(self, text='send', command=self.send, width=5).grid(row=2, column=1)
 
	def askopenfilename(self):
		self.file_opt = options = {}
		options['filetypes'] = [('all files', '.*'), ('text files', '.txt')]
		return tkFileDialog.askopenfilename(**self.file_opt)
 
	def asksaveasfilename(self):
		self.file_opt = options = {}
		options['filetypes'] = [('all files', '.*'), ('text files', '.txt')]
		options['defaultextension'] = '.txt'
		options['initialfile'] = 'myfile.txt'
		return tkFileDialog.asksaveasfilename(**self.file_opt)
 
	def send(self):
		print 'Opened file: '
		print 'Saved file: '
 
if __name__=='__main__':
	root = Tkinter.Tk()
	TkFileDialogExample(root).pack()
	root.title("Test")
	root.geometry("300x100")
	root.mainloop() | 
Partager