Anonymous tkinter/Pmw widgets?
Christopher Myers
chris.myers at ingenta.com
Tue May 7 09:19:49 EDT 2002
Anyone know how I can get a reference to an anonymous widget created on
the fly?
Background:
I created a Date-selector widget which is a Pmw dialog with a calendar.
The calendar is created on the fly, using Tkinter Buttons, and as such,
they are not named widgets -- just buttons placed properly in the
interior of the dialog using the Grid geometry manager and the Python
calendar module for which date goes where. I suppose I could create an
array of Buttons as use their array index as the name/reference, but I'd
like to avoid that if it's possible.
I want the dialog to return the label on the button pressed, so I can
use that as the date value.
(Note:
Is there any type of calendar widget already written? I poked around a
bit and couldn't find one, then figured it was pretty easy to write my
own.)
Code snip:
# Calendar selection dialog
self.calendarSelectDialog = Pmw.Dialog(master,
title = 'Select Date',
buttons = ('OK', 'Cancel'),
defaultbutton = 'OK',)
#-------> no command yet . . . #command=self.execCalDialog)
# Pack my calendar widget into the dialog's interior
self.myCalFrame = Frame(self.calendarSelectDialog.interior())
self.myCalFrame.pack()
# Build the calendar
# 8 rows, 7 cols grid. row0 is "Month, Year"
# row1 is Day of Week headings
# Calendar title, and arrow buttons
self.calTitle = StringVar()
self.getDate() # Fills fields self.y, self.m, self.d
self.calTitle.set("%s %i" %(calendar.month_name[self.m],
self.y))
self.calLabel = Label(self.myCalFrame,
textvariable=self.calTitle)
self.calLabel.grid(row=0, column=2, columnspan=3)
self.leftArrow =
PhotoImage(file="/home/chris/moon/arrow_left.gif")
self.rightArrow =
PhotoImage(file="/home/chris/moon/arrow_right.gif")
self.calBtnLeft = Button(self.myCalFrame,
image=self.leftArrow,
command=self.sub_month)
self.calBtnLeft.grid(row=0, column=1)
self.calBtnRight = Button(self.myCalFrame,
image=self.rightArrow,
command=self.add_month)
self.calBtnRight.grid(row=0, column=5)
# Day column Headings
col_cnt=0
for name in calendar.day_name:
day_abbrev = name[:2]
Label(self.myCalFrame,
text=day_abbrev).grid(row=1, column=col_cnt)
col_cnt=col_cnt+1
# Buttons for actual days.
mc = calendar.monthcalendar(self.y, self.m)
for i in range(len(mc)):
for j in range(7):
num = mc[i][j]
if num == 0:
state="disabled"
n = ""
else:
state="normal"
n = `num`
Button(self.myCalFrame, text=n, width=2, state=state,
# -----------> command=self.red_day).grid(row=i+2, column=j)
#
# self.red_day is supposed to turn the day red
# then the user clicks OK. Optimally, I'd like the button's command
# to return the day selected and close the dialog.
#
--
Christopher Myers, Graduate Software Developer
Ingenta, Inc.
12 Bassett St.
Providence, RI 02903
ph: 401.331.2014 x 102
em: chris.myers at ingenta.com
aim: chrismyers001
More information about the Python-list
mailing list