Need GUI pop-up to edit a (unicode ?) string

Bryan bryan.oakley at gmail.com
Tue Jan 25 16:54:29 EST 2011


On Jan 22, 2:22 pm, Rikishi42 <skunkwo... at rikishi42.net> wrote:
> I'm in need for a graphical pop-up that will display a (unicode ?) string in
> a field, allow the user to change it and return the modified string.
>
> Maybe also keep the original one displayed above it.
>
> Something like this:
> +-------------------------------------------------+
> |   Please confirm or edit the following string   |
> |                                                 |
> |     Original_example_string                     |
> |                                                 |
> |  +-------------------------------------------+  |
> |  |  Original_about_to_be_changed             |  |
> |  +-------------------------------------------+  |
> |                                                 |
> |                     OK                          |
> |                                                 |
> +-------------------------------------------------+
>
> I've never used any kind of graphical interface programing before, so I
> don't have a clue where to start.
> This would, however, be the *only* GUI part in the app at this point.
>
> From what I can see the solution lays with PyQT, but the docs I find are
> courses that aim to teach the whole GUI tool. I only need a little pop-up to
> alow a user to edit part of a filename, for instance.
>
> I'm using Python 2.6.x on various Linux platforms (mainly openSUSE and Mint)
> and on Windows. Windows support is not important, in this case.

tkinter is likely the easiest solution. Here's a quick hack, assuming
you want a program with a single window, rather than dialog you can
pop up.  This has no cancel button since you didn't specify you wanted
one. It just pops up a window, and when you press ok, <return> or
dismiss via the window manager, the edited value will be printed to
stdout. It's not a perfect solution but it gives you a feel for how
easy it is to do with tkinter.

import Tkinter as tk

class App(tk.Tk):
    def __init__(self, s):
        tk.Tk.__init__(self)
        self.wm_title("Edit the string")

        # the main layout is composed of four areas:
        # 1) the label / instructions
        # 2) the original value
        # 3) the edit field
        # 4) a row of buttons
        label = tk.Label(self, text="Please confirm or edit the
following string:")
        oframe = tk.LabelFrame(text="Original:")
        eframe = tk.LabelFrame(text="Edited:")
        buttons = tk.Frame(self)

        orig = tk.Entry(self, width=40)
        edit = tk.Entry(self, width=40)
        edit.insert(0, s)
        orig.insert(0, s)
        orig.config(state="disabled")
        ok = tk.Button(self, text="Ok", command=self.ok,
default="active")

        # this does all the layout
        label.pack(side="top", fill="both", expand=True)
        oframe.pack(side="top", fill="both", expand=True, padx=4)
        eframe.pack(side="top", fill="both", expand=True, padx=4,
pady=(4,0))
        orig.pack(in_=oframe, side="top", fill="x", padx=4, pady=4)
        edit.pack(in_=eframe, side="top", fill="x", padx=4, pady=4)
        buttons.pack(side="bottom", fill="x", pady=4)
        ok.pack(in_=buttons, expand=True)

        edit.select_range(0, "end")
        edit.bind("<Return>", lambda event: self.ok)
        self.wm_protocol("WM_DELETE_WINDOW", self.ok)
        edit.focus()

        # keep a reference so self.ok can access it
        self.edit = edit

    def ok(self):
        value = self.edit.get()
        print value
        self.destroy()

if __name__ == "__main__":
    import sys
    try:
        s = sys.argv[1]
    except:
        s = "Hello, world"
    app = App(s)
    app.mainloop()



More information about the Python-list mailing list