The lesson learned in this was how much time goes into creating an effective GUI. I think that many of us take our day to day GUI’s for granted. The project was to create a more effective application to use with ElementTree, which is an additional library in Python to use for creating and parsing XML. The reason for the XML was that the XML was used for configuration file of a JAR based application.
First you have to start with:
#Tkinter calls for GUI from Tkinter import * import ttk
As the comment states this will call the Tkinter application. In Python 2.7 you must use CAPS Tkinter vs. Python 3.5 you use tkinter.
Everything is based into a root or main loop. This allows the application to run until the user quits, or the program is set to move on.
#Root window root = Tk()
Inside of this is where your GUI will go. There are a ton of different ways to call for user input, checkbox, lists, text fields, etc. I choose to go with 1. Text Field, 2. Listbox and 3. Button for the user to run the program.
Most of the original program was a script that was running in standard Terminal. Previous to the GUI that application would ask the standard set of questions required for my variables.
while loop: ## While loop which will keep going until loop = False print_menu() choice = input("Options 1-4 set the configuration of the report, option 5 runs the report: ") if choice == 1: print "Meaningful Use Stage 1 selected: " meaningfulUseStage="STAGE_1" periodYear = raw_input('What year is this report for? Ex. 2012-2014') elif choice == 2: print "Meaningful Use Modified Scheduled Stage 1 selected: " meaningfulUseStage = "MODIFIED_SCHEDULED_STAGE_1" periodYear = "2015" elif choice == 3: print "Meaningful Use Stage 2 selected: " meaningfulUseStage = "STAGE_2" periodYear = "2014" elif choice == 4: print "Meaningful Use Modified Scheduled Stage 2 selected: " meaningfulUseStage = "MODIFIED_SCHEDULED_STAGE_2" periodYear = raw_input('What year is this report for? Ex. 2015-2016') elif choice == 5: print "Running report: " ## You can add your code or functions here loop = False # This will make the while loop to end as not value of loop is set to False else: raw_input("Wrong option selection. Enter any key to try again..")
This was much more simple since it did not require as many entries to accomplish the same goal. However, in the interest of the end users the GUI was a requirement.
This is the code for the Tkinter calls for a similar part of it. It does however, not contain the def and classes.
#Label for meaningfulUseStage list providerIdLabel = Label(root, text="Please select a Meaningful Use Stage: ") providerIdLabel.pack() #meaningfulUseStage list selection meaningfulUseStageList = Listbox(root, selectmode=SINGLE, height=4, width=30) meaningfulUseStageList.pack() for item1 in ["STAGE_1", "MODIFIED_SCHEDULED_STAGE_1", "STAGE_2", "MODIFIED_SCHEDULED_STAGE_2"]: meaningfulUseStageList.insert(END, item1) meaningfulUseStage = meaningfulUseStageList.curselection() #Label for periodYear periodYearLabel = Label(root, text="Please the reporting year: ") periodYearLabel.pack() #periodYear list selection periodYearList = Listbox(root, selectmode=SINGLE,height=5, width=30) periodYearList.pack() for item2 in ["2012", "2013", "2014", "2015", "2016"]: periodYearList.insert(END, item2) periodYear = periodYearList.curselection()
It is hard to say that it is inefficient as much as it was just more difficult since I was up to this point unfamiliar with Tkinter.
Some of the most common functions were:
Label() – Used to put a label, does not accept user input.
Listbox() – Structured user input, that can accept multiple selections.
Entry() – Text field that accepts user input.
Checkbox() – Check box that takes input in a binary function (on or off), the selection and lack thereof can be set to different tasks.
The tricky part of this was that some of the functions did not all behave in the same fashion. For example, calling Entry() and getting a str from the user input, was much less complex that getting the input from Listbox() or Checkbox(). The args were different and there is additional syntax. The benefit, however of using a more structured approach with Listbox() and Checkbox() is that the user cannot introduce variables that you haven’t already accounted for.
*More to follow