Hi! I just started using Python yesterday and I have written a program that reads in a very large data file and rearranges the data so that it can be read into a different program. I am doing this for some engineering modeling simulations. I wanted to create an interface for it, so I started writing a GUI for Tk.

I do not understand how the even handlers work and I can't seem to hack it out by my internet findings. I would like to create 4 radio buttons (2 groups of 2) which provide the user with some options as well as an openfile and savefile. Then I want a button that based on the input of the other buttons, inserts the file names into my code and then "converts" the file using one of 4 codes based on those inputs. I will paste the code below.

I will also attach the necessary files (a smaller .csv file than I am using, however to save memory)

1). Conversion Code -- ftop.py

# This file is for converting Fluent .csv file to Polyflow.csv file in 3D

import psyco
psyco.profile()

import csv
import sys
import time

print "Running..."
secStart = time.time()

# Adds the header information for Polyflow
saveout = sys.stdout
temp = open("temp.dat","w")
temp.write("# \n")
temp.write("# \n")
temp.write("# \n")
temp.write("# \n")
temp.write("# \n")
temp.write("COORDINATES \n")

# Opens the Fluent .csv file for converting
file = open("quinn.csv","r")
first_line = file.next()
for line in file:	
	v = eval(line)
	x = (v)
	n,x,y,z,t = x
	sys.stdout = temp                                      
	# Prints out the coordinate data	
	print '%8d' % n,";",'%E' % x,";",'%E' % y,";",'%E' % z 		
 	sys.stdout = saveout                                         

# Adds the header information for the Temperatures
temp.write(" \n")
temp.write("TEMPERATURE \n")

# Opens the Fluent .csv file for converting again
file = open("quinn.csv","r")
first_line = file.next()
for line in file:	
	v = eval(line)
	x = (v)
	n,x,y,z,t = x
	sys.stdout = temp                                      
	# Prints out the temperature data	
	print '%8d' % n,";",'%E' % t
	sys.stdout = saveout                                        

temp.close()

print "Elapsed time: %f sec." % (time.time() - secStart)

2). The code I am trying to use to create the GUI-- radio.py

from Tkinter import *
from tkMessageBox import *
from tkColorChooser import askcolor              
from tkFileDialog   import askopenfilename    
from tkFileDialog   import asksaveasfilename   


class gui(Tkinter.tk):

master = Tk()

dim = StringVar()
trans = StringVar()


w = Label(master, text="Please Select Model Dimensions:")
w.pack()
Radiobutton(master, text="2D", variable=dim, value='2D').pack(anchor=W)
Radiobutton(master, text="3D", variable=dim, value='3D').pack(anchor=W)


w = Label(master, text="Please Select Translation Options:")
w.pack()
Radiobutton(master, text="Polyflow to Fluent", variable=trans, value='ptof').pack(anchor=W)
Radiobutton(master, text="Fluent to Polyflow", variable=trans, value='ftop').pack(anchor=W)


# I want this to define the filename currently named "quinn.csv" in the conversion code

def callback():	
    	
	filename = askopenfilename() 
	
Button(text='Select source .csv File', command=callback).pack(fill=X)


# I want this to define the filename currently named "temp.dat" in the conversion code
def saveto():
	destination = asksaveasfilename()
Button(text='Select destination', command=saveto).pack(fill=Y)


# I want this to be an if statement like...
#	if dim == 3D and trans ==ftop3d.py  (i know this is not correct, but I just wanted to give the general idea)
Button(text = "Convert",command = 'exit').pack()


mainloop()

Please help me to understand what I am trying to do! Thanks for your help in advance!

Editor's note:
Please use the [code=python] and [/code] tag pair to enclose your python code.

On your class and Tkinter GUI code, I would recommend that you first get your feet wet on some smaller sample code. As it is now, it makes not much sense.

This article has been dead for over six months. Start a new discussion instead.