[Tutor] Frames and the grid manager
Klas Marteleur
klas.martelleur at telia.com
Sat Aug 21 15:47:23 CEST 2004
Hi
I have desigend a small gui for my convert program. I have used the grid
manager and placed everything in the master frame. I now plan to expand my
program a bit more to make it possible to convert a few other types of files.
I then think it would be best to separate my gui into frames.
Alan has examples of frames and the pack manager in his book but do i design
frames when i am using the grid manager?
If someone could ilustrate that for me for example by having the input section
of my program as a separate frame inside the master frame, i would be very
greatful. (please also take a look at my way of exiting the program... seems
like its something wrong with it, i dont get a clean exit)
Here is my program
#!/usr/bin/python
####################################################################
##Module: createEbomGUI
##Created: Klas Marteleur, 2004, 08, 15
##
##Function:
##GUI for createEbom2
##Changelog:
## version 0.1:
## Initial Beta release
## version 0.11:
## Path fixes
## version 0.2:
## def modifyList completly rewritten because of changes in inputfile
##
####################################################################
from Tkinter import *
import tkFileDialog, sys
class CreateEbom(Frame):
def __init__(self, master):
# Create master frame
self.myMaster = master
self.myMaster.title('Tragic Converter BETA 0.2')
self.myMaster.geometry('+450+400')
self.myContainer1 = Grid()
# Create input file label
self.inputFile = Label(master, text="Input file:")
self.inputFile.grid(row=0, sticky=E)
# Create output file label
self.outputFile = Label(master,text="Output file:")
self.outputFile.grid(row=1, sticky=E)
# Create inputfile entry box
#self.infile = "C:\users\default\input.1"
self.infile =
"/home/klas/Python_filer/Tragic/ebom_from_file_wider.txt"
self.inputEntry = Entry(master)
self.inputEntry.insert(INSERT,self.infile)
self.inputEntry.grid(row=0,column=1)
# Create outputfile entry box
#self.outfile = "C:\CreateEbom\output.txt"
self.outfile = "/home/klas/Python_filer/Tragic/output.txt"
self.outputEntry = Entry(master)
self.outputEntry.insert(INSERT,self.outfile)
self.outputEntry.grid(row=1,column=1)
# Create input browse button
self.inputBrowseButton = Button(master, command = self.openInFile)
self.inputBrowseButton.configure(text = "Browse")
self.inputBrowseButton.grid(row=0,column=2)
# Create output browse button
self.outputBrowseButton = Button(master, command = self.saveAsFile)
self.outputBrowseButton.configure(text = "Browse")
self.outputBrowseButton.grid(row=1,column=2)
# Create latest revision label
self.latestRevisonLabel = Label(master,text="Use rev.")
self.latestRevisonLabel.grid(row=2, sticky=E)
# Create latest revision check button
self.var = IntVar()
self.latestRevisionCheckbutton = Checkbutton(master,variable=self.var,
command=self.checkButton)
self.latestRevisionCheckbutton.grid(row=2,column=1,sticky=W)
# Create convert button
self.convertButton = Button(master, command = self.doProcessFiles)
self.convertButton.configure(text="Convert")
self.convertButton.grid(row=3,column=1,sticky=E)
# Create exit button
self.exitButton = Button(master, command = self.doExit)
self.exitButton.configure(text="Exit")
self.exitButton.grid(row=3,column=2,sticky=E)
# Create designed by label
self.designedByLabel = Label(master,text="This program is designed in
Python by Klas Marteleur. Tel. 21116",font=("Arial", 7), fg="red")
self.designedByLabel.grid(row=4,column=0,columnspan=3,sticky=S)
# Add Python logo
photo =
PhotoImage(file="/home/klas/Python_filer/Tragic/PythonPoweredSmall.gif")
self.logo = Label(master,image=photo)
self.logo.photo = photo
self.logo .grid(row=3,column=0)
# Temporary function
def callback(self):
print "Called the callback!"
# Check button function
def checkButton(self):
print "\n"*50
print """The value of the "Use rev." button is: """, self.var.get()
if self.var.get() == 0:
print "\nThis means that the LATEST revison of each item shall be
used in Magic.\nThis is the NORMAL most used setting"
else:
print "\nThis means that the revision of each item used in in
Magic,\nshall be as specified in the drawing table\nUSE WITH CARE"
# Open file function
def openInFile(self):
infile =
tkFileDialog.askopenfilename(initialdir="D:\users\default",filetypes=[("ProE
output files",".1"),("All files","*")])
self.inputEntry.delete(0,END)
self.inputEntry.insert(INSERT,infile)
print "\n"*50
print "The infile is now specified to: ", infile
# Save as function
def saveAsFile(self):
outfile = tkFileDialog.asksaveasfilename(initialdir="C:\Create Ebom
From File",defaultextension=".txt",filetypes=[("Text files",".txt"),("All
files","*")])
self.outputEntry.delete(0,END)
self.outputEntry.insert(INSERT, outfile)
print "\n"*50
print "The outfile is now specified to: ", outfile
# Process files function
def doProcessFiles(self):
from createEbomNew import *
from tkMessageBox import *
inputFileName = self.inputEntry.get()
outputFileName = self.outputEntry.get()
print "\n"*50
print "Inputfile = ", inputFileName,
print "Outputfile = ", outputFileName
latestRevision = self.var.get()
processFilesEbom(inputFileName,outputFileName,latestRevision)
print "+---------------------+"
print "| CONVERTING COMPLETE |"
print "+---------------------+"
showinfo("Convert Complete", "Convert Complete\n\n Program Exits")
self.myMaster.destroy()
sys.exit()
# Exit function
def doExit(self):
self.myMaster.destroy()
sys.exit()
print "\n"*100 # a simple way to clear the screen
print "Starting..."
root = Tk()
createebom = CreateEbom(root)
root.mainloop()
print "Shutdown in progress..."
print "Done!"
Thanks
Klas
More information about the Tutor
mailing list