RSGEO
RSGEO

Reputation: 13

Restart script on button press using tkinter

I have a simple script to convert Degree Minute Second to Decimal Degree, using tkinter with a simple GUI. The script waits for the user to click the "Calculate" button before proceeding, to ensure values have been entered in the required 'tk.Entry' fields, then displays the calcuated output.

How would I implement a "Reset" button to allow for another calculation to be run?

I was thinking a while loop for my entire script but don't understand how to implement it.

Apologies for the elementary question, this is my first attempt at using tkinter / GUI, it was much easier to add a re-run while loop to the commandline version of my script.

edit: the "Reset" button currently does nothing, it's just for placement.

# Check python version and import correct tkinter.
import string
import sys
if (sys.version_info.major == 3):
    print("Python 3")
    import tkinter as tk# for Python 3
else:
    print("Python 2")
    import Tkinter as tk# for Python 2.7

# Configure tkinter window name, size, colour, columns.
root = tk.Tk()
root.title("CCT")
root.resizable(False, False)
root.attributes("-alpha", 0.95)
root.config(bg = "#F5F5F5")

# Label for top of application.
label_info = tk.Label(root, text="Coordinate Conversion Tool", pady = 5, bg = "#e8e8e8", padx=10)
label_info.grid(row=0, column=0, columnspan=4, sticky = tk.W+tk.E, pady=(0,10))

# Label and entry for degree.
label_d = tk.Label(text="Degree ->", bg = "#F5F5F5")
label_d.grid(row=1, column=0, columnspan=2, sticky = tk.W, padx=(10,0))
entry_d = tk.Entry(root, width=10, bg = "#e8e8e8")
entry_d.grid(row=1, column=2, columnspan=2, sticky = tk.E, padx=(0,10))

# Label and entry for minute.
label_m = tk.Label(text="Minute ->", bg = "#F5F5F5")
label_m.grid(row=2, column=0, columnspan=2, sticky = tk.W, padx=(10,0))
entry_m = tk.Entry(root, width=10, bg = "#e8e8e8")
entry_m.grid(row=2, column=2, columnspan=2, sticky = tk.E, padx=(0,10))

# Label and entry for second.
label_s = tk.Label(text="Second ->", bg = "#F5F5F5")
label_s.grid(row=3, column=0, columnspan=2, sticky = tk.W, padx=(10,0))
entry_s = tk.Entry(root, width=10, bg = "#e8e8e8")
entry_s.grid(row=3, column=2, columnspan=2, sticky = tk.E, padx=(0,10))

# Radiobutton for quadrant selection.
def retrieve():
    print(quadrant_var.get())

quadrant_var = tk.StringVar(value = "N")
    
quad_button_n = tk.Radiobutton(root, text = "N", variable = quadrant_var, value = "N", command = retrieve, pady = 3)
quad_button_n.grid(row=4, column=0)
    
quad_button_e = tk.Radiobutton(root, text = "E", variable = quadrant_var, value = "E", command = retrieve, pady = 3)
quad_button_e.grid(row=4, column=1)

quad_button_s = tk.Radiobutton(root, text = "S", variable = quadrant_var, value = "S", command = retrieve, pady = 3)
quad_button_s.grid(row=4, column=2)

quad_button_w = tk.Radiobutton(root, text = "W", variable = quadrant_var, value = "W", command = retrieve, pady = 3)
quad_button_w.grid(row=4, column=3)

# Set blank variable for wait_variable
var = tk.IntVar()

# Button for calculating the conversion with wait_variable
calculate = tk.Button(text="Calculate", command=lambda: var.set(1))
calculate.grid(row=5, column=0, columnspan=2, sticky = tk.E+tk.W, padx=10, pady=10)

# Button to reset and allow for another calculation
calculate = tk.Button(text="Reset")
calculate.grid(row=5, column=2, columnspan=2, sticky = tk.E+tk.W, padx=10, pady=10)

# Label with placeohlder output text
label_output = tk.Label(text=" ", borderwidth=2, relief="groove", font="Arial, 12", pady=8)
label_output.grid(row=7, column=0, columnspan=4, sticky = tk.W+tk.E, padx=10)

# Label at the bottom of the application
label_foot = tk.Label(text="Developed by Ryan Seabrook", bg = "#F5F5F5", fg = "#7a7a7a", pady = 1)
label_foot.grid(row=8, column=0, columnspan=4, sticky = tk.W+tk.E)

# Wait for the user to press the calculate button.
calculate.wait_variable(var)

# Information from user input for degree minute second, converted to float for math calculation.
degree = float(entry_d.get())
minute = float(entry_m.get())
second = float(entry_s.get())

# Raw calculated output for DMS to DD.
calculated_dd = (float(degree) + (float(minute)/60) + (float(second)/3600))

# Rounded DD output.
rounded_dd = round(calculated_dd,8)

# Fetch string for final output
rounded_dd_str = str(rounded_dd)
final_output = "output"
selected_quadrant = quadrant_var.get()

# If statement to assign correct quadrant value to output
if selected_quadrant == "W":
    final_output = "-" + rounded_dd_str + " " + selected_quadrant
elif selected_quadrant == "S":
    final_output = "-" + rounded_dd_str + " " + selected_quadrant
else:
    final_output = rounded_dd_str + " " + selected_quadrant

# Label for final output
label_output = tk.Label(text=final_output, borderwidth=2, relief="sunken", font="Arial 12", pady=8)
label_output.grid(row=7, column=0, columnspan=4, sticky = tk.W+tk.E, padx=10)

# Holds python while tkinter root window is open.
root.mainloop()

Upvotes: 1

Views: 965

Answers (1)

There seems to be a related post here,

Restart program tkinter

which also links to this code snippet on this page,

https://www.daniweb.com/programming/software-development/code/260268/restart-your-python-program

which may solve your problem.

I hope this helps!

Upvotes: 1

Related Questions