Python Source

Site Navigation



 

Python Source Code May Be Edited.

Application GUI Python source code is created when tk_happy performs a save operation. Python implements the tk GUI by calling the Tkinter standard python library.

The simple application shown at the right was saved and a python source file was generated by tk_happy.

The top of that generated python file is shown below. This code illustrates a user including the standard python math library with a "from math import *" statement.

All major sections have a name and will include an area marked by a comment statement such as:
# >>>>>>insert any user code below this comment for section "NAME"
where user statements may be placed.

#!/usr/bin/env python

# NOTICE... this file is generated by tk_happy.
# Any code or comments added by the user must be in designated areas ONLY.
# User additions will be lost if they are placed in code-generated areas.

# tk_happy generated code. DO NOT EDIT THE FOLLOWING. section "imports"
from Tkinter import *
# >>>>>>insert any user code below this comment for section "imports"
# Place any user import statements here

from math import *

A class is generated for the Tkinter application with all of the widgets, StringVars, trace connections, bind statements, and any event routines that may be needed for those bind statements. (Some of the listing has been modified to fit this web format)

# tk_happy generated code. DO NOT EDIT THE FOLLOWING. section "top_of_init"


class _Expression:
    def __init__(self, master):
        self.initComplete = 0
        frame = Frame(master, width=185, height=188)
        frame.pack()
        self.master = master
        self.x, self.y, self.w, self.h = -1,-1,-1,-1

        # bind master to <Configure> in order to handle any resizing, etc.
        # postpone self.master.bind("<Configure>", self.Master_Configure)
        self.master.bind('<Enter>', self.bindConfigure)


        self.master.title("Expression")

        self.Button_1 = Button(self.master,text="Calculate Expression",
             width="15")
        self.Button_1.place(x=24, y=36, width=141, height=25)
        self.Button_1.bind("<ButtonRelease-1>", self.Button_1_Click)

        self.Entry_1 = Entry(self.master,width="15")
        self.Entry_1.place(x=24, y=84, width=141, height=22)
        self.Entry_1_StringVar = StringVar()
        self.Entry_1.configure(textvariable=self.Entry_1_StringVar)
        self.Entry_1_StringVar.set("99 * 1.2 / 7")
        self.Entry_1_StringVar_traceName =\
             self.Entry_1_StringVar.trace_variable("w", \
             self.Entry_1_StringVar_Callback)

        self.Label_1 = Label(self.master,text="Answer=", justify="left", \
            anchor="w", width="15")
        self.Label_1.place(x=24, y=120, width=144, height=20)
        self.master.resizable(0,0) # Linux may not respect this
        # >>>>>>insert any user code below this comment for section "top_of_init"


Place-holders for bind and trace routines consist simply of print statements. During development, these print statements show that the proper connections are made, and are simply waiting for user logic to be connected.

In the code below, the button click routine will print "executed method Button_1_Click" when the button is pressed. Likewise, the new value of self.Entry_1_StringVar will be printed if it is changed by any part of the code.

    # tk_happy generated code. DO NOT EDIT THE FOLLOWING. section "compID=1"
    def Button_1_Click(self, event): #click method for component ID=1
        pass
        # >>>>>>insert any user code below this comment for section "compID=1"
        # replace, delete, or comment-out the following
        print "executed method Button_1_Click"

    # tk_happy generated code. DO NOT EDIT THE FOLLOWING. section "Entry...
    def Entry_1_StringVar_Callback(self, varName, index, mode):
        pass
        # >>>>>>insert any user code below this comment for section "Entry...
        # replace, delete, or comment-out the following
        print "Entry_1_StringVar_Callback varName, index, mode",\
                   varName, index, mode
        print "    new StringVar value =",self.Entry_1_StringVar.get()

The python file will launch the Tkinter application automatically if it is called directly. The code below shows how tk_happy approaches that task.


# tk_happy generated code. DO NOT EDIT THE FOLLOWING. section "end"

def main():
    root = Tk()
    app = _Expression(root)
    root.mainloop()

if __name__ == '__main__':
    main()

Python comments identify locations for "user" code.

Colorized listings thanks to ASPN Recipe at http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/442482 by Peter Krantz