2

I wrote a small python function, which takes several numerical input parameters and prints many lines with statements, which going to be used in an experiment, like this toy example:

    def myADD(x,y,z):
        res = x + y + z
        print("the result is: {0}+{1}+{2}={3}").format(x,y,z,res)

I would like to create a minimalistic GUI, simply an overlay which calls my myADD.py script, where I can fill those parameters x,y,z and after clicking a "compute" button a text field occurs with the print statement.

Does anyone has a template, I was looking into the TKinter, but my attempts by manipulating other templates didn't succeed.

Would appreciate help, thanks.

4
  • 1
    Tkinter would be your best shot. MAYBE pygame Commented Mar 26, 2014 at 13:00
  • There are several GUI-Libraries available (I prefer QT). You should provide more information about your solution/attempts in order to get helpful hints) Commented Mar 26, 2014 at 13:33
  • @dorvak: I don't think any more information is necessary. We know the GUI should be minimalistic, and it has exactly three inputs. What more do you need to know? Commented Mar 26, 2014 at 14:09
  • IMO, SO is not a platform where you should aks for complete & working solutions, but a place to get hints and advices on your own attempts. Commented Mar 26, 2014 at 14:16

2 Answers 2

3

Tkinter is a fantastic choice since it is built-in. It is ideally suited for this type of quick, minimalistic GUI.

Here's a basic framework for a Tkinter app to show you how simple it can be. All you need to do is add your function, either by importing it or including it in the same file:

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.parent = parent
        self.entry = {}

        # the basic layout is a form on the top, and
        # a submit button on the bottom
        form = tk.Frame(self)
        submit = tk.Button(self, text="Compute", command=self.submit)
        form.pack(side="top", fill="both", expand=True)
        submit.pack(side="bottom")

        # this fills in the form with input widgets for each parameter
        for row, item in enumerate(("x", "y", "z")):
            label = tk.Label(form, text="%s:"%item, anchor="w")
            entry = tk.Entry(form)
            label.grid(row=row, column=0, sticky="ew")
            entry.grid(row=row, column=1, sticky="ew")
            self.entry[item] = entry

        # this makes sure the column with the entry widgets
        # gets all the extra space when the window is resized
        form.grid_columnconfigure(1, weight=1)

    def submit(self):
        '''Get the values out of the widgets and call the function'''
        x = self.entry["x"].get()
        y = self.entry["y"].get()
        z = self.entry["z"].get()
        print "x:", x, "y:", y, "z:", z


if __name__ == "__main__":
    # create a root window
    root = tk.Tk()

    # add our example to the root window
    example = Example(root)
    example.pack(fill="both", expand=True)

    # start the event loop
    root.mainloop()

If you want the result to appear in the window, you can create another instance of a Label widget, and change it's value when you perform the computation by doing something like self.results_label.configure(text="the result")

Sign up to request clarification or add additional context in comments.

Comments

2

Tkinter is usually a good start because it is bundled with Python (tutorial).

That said, Tk is pretty old and therefore "odd" at times. If you want a more modern UI, have a look at PyQt. It's based on Qt but it doesn't come with Python by default, so you have to install it manually.

6 Comments

How do you define "odd"? I think recommending PyQT is overkill for a project specifically described as "minimalistic GUI".
I define "odd" as "behaves in unexpected ways that will eat countless hours of your time trying to understand why it happens and then even more trying to fix it."
hmm. Ok. I've never experienced that. Tkinter has very well defined behavior, and is very consistent in that behavior. I'm not sure why you think it works in unexpected ways. <shrug>
@BryanOakley: 1. If you have a tree or a list, you need to define key handlers to move the current selection using the keyboard. I expect that a UI framework does that for me. 2. It looks outdated. 3. The available layouts are very limited. 4. The widgets are very basic. Is there an HTML browser widget?
@BryanOakley: Note that I gave you a +1 for your answer. I just think that Tk's time is over :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.