#55 verify entries to be the right type
verify the entries and tells the user when the wrong type is entered
This commit is contained in:
		| @@ -1,6 +1,8 @@ | ||||
| import os | ||||
| from tkinter import * | ||||
| from tkinter.filedialog import askdirectory, askopenfilename, asksaveasfilename | ||||
| from tkinter.ttk import Combobox | ||||
| import subprocess | ||||
|  | ||||
| from pcbdevice.main import main | ||||
|  | ||||
| @@ -9,18 +11,19 @@ frame = Frame(root) | ||||
| frame.pack() | ||||
|  | ||||
| class textBox: | ||||
|   def __init__(self, master, parameter): | ||||
|   def __init__(self, master, parameter, text): | ||||
|     self.master = master | ||||
|     self.createWidget(master, parameter) | ||||
|     self.createWidget(master, parameter, text) | ||||
|  | ||||
|   def createWidget(self, master, parameter): | ||||
|   def createWidget(self, master, parameter, text): | ||||
|     frame = Frame(master, width=500, height=30, ) | ||||
|     frame.pack(side=TOP) | ||||
|     frame.pack_propagate(0) | ||||
|  | ||||
|     Text = Label(frame, text=parameter) | ||||
|     Text.pack(side=LEFT) | ||||
|     self.Box = Entry(frame, width = 20) | ||||
|     self.v = StringVar(frame, value=text) | ||||
|     self.Box = Entry(frame, width = 20, textvariable = self.v) | ||||
|     self.Box.pack(side=RIGHT) | ||||
|  | ||||
| class menuBox: | ||||
| @@ -35,7 +38,8 @@ class menuBox: | ||||
|  | ||||
| 		Text = Label(frame, text=parameter) | ||||
| 		Text.pack(side=LEFT) | ||||
| 		self.Box = Combobox(frame, values = selection, width = 17) | ||||
| 		self.v = StringVar(frame, value = selection[0]) | ||||
| 		self.Box = Combobox(frame, state = 'readonly', values = selection, width = 17, textvariable = self.v) | ||||
| 		self.Box.pack(side=RIGHT) | ||||
|  | ||||
| class button: | ||||
| @@ -44,14 +48,43 @@ class button: | ||||
| 		self.createWidget(master) | ||||
|  | ||||
| 	def execution(self): | ||||
| 		main(PCB.path, Gcode.path, bool(ascii.Box.current()), int(Width.Box.get()), int(Height.Box.get()), | ||||
| 		     int(radius.Box.get()), unit.Box.get()) | ||||
| 		if self.verifyEntry(): | ||||
| 			main(PCB.path, Gcode.path, bool(ascii.Box.current()), int(Width.Box.get()), int(Height.Box.get()), | ||||
| 			     int(radius.Box.get()), unit.Box.get()) | ||||
| 			os.startfile(Gcode.path.rsplit('/', 1)[0]) | ||||
| 		else: | ||||
| 			print('FAIL') | ||||
|  | ||||
| 	def verifyEntry(self): | ||||
| 		if not Width.Box.get().isdigit(): | ||||
| 			self.State.config(text = 'ERROR: Width needs to be a positive integer', fg = 'red', font = 'Helvetica 10 bold') | ||||
| 			return 0 | ||||
| 		if not Height.Box.get().isdigit(): | ||||
| 			self.State.config(text = 'ERROR: Height needs to be a positive integer', fg = 'red', font = 'Helvetica 10 bold') | ||||
| 			return 0 | ||||
| 		if not radius.Box.get().isdigit(): | ||||
| 			self.State.config(text = 'ERROR: Tool radius needs to be a positive integer', fg = 'red', font = 'Helvetica 10 bold') | ||||
| 			return 0 | ||||
| 		if not (radius.Box.get() < Width.Box.get() or radius.Box.get() < Height.Box.get()): | ||||
| 			self.State.config(text = 'ERROR: Tool radius needs to be smaller than Width and Height', fg = 'red', font = 'Helvetica 10 bold') | ||||
| 			return 0 | ||||
| 		if not os.path.isfile(PCB.path) or not PCB.path.endswith('.pbm'): | ||||
| 			self.State.config(text = 'ERROR: Unable to find PCB file or file type is not .pbm', fg = 'red', font = 'Helvetica 10 bold') | ||||
| 			return 0 | ||||
| 		if not Gcode.path.endswith('.gcode'): | ||||
| 			self.State.config(text = 'ERROR: File type is not .gcode', fg = 'red', font = 'Helvetica 10 bold') | ||||
| 			return 0 | ||||
|  | ||||
| 		self.State.config(text='SUCCESS: (ง ͠° ͟ل͜ ͡°)ง', fg='green', font='Helvetica 10 bold') | ||||
| 		return 1 | ||||
|  | ||||
|  | ||||
| 	def createWidget(self, master): | ||||
| 		frame = Frame(master, width=500, height=30, ) | ||||
| 		frame.pack(side=BOTTOM) | ||||
| 		frame.pack_propagate(0) | ||||
| 		self.State = Label(frame, text='') | ||||
| 		self.State.pack(side=LEFT) | ||||
| 		Butt = Button(frame, text = 'execute program', command = lambda: self.execution()) | ||||
| 		Butt.pack(side = RIGHT) | ||||
|  | ||||
| @@ -64,10 +97,12 @@ class pathFind: | ||||
| 	  self.createWidget(master, parameter, openSave) | ||||
|  | ||||
| 	def openDir(self): | ||||
| 		self.path = str(askopenfilename(initialdir = "/",title = "Select file",filetypes = (("pbm files","*.pbm")))) | ||||
| 		self.path = str(askopenfilename(initialdir = "/",title = "Select file",filetypes = (("pbm files","*.pbm"),("all files","*.*")))) | ||||
| 		self.Box.insert(END, self.path) | ||||
|  | ||||
| 	def saveDir(self): | ||||
| 		self.path = str(asksaveasfilename(initialdir = "/",title = "Select file",filetypes = (("pbm files","*.gcode")))) | ||||
| 		self.path = str(asksaveasfilename(initialdir = "/",title = "Select file",filetypes = (("pbm files","*.gcode"),("all files","*.*")))) | ||||
| 		self.Box.insert(END, self.path) | ||||
|  | ||||
| 	def createWidget(self, master, parameter, openSave): | ||||
| 		frame = Frame(master, width=500, height=30, ) | ||||
| @@ -77,10 +112,12 @@ class pathFind: | ||||
| 		Text = Label(frame, text=parameter) | ||||
| 		Text.pack(side=LEFT) | ||||
| 		if openSave: | ||||
| 			Butt = Button(frame, text = 'find path', command = lambda: self.openDir() ) | ||||
| 			Butt = Button(frame, text = 'search', command = lambda: self.openDir()) | ||||
| 		else: | ||||
| 			Butt = Button(frame, text = 'find path', command = lambda: self.saveDir() ) | ||||
| 			Butt = Button(frame, text = 'search', command = lambda: self.saveDir()) | ||||
| 		Butt.pack(side = RIGHT) | ||||
| 		self.Box = Entry(frame, width=45) | ||||
| 		self.Box.pack(side=RIGHT, padx = 3) | ||||
|  | ||||
| def display(this): | ||||
| 	print(this) | ||||
| @@ -92,9 +129,9 @@ root.geometry("550x300") | ||||
| PCB = pathFind(root, 'PCB image path', TRUE) | ||||
| Gcode = pathFind(root, 'Gcode output path', FALSE) | ||||
| ascii = menuBox(root, 'If the image is in ascii or binary', ['binary','ascii']) | ||||
| Width = textBox(root, 'Width of the PCB') | ||||
| Height = textBox(root, 'Height of the PCB') | ||||
| radius = textBox(root, 'Tool\'s radius in mm') | ||||
| Width = textBox(root, 'Width of the PCB', 100) | ||||
| Height = textBox(root, 'Height of the PCB', 100) | ||||
| radius = textBox(root, 'Tool\'s radius in mm', 1) | ||||
| unit = menuBox(root, 'PCB dimension unit', ['mm', 'm', 'in']) | ||||
|  | ||||
| button = button(root) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Ian
					Ian