Difference between revisions of "CSC111 Programs for Week 13 2015"

From dftwiki3
Jump to: navigation, search
(Synthetic Email Addresses)
Line 4: Line 4:
 
=TKDemo0.py=
 
=TKDemo0.py=
 
<br />
 
<br />
<source lang="python">
+
::<source lang="python">
 
# tkdemo0.py
 
# tkdemo0.py
 
# D. Thiebaut
 
# D. Thiebaut
Line 22: Line 22:
 
=TKDemo1.py=
 
=TKDemo1.py=
 
<br />
 
<br />
<source lang="python">
+
::<source lang="python">
 
# tkdemo1.py
 
# tkdemo1.py
 
# D. Thiebaut
 
# D. Thiebaut
Line 84: Line 84:
 
* This version allows the user to pick a file in the directory the user navigates too, and then opens the file, reads its contents, and puts it inside the text area.
 
* This version allows the user to pick a file in the directory the user navigates too, and then opens the file, reads its contents, and puts it inside the text area.
 
<br />
 
<br />
<source lang="python">
+
::<source lang="python">
 
# tkWindow.py
 
# tkWindow.py
 
# D. Thiebaut
 
# D. Thiebaut

Revision as of 11:35, 29 April 2015

--D. Thiebaut (talk) 22:40, 25 April 2015 (EDT)


TKDemo0.py


# tkdemo0.py
# D. Thiebaut
# The very basic skeleton of a TKinter program
from tkinter import *
from tkinter import ttk

def main():
    rootWindow = Tk()
    rootWindow.mainloop()


main()


TKDemo1.py


# tkdemo1.py
# D. Thiebaut
# A Simple TKInter window with a few widgets.  Code needs to be added
# to make this functional.

from tkinter import *
from tkinter import ttk

class GUI:

    def __init__( self, root ):
        # create a button that will clear the text area
        button1 = ttk.Button( root, text="Clear", command = self.clear )
        button1.grid( row=0, column=0 )

        # create another button that will process the text inside the text area
        button2 = ttk.Button( root, text="Process", command = self.process )
        button2.grid( row=0, column=1 )

        # set a check box for selecting Smith email addresses
        self.showSmith = IntVar()
        self.showSmith.set( 1 )      # make the box checked by default
        check1 = ttk.Checkbutton( root, text="Show Smith", variable=self.showSmith )
        check1.grid( row=0, column=2 )

        # set a check box for selecting Hampshire email addresses
        self.showHampshire = IntVar()
        self.showHampshire.set( 0 )  # make the box unchecked by default
        check2 = ttk.Checkbutton( root, text="Show Hampshire", variable=self.showHampshire )
        check2.grid( row=0, column=3 )
        
    def clear( self ):
        # this will eventually clear the text area
        print( "clear text area" )
        return

    def process( self ):
        # this will eventually process the list of email addresses and sort it
        # and remove duplicates.
        print( "process email addresses" )
        return
    
def main():
    # create an empty window
    rootWindow = Tk()
    rootWindow.title( "CSC111 TK Demo" )

    # create the GUI inside the window
    gui = GUI( rootWindow)

    # main event loop
    rootWindow.mainloop()


main()


TkDemo Full Implementation


  • This version allows the user to pick a file in the directory the user navigates too, and then opens the file, reads its contents, and puts it inside the text area.


# tkWindow.py
# D. Thiebaut
# Illustrates how to create a GUI application
# using TKInter.
#
from tkinter import *
from tkinter import ttk
import tkinter.filedialog as fdialog # this is used to find a file in the user's file system

class GUI:
    """A class containing all the widgets and the actions they trigger"""

    def __init__( self, root ):
        """constructor.  Creates all the widgets, and define the actions to be taken"""
        self.root = root
        
        # define button to clear the textArea
        button1 = ttk.Button( root, text="Clear", command = self.clear )
        button1.grid( row=0, column=0 )

        # define button to process textArea
        button2 = ttk.Button( root, text="Process", command = self.process )
        button2.grid( row=0, column=1 )

        # set a check box for Smith
        self.showSmith = IntVar()
        self.showSmith.set( 1 )
        check1 = ttk.Checkbutton( root, text="Show Smith", variable=self.showSmith )
        check1.grid( row=0, column=2 )

        # set a check box for Hampshire
        self.showHampshire = IntVar()
        self.showHampshire.set( 0 )
        check2 = ttk.Checkbutton( root, text="Show Hampshire", variable=self.showHampshire )
        check2.grid( row=0, column=3 )

        # add an "Open File" button
        button3 = ttk.Button( root, text="Open File", command = self.openFile )
        button3.grid( row=0, column=4 )
        
        # create the text area
        self.text = Text( root, width=80, height=30, background="ivory" )
        self.text.grid( row=1, column=0, columnspan=5 )


    def openFile( self ):
        """ opens a dialog for the user to pick a file, then tries to open the file, then
        reads the file and displays its contents on the text area"""
        filename = fdialog.askopenfilename(filetypes = (("Text files", "*.txt"),
                                                         ("All files", "*.*")))
        text = ""
        try:
            file = open( filename, "r" )
            text = file.read()
            file.close()            
        except:
            # if there is an exception, i.e. file not found, then we return
            return
        
        # if we're here, it means we have some text read from the file. 
        # display it in the text area.
        self.clear()
        self.text.insert( INSERT, text )

        
    def clear( self ):
        """clear the text area"""
        self.text.delete(1.0, END) 

    def process( self ):
        """process the email addresses"""
        # get the text from the text area.
        emails = self.text.get(1.0, END)
        self.clear()

        # split the string into lines, skip over invalid lines, split
        # valid lines into 3 fields, and keep the last field.
        # remove the duplicates, and sort the list.
        emailList = []
        for line in emails.split( "\n" ):
            if len( line.strip() ) <= 1:
                continue
            words = line.split( "," )
            if len( words ) != 3:
                continue

            email = words[-1]

            # keep email only if  user wants Smith and email contains "@smith"
            if email.find( "@smith" ) != -1 and self.showSmith.get()==1:
                   emailList.append( email )

            # keep email only if user wants Hampshire and email contains "@hamp"
            if email.find( "@hamp" ) != -1 and self.showHampshire.get()==1:
                   emailList.append( email )

        # remove duplicates and sort
        emailList = list( set( emailList ) )
        emailList.sort()

        # put list joined by "\n" into the text area        
        self.text.insert( INSERT, "\n".join( emailList ) )


