Python Source Code May Be Edited.
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.
from Tkinter import *
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)
def __init__(self, master):
self.initComplete = 0
frame = Frame(master, width=185, height=188)
self.master = master
self.x, self.y, self.w, self.h = -1,-1,-1,-1
self.Button_1 = Button(self.master,text="Calculate Expression",
self.Button_1.place(x=24, y=36, width=141, height=25)
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_StringVar.set("99 * 1.2 / 7")
self.Label_1 = Label(self.master,text="Answer=", justify="left", \
self.Label_1.place(x=24, y=120, width=144, height=20)
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.
def Button_1_Click(self, event):
print "executed method Button_1_Click"
def Entry_1_StringVar_Callback(self, varName, index, mode):
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.
root = Tk()
app = _Expression(root)
if __name__ == '__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