[Tutor] More Tkinter Help Please [Dialog windows]

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Mon, 17 Jun 2002 16:39:38 -0700 (PDT)


On Mon, 17 Jun 2002, SA wrote:

> class Saving(tkSimpleDialog.Dialog):
>     def savin(self, question):
>         Label(question, text="Directory:").grid(row=0)
>         Label(question, text="Filename:").grid(row=1)
>         self.e1 = Entry(question)
>         self.e2 = Entry(question)
>         self.e1.grid(row=0, column=1)
>         self.e2.grid(row=1, column=1)
>     def apply(self):
>         self.dir = self.e1.get()
>         self.fn = self.e2.get()
>         self.sav = self.dir + self.fn
>         self.savfile = open(self.sav, "w")
>         for line in self.t1.readlines():
>             self.savefile.write(line)
>         self.savefile.close()

Hi SA,


I took another look at the tkSimpleDialog stuff on the Tkinter
Introduction page:

http://www.pythonware.com/library/tkinter/introduction/dialog-windows.htm

Are you sure that the 'savin' method is supposed to be named that way?  I
think you mean to call it 'body' instead!



The dialog layout assumes that it can call the body() and buttonbox()
methods to customize the dialog display.  Without customization, it'll
default to a simple yes/no dialog box.  To customize the appearance of the
Saving dialog box, you'll want to write your own versions of body() and
buttonbox().  The Tkinter Introduction above has an example for doing
this:


###
# File: dialog2.py

import tkSimpleDialog

class MyDialog(tkSimpleDialog.Dialog):

    def body(self, master):

        Label(master, text="First:").grid(row=0)
        Label(master, text="Second:").grid(row=1)

        self.e1 = Entry(master)
        self.e2 = Entry(master)

        self.e1.grid(row=0, column=1)
        self.e2.grid(row=1, column=1)
        return self.e1 # initial focus

    def apply(self):
        first = string.atoi(self.e1.get())
        second = string.atoi(self.e2.get())
        print first, second # or something
###

(It doesn't override the buttonbar() method, so by default, it has the
"Ok" and "Cancel" buttons in there.  Using the right method names is
important when we inherit from parent classes, because the parent class
expects to call specific methods.



> Now if I move def saving_callback(): Saving(f) within __init__, I get two

Yes, this should be within.  Having it outside won't work because there
wouldn't be a context, a way of knowing what 'f' meant.



Hope this helps!