From 967b917a6c5a38b79c627060e840494bdfb6163e Mon Sep 17 00:00:00 2001 From: Benjamin Davis Date: Tue, 12 Apr 2022 10:25:36 -0400 Subject: [PATCH 1/3] Name exported script and select import from multiple scripts --- classes/screen.py | 53 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/classes/screen.py b/classes/screen.py index 19b114d..54fc18c 100644 --- a/classes/screen.py +++ b/classes/screen.py @@ -49,8 +49,8 @@ def __init__(self, root, canvas): file = tk.Menu(self.root, tearoff=False) # add import, export, clear - file.add_command(label = "Import", command=self.import_script) - file.add_command(label = "Export", command=self.export_script) + file.add_command(label = "Import", command=self.choose_import_script) + file.add_command(label = "Export", command=self.export_script_name) file.add_command(label = "Clear", command= self.clear) # attach file submenu to main menu bar @@ -369,11 +369,30 @@ def motion_handler(self, event): self.temp_line = self.canvas.create_line(self.start_point, (event.x, event.y), fill="lime", width=5, arrow=tk.LAST, arrowshape=(8, 10, 8)) + def export_script_name(self): + top = tk.Toplevel(self.root) + + # TextBox Creation + inputtxt = tk.Text(top, + height = 5, + width = 20) + + inputtxt.pack() + + # Button Creation + printButton = tk.Button(top, + text = "Print", + command = lambda: self.export_script(inputtxt.get(1.0, "end-1c"))) + printButton.pack() + # Export path as cpp script - def export_script(self): + def export_script(self, file_name): if not os.path.exists("output"): os.mkdir("output") - f = open("output/script.cpp", "w") + if not file_name.endswith(".cpp"): + file_name += ".cpp" + + f = open(os.path.join("output", file_name), "w") if(len(self.movements) > 0): f.write("// Reset odom\n") f.write( @@ -411,11 +430,31 @@ def clear(self): # set dark mode self.set_darkmode() - def import_script(self): + def choose_import_script(self): + onlyfiles = [f for f in os.listdir(os.path.normpath("output")) if os.path.isfile(os.path.join("output", f))] + if len(onlyfiles) == 0: + print("Error Importing: There is no script in the output folder") + sys.exit() + elif len(onlyfiles) == 1: + self.import_script(onlyfiles[0]) + return + top = tk.Toplevel(self.root) + + # create a listbox to display all the files in the output folder + listbox = tk.Listbox(top, selectmode=tk.SINGLE) + listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) + for f in onlyfiles: + listbox.insert(tk.END, f) + + # create a button to import the selected file + btn = tk.Button(top, text="Import", command=lambda: self.import_script(listbox.get(tk.ACTIVE))) + btn.pack(side=tk.RIGHT) + + def import_script(self, file="script.cpp"): self.clear() # Check if the script.cpp file exists - if not os.path.isfile(os.path.join("output","script.cpp")): + if not os.path.isfile(os.path.join("output",file)): try: os.mkdir("output") except: @@ -424,7 +463,7 @@ def import_script(self): sys.exit() # Open the script - f = open("output/script.cpp","r") + f = open(os.path.join("output", file),"r") start = None # Starting point based on odom::reset allLines = f.readlines() # Read the script From 06f0c0324b43a129e0c063a9ac607311d7fb93b9 Mon Sep 17 00:00:00 2001 From: Mihir Laud Date: Mon, 9 May 2022 14:57:05 -0400 Subject: [PATCH 2/3] Sanitize inputs --- classes/screen.py | 30 +++++++++++++++++++++--------- main.py | 2 +- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/classes/screen.py b/classes/screen.py index 54fc18c..e777cbe 100644 --- a/classes/screen.py +++ b/classes/screen.py @@ -4,6 +4,7 @@ from classes.constants import * import tkinter as tk from tkinter import ttk +import re import sys import os from math import fmod @@ -25,6 +26,7 @@ def __init__(self, root, canvas): self.next_temp_line = None self.movements = [] self.sidebar_groups = [] + self.filename = tk.StringVar() # create scrollable canvas for sidebar self.sidebar_canvas = tk.Canvas(root, width=SIDEBAR_WIDTH, height=SCREEN_HEIGHT) @@ -153,7 +155,7 @@ def key_handler(self, event): self.end_point = (0, 0) # if e is hit, export elif event.keysym == "e": - self.export_script() + self.export_script_name() # if i is hit, import elif event.keysym == "i": self.import_script() @@ -371,19 +373,20 @@ def motion_handler(self, event): def export_script_name(self): top = tk.Toplevel(self.root) - + top.title("Export") + label = tk.Label(top, text="Export As...") + # TextBox Creation - inputtxt = tk.Text(top, - height = 5, - width = 20) + inputtxt = tk.Entry(top, textvariable=self.filename) - inputtxt.pack() + label.grid(row=0, column=0) + inputtxt.grid(row=0, column=1) # Button Creation printButton = tk.Button(top, text = "Print", - command = lambda: self.export_script(inputtxt.get(1.0, "end-1c"))) - printButton.pack() + command = lambda: self.export_script(self.filename.get())) + printButton.grid(row=1, column=0, columnspan=2) # Export path as cpp script def export_script(self, file_name): @@ -392,7 +395,16 @@ def export_script(self, file_name): if not file_name.endswith(".cpp"): file_name += ".cpp" - f = open(os.path.join("output", file_name), "w") + try: + f = open(os.path.join("output", file_name), "w") + except PermissionError: + # pop up modal to alert user that script was exported + top = tk.Toplevel(self.root) + top.title("Export") + tk.Label(top, text= "Failed to export script", font=('Arial 18 bold')).pack(side=tk.TOP) + tk.Label(top, text= "Ensure filepath is valid", font=('Arial 18 bold')).pack(side=tk.TOP) + return + if(len(self.movements) > 0): f.write("// Reset odom\n") f.write( diff --git a/main.py b/main.py index 18f9168..9f3c1ce 100644 --- a/main.py +++ b/main.py @@ -8,7 +8,7 @@ root = tk.Tk() # set window title and size -root.title("Visual Auton Generator v0.1") +root.title("Machine Autonomous Planner v0.1") root.geometry("{0}x{1}+50+0".format(SCREEN_WIDTH, SCREEN_HEIGHT)) # create field image from assets and resize From c4fc890b58d9d6459b306382a8e315e5c777f973 Mon Sep 17 00:00:00 2001 From: Mihir Laud Date: Tue, 10 May 2022 09:57:25 -0400 Subject: [PATCH 3/3] Fix import keybinding --- classes/screen.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/classes/screen.py b/classes/screen.py index e777cbe..1b12356 100644 --- a/classes/screen.py +++ b/classes/screen.py @@ -445,8 +445,11 @@ def clear(self): def choose_import_script(self): onlyfiles = [f for f in os.listdir(os.path.normpath("output")) if os.path.isfile(os.path.join("output", f))] if len(onlyfiles) == 0: - print("Error Importing: There is no script in the output folder") - sys.exit() + top = tk.Toplevel(self.root) + top.title("Import") + tk.Label(top, text= "No scripts in output folder", font=('Arial 18 bold')).pack(side=tk.TOP) + tk.Label(top, text= "Place script in output folder to import", font=('Arial 18 bold')).pack(side=tk.TOP) + return elif len(onlyfiles) == 1: self.import_script(onlyfiles[0]) return @@ -471,8 +474,11 @@ def import_script(self, file="script.cpp"): os.mkdir("output") except: pass - print("Please put script.cpp into the output directory") - sys.exit() + top = tk.Toplevel(self.root) + top.title("Import") + tk.Label(top, text= "File does not exist", font=('Arial 18 bold')).pack(side=tk.TOP) + tk.Label(top, text= "in output folder", font=('Arial 18 bold')).pack(side=tk.TOP) + return # Open the script f = open(os.path.join("output", file),"r")