Abstract: entry class
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 2 12:35:28 2015
@author: yuan
"""
import sys
from Tkinter import *
makemodal=(len(sys.argv)>1)
class MyWindow:
def __init__(self, title='tkinter', width=100, height=80):
self.win=Tk()
self.win.title(title) #title
self.win.minsize(width=width, height=height) #size of window
self.win.geometry('+600+400') #position of window
#add entries
def makeform(self, fields, field_width):
entries = []
for field in fields.keys():
row = Frame(self.win) # make a new row
row.pack(side='top', fill=X) # pack row on top
lab = Label(row, width=field_width, text=field) # add label, entry
lab.pack(side='left')
ent = Entry(row)
ent.insert('end', fields[field])
ent.pack(side='right', expand='yes', fill=X) # grow horizontal
entries.append(ent)
return entries
#add a button configuration
def config_button(self, widget, txt):
widget.config(text=txt)
widget.config(cursor='gumby')
widget.config(bd=5, relief='raised')
widget.config(bg='cyan', fg='black')
widget.config(font=('helvetica', 20, 'bold') )
widget.pack(padx=10, pady=10)
widget.pack(side='left', expand='yes')
return widget
#add a button
def exec_button(self, label, func):
widget=Button(self.win, command=func )
widget=self.config_button(widget, label)
return self.win
def exec_save(self, entry_widget):
for entry in entry_widget:
#Gets the current contents of the entry field
print entry.keys(), '=>', entry.get()
print "\n"
###main program
#the simplest label window
ww=MyWindow('Personal Information', 400, 200)
#the window will be present on the left top corner of the screen
#win=ww.win_v1('Hello GUI world')
#entries
fields = {'First Name':'John', 'Last Name':'Wang', 'Gender':'Male',
'Citizen':'Moon', 'Birth(mm/dd/yyyy)':'07/04/2088'}
entry_widget = ww.makeform(fields, 15)
#add two buttons
win=ww.exec_button('Save', lambda e=entry_widget: ww.exec_save(e) )
win=ww.exec_button('Close', sys.exit )
win.mainloop()
No comments:
Post a Comment