LAM Image Annotator Lab Solutions

From PkalTides Wiki
Revision as of 16:38, 27 July 2015 by Thiebaut (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--Thiebaut (talk) 15:48, 27 July 2015 (EDT)



Back to the Liberal Arts Module page

Solution Programs


2 Buttons, 1 Label, Pack


from tkinter import *
from tkinter import ttk
 
label = None # this variable will hold the label created by the GUI, and will be accessible
                    # by the change1() function.
 
def bye():
    global label
    label.config( text = "Goodbye World!" )
 
def hello():
    global label
    label.config( text = "Hello World!" )
 
def main():
    global label
    rootWindow = Tk()
 
    label = ttk.Label( rootWindow, text="Hello World!" )
    label.pack()
 
    button1 = ttk.Button( rootWindow, text="Hello", command=hello )
    button1.pack()
 
    button2 = ttk.Button( rootWindow, text="Bye", command=bye )
    button2.pack()
 
    rootWindow.mainloop()
 
 
main()


2 Buttons, 1 Label, Grid


from tkinter import *
from tkinter import ttk
 
label = None # this variable will hold the label created by the GUI, and will be accessible
                    # by the change1() function.
 
def bye():
    global label
    label.config( text = "Goodbye World!" )
 
def hello():
    global label
    label.config( text = "Hello World!" )
 
def main():
    global label
    rootWindow = Tk()
 
    label = ttk.Label( rootWindow, text="Hello World!" )
    label.grid( row=0, column=0 )
 
    button1 = ttk.Button( rootWindow, text="Hello", command=hello )
    button1.grid( row=0, column=1 )
 
    button2 = ttk.Button( rootWindow, text="Bye", command=bye )
    button2.grid( row=0, column=2 )
 
    rootWindow.mainloop()
 
 
main()


Enable CheckBox


from tkinter import *
from tkinter import ttk
 
class GUI:
    def __init__( self, rootWindow ):
        self.label = ttk.Label( rootWindow, text="Hello World!" )
        self.label.grid( row=0, column=0 )
 
        self.button1 = ttk.Button( rootWindow, text="Hello",
                                   command=self.hello )
        self.button1.grid( row=0, column=1 )
 
        self.button2 = ttk.Button( rootWindow, text="Bye",
                                   command=self.bye )
        self.button2.grid( row=0, column=2 )
 
        # create an object to keep the value of the checkbox
        self.buttonsEnabled = IntVar()
        self.buttonsEnabled.set( 1 )
 
        # create a checkbox and associate it with the object above.
        self.check1 = ttk.Checkbutton( rootWindow, text="Enable", variable=self.buttonsEnabled )
        self.check1.grid( row=1, column=0 )
 
    def bye( self ):
        if self.buttonsEnabled.get()==1:
            self.label.config( text = "Goodbye World!" )
        #print( "buttonsEnable = ", self.buttonsEnabled.get() )
 
    def hello( self ):
        if self.buttonsEnabled.get()==1:
            self.label.config( text = "Hello World!" )
        #self.buttonsEnabled.set( 1-self.buttonsEnabled.get() )
 
def main():
    global label
    rootWindow = Tk()
 
    gui = GUI( rootWindow )
    rootWindow.mainloop()
 
 
main()


Clear Text Area


from tkinter import *
from tkinter import ttk
 
class GUI:
    def __init__( self, rootWindow ):
        self.label = ttk.Label( rootWindow, text="Hello World!" )
        self.label.grid( row=0, column=0 )
 
        self.button1 = ttk.Button( rootWindow, text="Hello",
                                   command=self.hello )
        self.button1.grid( row=0, column=1 )
 
        self.button2 = ttk.Button( rootWindow, text="Clear",
                                   command=self.clear )
        self.button2.grid( row=0, column=2 )
 
        # create an object to keep the value of the checkbox
        self.buttonsEnabled = IntVar()
        self.buttonsEnabled.set( 1 )
 
        # create a checkbox and associate it with the object above.
        self.check1 = ttk.Checkbutton( rootWindow, text="Enable", variable=self.buttonsEnabled )
        self.check1.grid( row=0, column=3 )
 
        # add text area
        self.text = Text( rootWindow, width=80, height=30, background="ivory" )
        self.text.grid( row=1, column=0, columnspan=4 )
 
    def clear( self ):
        self.text.delete( 1.0, END )
 
    def hello( self ):
        if self.buttonsEnabled.get()==1:
            self.label.config( text = "Hello World!" )
        #self.buttonsEnabled.set( 1-self.buttonsEnabled.get() )
 
def main():
    global label
    rootWindow = Tk()
 
    gui = GUI( rootWindow )
    rootWindow.mainloop()
 
 
main()


Challenges of the Day


# challengeOfTheDay.py
# D. Thiebaut
# This app contains a GUI that allows the user to clear the text area, or
# process its contents, and filter out some information, leaving only unique
# email addresses.
# using TKInter.
#
from tkinter import *
from tkinter import ttk
import tkinter.filedialog as fdialog
 
class GUI:
    def __init__( self, root ):
 
        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 ):
        filename = fdialog.askopenfilename(filetypes = (("Text files", "*.txt"),
                                                         ("All files", "*.*")))
        text = ""
        try:
            file = open( filename, "r" )
            text = file.read()
            file.close()            
        except:
            return
 
        self.clear()
        self.text.insert( INSERT, text )
 
 
    def clear( self ):
        self.text.delete(1.0, END) 
 
    def process( self ):
        emails = self.text.get(1.0, END)
        self.clear()
        emailList = []
        for line in emails.split( "\n" ):
            if len( line.strip() ) <= 1:
                continue
            words = line.split( "," )
            emailList.append( words[-1] )
 
        emailList = list( set( emailList ) )
        emailList.sort()
 
        newList = []
 
        smith = ( self.showSmith.get() != 0 )
        hamp  = ( self.showHampshire.get() != 0 )
 
        for email in emailList:
            if smith and email.find( "@smith" ) != -1:
                newList.append( email )
            if hamp and email.find( "@hamp" ) != -1:
                newList.append( email )
 
        self.text.insert( INSERT, "\n".join( newList ) )
 
 
        return
 
 
def main():
    rootWindow = Tk()
    rootWindow.title( "CSC111 TK Demo" )
    #rootWindow.resizable(False, False)
 
    gui = GUI( rootWindow)
 
    rootWindow.mainloop()
 
 
main()