def main():
    rootWindow = Tk()
    rootWindow.title( "CSC111 TK Demo" )

    
    gui = GUI( rootWindow)

    rootWindow.mainloop()


main()


Synthetic Email Addresses


  • These are not real addresses. Just random first names made into what looks like real email addresses, formatted as CSV.
1, Anglea, Anglea@hampshire.edu
2, Chun, Chun@smith.edu
3, Codi, Codi@smith.edu
4, Cordelia, Cordelia@smith.edu
5, Cythia, Cythia@smith.edu
6, Evalyn, Evalyn@smith.edu
7, Josie, Josie@smith.edu
8, Karena, Karena@hampshire.edu
9, Lanell, Lanell@smith.edu
10, Lisbeth, Lisbeth@smith.edu
11, Love, Love@smith.edu
12, Natacha, Natacha@smith.edu
13, Romana, Romana@smith.edu
14, Traci, Traci@smith.edu
15, Whitley, Whitley@smith.edu

1, Aja, Aja@hampshire.edu
2, Anna, Anna@hampshire.edu
3, Constance, Constance@smith.edu
4, Jeneva, Jeneva@smith.edu
5, Kaylee, Kaylee@hampshire.edu
6, Leilani, Leilani@smith.edu
7, Margaretta, Margaretta@smith.edu
8, Marline, Marline@smith.edu
9, Matha, Matha@smith.edu
10, Natacha, Natacha@smith.edu
11, Phuong, Phuong@hampshire.edu
12, Sanjuana, Sanjuana@smith.edu
13, Sharie, Sharie@smith.edu
14, Tashina, Tashina@smith.edu

1, Anna, Anna@hampshire.edu
2, Bessie, Bessie@smith.edu
3, Chun, Chun@smith.edu
4, Constance, Constance@smith.edu
5, Edith, Edith@smith.edu
6, Elfrieda, Elfrieda@smith.edu
7, Gema, Gema@smith.edu
8, Johanna, Johanna@smith.edu
9, Margaretta, Margaretta@smith.edu
10, Maryellen, Maryellen@smith.edu
11, Matha, Matha@smith.edu
12, Niesha, Niesha@smith.edu
13, Patrica, Patrica@hampshire.edu
14, Traci, Traci@smith.edu

1, Ammie, Ammie@hampshire.edu
2, Ayana, Ayana@hampshire.edu
3, Birdie, Birdie@smith.edu
4, Candyce, Candyce@smith.edu
5, Constance, Constance@smith.edu
6, Edelmira, Edelmira@smith.edu
7, Erlinda, Erlinda@smith.edu
8, Kali, Kali@hampshire.edu
9, Lisbeth, Lisbeth@smith.edu
10, Mallie, Mallie@smith.edu
11, Margaretta, Margaretta@smith.edu
12, Marlene, Marlene@smith.edu
13, Marline, Marline@smith.edu
14, May, May@smith.edu
15, Nola, Nola@smith.edu
16, Rachael, Rachael@smith.edu
17, Renata, Renata@smith.edu
18, Tamela, Tamela@smith.edu
19, Tashina, Tashina@smith.edu

1, Ammie, Ammie@hampshire.edu
2, Anna, Anna@hampshire.edu
3, Candyce, Candyce@smith.edu
4, Clorinda, Clorinda@smith.edu
5, Dorathy, Dorathy@smith.edu
6, Jennine, Jennine@smith.edu
7, Johanna, Johanna@smith.edu
8, Kandace, Kandace@hampshire.edu
9, Niesha, Niesha@smith.edu
10, Patrica, Patrica@hampshire.edu
11, Rosalina, Rosalina@smith.edu
12, Sharie, Sharie@smith.edu
13, Suk, Suk@smith.edu
14, Yuko, Yuko@smith.edu