1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
# GUIQuestion.py
# $Id: GUIQuestion.py,v 1.13 2001/07/02 22:50:39 s2mdalle Exp $
# Written by David Allen <mda@idatar.com>
# Released under the terms of the GNU General Public License
#
# A GUI representation of a question. It should provide all methods needed
# to get the response, set default values, etc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#############################################################################
from Tkinter import *
import Pmw
import tkFileDialog
from string import *
from gopher import *
import GopherResource
import GopherResponse
import GopherConnection
import ContentFrame
import Cache
import Options
import Question
GUIQuestionException = "Sorry. Things just sometimes go wrong."
class GUIQuestion(Frame):
verbose = None
def __init__(self, parent_widget, question):
Frame.__init__(self, parent_widget) # Superclass constructor
self.parent = parent_widget
self.question = question
self.type = self.question.getType()
promptString = self.question.getPromptString()
defaultValue = self.question.getDefault()
Label(self, text=promptString).grid(row=0, column=0, sticky=W)
if self.type == QUESTION_NOTE:
# Prompt string is all we need for this one.
return None
if self.type == QUESTION_ASK:
self.entry = Entry(self)
if len(defaultValue) > 0:
self.entry.insert('end', defaultValue)
self.entry.grid(row=0, column=1, columnspan=4, sticky=W)
return None
if self.type == QUESTION_ASKF or self.type == QUESTION_CHOOSEF:
self.entry = Entry(self)
if len(defaultValue) > 0:
self.entry.insert('end', defaultValue)
self.entry.grid(row=0, column=1, columnspan=4, sticky=W)
# Browse buttons for file selection.
self.browse = Button(text="Browse", command=self.browse)
self.browse.grid(row=0, column=5, sticky=W)
return None
if self.type == QUESTION_ASKP:
self.entry = Entry(self, show="*")
if len(defaultValue) > 0:
self.entry.insert('end', defaultValue)
self.entry.grid(row=0, column=1, columnspan=4, sticky=W)
return None
if self.type == QUESTION_ASKL:
self.entry = Pmw.ScrolledText(self, hscrollmode='dynamic',
text_width=80, text_height=6,
vscrollmode='dynamic')
self.entry.grid(row=1, column=0, columnspan=2, rowspan=2,
sticky='N')
return None
if self.type == QUESTION_SELECT:
self.entry = Pmw.RadioSelect(self, buttontype='checkbutton',
command=self.changed)
for opt in self.question.options:
self.entry.add(opt)
if defaultValue:
print "Invoking defalut %s" % defaultValue
self.entry.invoke(defaultValue)
self.entry.grid(row=1, column=0, columnspan=4, rowspan=4,
sticky='NSEW')
print 'Returning SELECT GUIQuestion'
return None
if self.type == QUESTION_CHOOSE:
self.entry = Pmw.RadioSelect(self, buttontype='radiobutton',
command=self.changed)
for opt in self.question.options:
self.entry.add(opt)
if defaultValue:
print "Invoking defalut %s" % defaultValue
self.entry.invoke(defaultValue)
self.entry.grid(row=1, column=0, columnspan=4, rowspan=4,
sticky='NSEW')
print "Returning CHOOSE GUIQuestion"
return None
return None
def browse(self, *args):
dir = os.path.abspath(os.getcwd())
filename = tkFileDialog.asksaveasfilename(initialdir=dir)
self.entry.delete(0, 'end')
if filename:
self.entry.insert('end', filename)
return None
def getType(self):
return self.question.getType()
def changed(self, *args):
print "Selection changed: Current selection: ", args
def getResponse(self):
"""Returns the current state of the widget, or what should be sent
to the server."""
if self.entry.__class__ == Entry:
return "%s\n" % self.entry.get()
elif self.entry.__class__ == Pmw.ScrolledText:
buf = self.entry.get()
lines = count(buf, "\n")
return "%d\n%s\n" % (lines, buf)
elif self.entry.__class__ == Pmw.RadioSelect:
list = self.entry.getcurselection()
return "%s\n" % list[0]
else:
# Huh? What? Eh? WTF is going on?
raise GUIQuestionException, "Cannot get content: Unknown type"
return ""
|