Skip to content

Instantly share code, notes, and snippets.

@donkirkby
Last active August 29, 2015 14:19
Show Gist options
  • Save donkirkby/ea6728df3c98cdeedb0a to your computer and use it in GitHub Desktop.
Save donkirkby/ea6728df3c98cdeedb0a to your computer and use it in GitHub Desktop.
Tkinter example of calculation based on text fields
# Trivial example adapted from a CodeSkulptor example:
# http://www.codeskulptor.org/#user39_dAIPAbXbfU_0.py
from Tkinter import Button, E, Entry, Frame, Label, StringVar, Tk, W
class Application(Frame):
def get_initials(self):
initials = (self.first_name.get()[:1].upper() +
self.last_name.get()[:1].upper())
print "Initials:", initials
def createWidgets(self):
self.first_name = StringVar()
self.last_name = StringVar()
Label(self, text='First name:').grid(column=1, row=1)
Entry(self, width=7, textvariable=self.first_name).grid(column=2,
row=1,
sticky=(W, E))
Label(self, text='Last name:').grid(column=1, row=2)
Entry(self, width=7, textvariable=self.last_name).grid(column=2,
row=2,
sticky=(W, E))
Button(self,
text="Create Initials",
command=self.get_initials).grid(column=2, row=3)
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
root = Tk()
app = Application(master=root)
app.mainloop()
# Same example using constants instead of a form
# Edit the constants in this section to run the calculation on your data.
FIRST_NAME = "Don"
LAST_NAME = "Kirkby"
# This is the calculation
def get_initials(first_name, last_name):
initials = (first_name[:1].upper() + last_name[:1].upper())
print "Initials:", initials
get_initials(FIRST_NAME, LAST_NAME)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment