Hi, I'm having some problems with opening files in python using Tkinter. What I want to make is a GUI where the user can select two text documents and then merge them into one and save it as a new file. My proof of concept for right now would be to open the file and write lines from it to the GUI when I press the run button. Right now a file dialog will open and I can select a file but I'm confused as to what or where my askopenfile function is returning. (I copied that part of the code from the tkinter documentation webpage) Any help would be appreciated. I've never programed in python before I'm usually working with C/C++ command line programs.
Here's my code so far. It should work with python 2.7 and 3.4:
try:
from tkinter import *
import tkinter.messagebox as tkinterMessagebox
import tkinter.constants as tkinterConstants
import tkinter.filedialog as tkinterFiledialog
except ImportError:
import Tkinter as tkinter
import Tkconstants as tkinterConstants
import tkMessageBox as tkinterMessagebox
import tkFileDialog as tkinterFiledialog
class MyGuiProgram(tkinter.Frame):
def __init__(self, root):
tkinter.Frame.__init__(self, root)
# define buttons
tkinter.Button(self, text='File 1', command=self.askopenfile).pack()
tkinter.Button(self, text='Run', command=self.mergeFiles).pack()
def askopenfile(self):
"""Returns an opened file in read mode."""
return tkinterFiledialog.askopenfile(mode='r')
def mergeFiles(self):
#For testing: Check that the file will open and print each line to gui.
##line = ??.readlines()
##tkinter.Label(self, text=line).pack()
#How I want to merge files will go here
tkinterMessagebox.showinfo( "Finished", "Finished")
if __name__=='__main__':
root = tkinter.Tk()
root.minsize(200,100)
MyGuiProgram(root).pack()
root.mainloop()