[Tkinter-discuss] looking for a multiline equivalent oftkSimpleDialog.askstring

Michael O'Donnell michael.odonnell at uam.es
Thu Apr 23 16:57:55 CEST 2009


Hi PaterMaximus,

Code a bit long, but it should do what you want:

-------------------------------------------

from Tkinter import *

import os

class ModalDialog(Toplevel):

    def __init__(self, parent, x, y, title):
        Toplevel.__init__(self, parent)
        self.transient(parent)
        self.title(title)
        self.protocol( 'WM_DELETE_WINDOW', self.cancel)
        self.value=None
        self.x = x
        self.y = y

        # NEED TO HIDE UNTIL SHOWN
        # NEED TO POSITION REGARD TO X, Y

    def show( self ):
        self.position()
        oldFoc = self.focus_get()
        self.focus_set()
        self.grab_set()
        self.lift()
        self.deiconify()
        self.update()
        self.mainloop()
        self.grab_release()
        if oldFoc:
           oldFoc.focus_set()
        return self.value


    def returnValue(self):
        self.quit()

    def cancel(self):
        self.value=None
        self.quit()

    def position(self):
        # We want the window displayed hidden  in case it is moved
        self.withdraw()
        self.update()

        # Position the window at x,y
        # Ensure the window will not go off screen
        # windims are like '30x40+50+50'
        # 30x40 is the size, the rest is startpos

        maxX, maxY = self.maxsize()
        maxY -= 40
        windims = self.geometry().split('+')[0].split('x')
        width=int(windims[0])
        height=int(windims[1])

        if self.x == None or self.y == None:
            self.x=(maxX-width)/2
            self.y=(maxY-height)/2

        if (self.x+width) > maxX: self.x = maxX - width
        if (self.y+height) > maxY: self.y = maxY - height

        # Now position the window and show it.
        self.geometry("+"+str(self.x)+"+"+str(self.y))
        self.update()


def editString(par, prompt, startVal="", x=None, y=None):
    gframe = EditStringDialog(par, prompt, startVal, x, y)
    val = gframe.show()
    gframe.destroy()
    return val

class EditStringDialog(ModalDialog):

    def __init__(self, parent, prompt, startValue, x, y):
        ModalDialog.__init__(self, parent, x, y, prompt)
        self.config(bg="white")
        self.value=startValue

        self.ew=Text(self, bg="white",  width=60, borderwidth=0, height=6)

        if startValue:
            self.ew.insert("1.0", startValue)
        self.ew.pack(side="top", fill="both", padx=5, expand=True)

        bframe=Frame(self, bd=10)
        bframe.pack(side="top", fill="x")

        okBut=Button(bframe, text="OK", command=self.returnValue,
underline=False, default="active")
        cancelBut = Button(bframe, text="Cancel", command=self.cancel,
underline=False)

        okBut.pack(side="left")
        cancelBut.pack(side="right")

    def returnValue(self):
        self.value=self.ew.get("1.0", END)
        self.quit()

if __name__ == '__main__':
    mywin = Tk()
    Button(mywin, text="Go",
           command=lambda win=mywin, prompt="Hello": editString(win,
prompt)).pack()
    mywin.mainloop()

On Thu, Apr 23, 2009 at 4:21 PM, PaterMaximus <patermaximus at got2know.net> wrote:
>
> any help appreciated
>
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>


More information about the Tkinter-discuss mailing list