Real World Tkinter GUI

Here is a real world Tkinter GUI that I made for a command line application that translates between MIDI files and Text files. The code follows the screen shot.

# -*- coding: ISO-8859-1 -*-
import sys

if not('.' in sys.path): sys.path.append('.')
import midi24txt 

from Tkinter import *
from tkFileDialog import *

# thinking in tkinter http://www.ferg.org/thinking_in_tkinter/all_programs.html

class TheGui:
    def __init__(self, parent):
        #------- frmSetup ----------#
        self.frmSetup = Frame(parent, bd=5)
        self.frmSetup.pack()
        
        self.inChoices = ('Text', 'Midi')
        self.varRadio = IntVar()
        
        self.r1 = Radiobutton(self.frmSetup, text="Convert Text INPUT into Midi OUTPUT",
            variable=self.varRadio, value=0, command=self.selRadio)
        self.r1.pack(anchor=W)

        self.r2 = Radiobutton(self.frmSetup, text="Convert Midi INPUT into Text OUTPUT", 
            variable=self.varRadio, value=1, command=self.selRadio)
        self.r2.pack(anchor=W)
        #------- frmSetup ----------#

        sep = Frame(parent, width=1, bd=5, bg='black')
        sep.pack(fill=X, expand=1)
         
        #------- frmIn ----------#
        # http://effbot.org/tkinterbook/tkinter-widget-styling.htm
        self.frmIn = Frame(parent, bd=5)         
        self.frmIn.pack()

        self.lblIn = Label(self.frmIn, text='Text Input File Path', width=20)
        self.lblIn.pack(side=LEFT) 

        self.inFilePath = StringVar() # http://effbot.org/tkinterbook/entry.htm
        self.entIn = Entry(self.frmIn, width=20, textvariable=self.inFilePath)
        self.entIn.pack(side=LEFT)
        

        self.btnIn = Button(self.frmIn, text='Browse', command=self.btnInBrowseClick)
        self.btnIn.pack(side=LEFT) 
        #------- frmIn ----------#
        
        
        #------- frmOut ----------#
        self.frmOut = Frame(parent, bd=5)
        self.frmOut.pack()

        self.lblOut = Label(self.frmOut, text='Midi Output File Path', width=20)
        self.lblOut.pack(side=LEFT) 

        self.outFilePath = StringVar()
        self.entOut = Entry(self.frmOut, width=20, textvariable=self.outFilePath)
        self.entOut.pack(side=LEFT) 

        self.btnOut = Button(self.frmOut, text='Browse', command=self.btnOutBrowseClick)
        self.btnOut.pack(side=LEFT) 
        #------- frmOut ----------#
        
        sep = Frame(parent, width=1, bd=5, bg='black')
        sep.pack(fill=X, expand=1)
        
        #------- frmButtons ----------#
        self.frmOut = Frame(parent, bd=5)
        self.frmOut.pack()
        
        self.btnConvert = Button(self.frmOut, 
            text='Convert', command=self.btnConvertClick)
        self.btnConvert.pack() 
  
    #------- handle commands ----------#
    def selRadio(self):
        self.lblIn.config(text = self.inChoices[self.varRadio.get()] 
            + ' Input File Path')
        self.lblOut.config(text = self.inChoices[(self.varRadio.get()+1)%2] 
            + ' Output File Path')
        print str(self.varRadio.get())
   
    def btnInBrowseClick(self):             
        rFilepath = askopenfilename(defaultextension='*', 
            initialdir='.', initialfile='', parent=self.frmIn, title='select a file') 
        self.inFilePath.set(rFilepath)
        print self.entIn.get()
    
    def btnOutBrowseClick(self):  
        rFilepath = asksaveasfilename(defaultextension='*', 
            initialdir='.', initialfile='', parent=self.frmIn, title='select a file') 
        self.outFilePath.set(rFilepath)
        print self.entOut.get()
    
    def btnConvertClick(self):  
        if self.varRadio.get() == 0:
            inputTextFilePath = str(self.inFilePath.get())
            outputMidiFilePath = str(self.outFilePath.get())
            print 'midi 4 txt', inputTextFilePath, outputMidiFilePath
            midi24txt.mid4txt(inputTextFilePath, outputMidiFilePath)
        else:      
            inputMidiFilePath = str(self.inFilePath.get())
            outputTextFilePath = str(self.outFilePath.get())            
            print 'midi 2 txt', inputMidiFilePath, outputTextFilePath
            midi24txt.mid2txt(inputMidiFilePath, outputTextFilePath)
 
  
root = Tk()
root.title("Convert between Midi and Text Files")
#http://infohost.nmt.edu/tcc/help/pubs/tkinter/std-attrs.html#geometry
#http://infohost.nmt.edu/tcc/help/pubs/tkinter/toplevel.html
root.geometry("350x200+10+10")
gui = TheGui(root)
root.mainloop()

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s