GUI and get inputs


I would like to create a GUI to make my script more user friendly for my colleagues and avoid using the console. So far, I could generate a GUI using the following code:

from tkinter import *

win=Tk() #creating the main window and storing the window object in 'win'
win.geometry('500x300') #setting the size of the window
win.title('Key Parameters')

Label(win, text="Enter path for original images").grid(row=0) 
Label(win, text="Enter path for thresholded images").grid(row=1) 
Label(win, text="Enter disk size (integer)").grid(row=2)    #integer
Label(win, text="Enter pixel size (float)").grid(row=3)   #float

ent1 = Entry(win) 
ent2 = Entry(win)
ent3 = Entry(win)
ent4 = Entry(win) 

ent1.grid(row=0, column=1) 
ent2.grid(row=1, column=1)
ent3.grid(row=2, column=1)
ent4.grid(row=3, column=1)


How can I get the inputs (text, integer or float) and assign them to variables? How can I can create a button to allow the script to proceed further?

Thanks in advance.


You already are via the entx = Entry(win) commands. The entx’s are the variables. To get the
value from these variables, you have to use the get() command on each variable. If you don’t like
the names of these variables, then rename them so that they are more contextual.

Also, from my understanding, it is not good practice to use the from tkinter import * command. Instead use: import tkinter as tk. Then, every tkinter function/class/etc will have to be prefixed with tk.

As written, you also don’t need the mainloop(). Here I have made some small adjustments to your code.

import tkinter as tk

win = tk.Tk() #creating the main window and storing the window object in 'win'
win.geometry('500x300') #setting the size of the window
win.title('Key Parameters')

# Create labels
tk.Label(win, text="Enter path for original images").grid(row=1) 
tk.Label(win, text="Enter path for thresholded images").grid(row=2) 
tk.Label(win, text="Enter disk size (integer)").grid(row=3)    #integer
tk.Label(win, text="Enter pixel size (float)").grid(row=4)   #float

# Create input entries
ent1 = tk.Entry(win) 
ent2 = tk.Entry(win)
ent3 = tk.Entry(win)
ent4 = tk.Entry(win)

# Define entry placements on window
ent1.grid(row = 1, column = 1) 
ent2.grid(row = 2, column = 1)
ent3.grid(row = 3, column = 1)
ent4.grid(row = 4, column = 1)

# Define callback function
def print_inputs():
    print('The entered information is:')
    print('Original image path:  {:>10}'.format(ent1.get()))
    print('Threshold image path: {:>10}'.format(ent2.get()))
    print('Disk size (integer):  {:>10}'.format(ent3.get()))
    print('Pixel size (float):   {:>10}'.format(ent4.get()))

def clear_entries():
    ent1.delete(0, tk.END)
    ent2.delete(0, tk.END)
    ent3.delete(0, tk.END)
    ent4.delete(0, tk.END)

    ent1.focus_set() # Resets cursor to first entry

# Create button
print_button = tk.Button(win, width = 10,text = "Print Values",
                                    command = print_inputs)

clear_inputs = tk.Button(win, width = 10,text = "Clear Entries",
                                    command = clear_entries)
# Define placement of button = 40, y = 120) = 40, y = 160)

As a side note, it is good practice to add comments to your code so that the reader (you or others on your team) get a hint as to what the code lines in your script are doing.

I have added two buttons. One to print the inputs (say, for verification or testing/development purposes) and the other to clear the entries and reset the cursor to the first entry.

Note that in the code, the following sub-entry of the create button command, defines the function to call when the button is pressed:

command = print_inputs

If you want to create additional buttons, note that it will have to be accompanied by a function. Since the pressing of a button implies a call to action. The action is executed by the function.