micdao
micdao

Reputation: 21

Create a simple GUI for a minimalistic python script

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.

Upvotes: 2

Views: 541

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 386352

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")

Upvotes: 3

Aaron Digulla
Aaron Digulla

Reputation: 328830

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.

Upvotes: 2

Related Questions