Last active
August 29, 2015 14:19
-
-
Save donkirkby/ea6728df3c98cdeedb0a to your computer and use it in GitHub Desktop.
Tkinter example of calculation based on text fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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