def __image_file_selection_event(self): open = ControlFile('image') open.click() fname = open.value.split("/") _filename = fname[len(fname) - 1] ## check blank path if open.value == "": return ## is image try: img = cv2.imread(open.value) height, width, alp = img.shape except: self.alert(msg="File is not a image") #check for file type self.tempath = "" self.path = open.value self.title = "Open file " + path self._imglabel.value = _filename self.tempath = TEMP + _filename copyfile(self.path, self.tempath) self._ToolsBox__updateImage(self.tempath)
class OrganismsGUI(BaseWidget, Organisms): ''' Main widget. Shows all entities created ''' def __init__(self): BaseWidget.__init__(self) Organisms.__init__(self) self._list = ControlList( 'Organisms', add_function=self.__addOrganismBtnAction, remove_function=self.__removeOrganismBtnAction) self._list.horizontal_headers = [ 'Kingdom', 'Class', 'Order', 'Family', 'Name' ] self._panel = ControlEmptyWidget() self.filedialog = ControlFile() self.filedialog.hide() self.mainmenu = [{ 'File': [{ 'Open': self.__openMenuAction }, { 'Save': self.__saveMenuAction }] }] # Add by default some entities self.add(cat) self.add(human) def __addOrganismBtnAction(self): # Popup a window to create a new entity with the user input win = NewOrganismGUI() win.parent = self self._panel.value = win win.show() def __removeOrganismBtnAction(self): if self._list.selected_row_index is not None: self.remove(self._list.selected_row_index) def add(self, organism): Organisms.add(self, organism) # Show the newly created entity in our GUI list self._list += [ organism.kingdom, organism.cls, organism.order, organism.family, organism.name ] def remove(self, index): # Remove the entity selected by user Organisms.remove(self, index) # Reflect changes in GUI self._list -= index def __saveMenuAction(self): self.filedialog.use_save_dialog = True self.filedialog.click() try: with open(self.filedialog.value, 'w') as file: self.save(file) except: pass def __openMenuAction(self): self.filedialog.use_save_dialog = False self.filedialog.click() try: with open(self.filedialog.value, 'r') as file: self.load(file) except: pass
class FileEditor(BaseWidget): def __init__(self): super().__init__() # mainmenu attribute can be specified to create the GUI menu self.mainmenu = [{ 'File': [{ 'Open': self.__openEvent }, { 'Save': self.__saveEvent }, { 'Save as': self.__saveAsEvent }, { 'Reload': self.__reloadEvent }] }] # Create text area to edit file contents self.editor = ControlTextArea() # Create file dialog control object. self.filedialog = ControlFile() self.filedialog.hide() # This attribute will hold the path to the current file being edited self.current_file = None def select_file(self, use_save_dialog=False): ''' This function opens up the file dialog and waits for user input. :return: Returns the selected user file or None if no file was selected. ''' self.filedialog.use_save_dialog = use_save_dialog self.filedialog.click() value = self.filedialog.value if len(value) == 0: return None return value def read_file(self, path): ''' Read file contents of a file and update editor text with its content :param path: :return: ''' with open(path, 'r') as file: self.editor.value = file.read() def write_file(self, path): ''' Write current editor content to a file :param path: :return: ''' with open(path, 'w') as file: file.write(self.editor.value) def __openEvent(self): # This is called when menu "Open..." is clicked try: selected_file = self.select_file() if selected_file is None: raise ValueError() self.read_file(selected_file) self.current_file = selected_file except: pass def __saveEvent(self): # Called when menu "Save" is opened try: if self.current_file is None: self.__saveAsEvent() else: self.write_file(self.current_file) except: pass def __saveAsEvent(self): try: selected_file = self.select_file(use_save_dialog=True) if selected_file is None: raise ValueError() self.write_file(selected_file) self.current_file = selected_file except: pass def __reloadEvent(self): try: self.read_file(self.current_file) except: self.current_file = None
class SimpleExample1(BaseWidget): def __init__(self): super(SimpleExample1, self).__init__(' Thực Tập Cơ Sở ') #main menu self.mainmenu = [{ 'File': [{ 'Open Excel': self.__open, 'icon': 'img/folder_open.png' }, '-', { 'Import': self.__import, 'icon': 'img/import_icon.png' }] }] #tkinler for messagebox root = tk.Tk() root.withdraw() #list self._list = ControlList('Danh sách') self._list.readonly = True #1.open file excel và heap sort self._file = ControlFile('Chọn file Excel') self._butheapsort = ControlButton('Heap Sort') self._butheapsort.icon = 'img/sort_icon.png' self._butheapsort.value = self.__heapsort self._butloadexcel = ControlButton('Load') self._butloadexcel.icon = 'img/load_icon.png' self._butloadexcel.value = self.__load self._butremoveloadexcel = ControlButton('Hủy bỏ') self._butremoveloadexcel.icon = 'img/remove_icon.png' self._butremoveloadexcel.value = self.__removeloadexcel #2.thêm thửa đất self._diachi = ControlText('Địa chỉ') self._dientich = ControlText('Diện tích') self._chusohuuhientai = ControlText('Chủ sở hữu hiện tại') self._loainha = ControlText('Loại nhà') self._mucdichsudung = ControlText('Mục đích Sử dụng') self._giatien = ControlText('Giá tiền') self._but1 = ControlButton('Thêm thửa đất') self._but1.value = self.__add self._but1.icon = 'img/add_icon.png' #3.tìm kiếm thử đất và xóa #tìm kiếm self._butsearch = ControlButton('Tìm kiếm') self._butsearch.icon = 'img/search_icon.png' self._butsearch.value = self.__search self._timkiem = ControlText('Tìm Kiếm') self._checklisttimkiem = ControlCheckBoxList('Chọn tiêu chí tìm kiếm:') self._checklisttimkiem.hide() self._buttonhideshowtimkiem = ControlButton('Hiển thị tiêu chí') self._buttonhideshowtimkiem.value = self._buthideshowtimkiem self._buttonhideshowtimkiem.icon = 'img/show.png' self._buthuybo = ControlButton('Hủy bỏ') self._buthuybo.icon = 'img/remove_icon.png' self._buthuybo.value = self._huybo #xóa self._textxoa = ControlText('Nhập nội dung cần xóa') self._butxoa = ControlButton('Xoá') self._butxoa.icon = 'img/delete_icon.png' self._butxoa.value = self.__xoa self._checklistxoa = ControlCheckBoxList('Chọn tiêu chí xóa:') self._checklistxoa.hide() self._buttonhideshowxoa = ControlButton('Hiển thị tiêu chí') self._buttonhideshowxoa.value = self._buthideshowxoa self._buttonhideshowxoa.icon = 'img/show.png' #4.xuất self._directory = ControlDir('Chọn chỗ xuất file excel') self._tenfilexuat = ControlText('Tên file xuất') self._butxuat = ControlButton('Xuất') self._butxuat.icon = 'img/export_icon.png' self._butxuat.value = self.__xuat #5.merge self._filemerge = ControlFile('Chọn file Excel cần merge') self._butimport = ControlButton('Import') self._butimport.icon = 'img/import2_icon.png' self._butimport.value = self._import self._butmerge = ControlButton('Gộp') self._butmerge.icon = 'img/merge_icon' self._butmerge.value = self._merge self._butmerge.hide() self._listmerge = ControlList('Danh sách import') self._listmerge.readonly = True self._buttonhideshow = ControlButton('Hiển thị tùy chọn') self._buttonhideshow.value = self._buthideshow self._buttonhideshow.hide() self._buttonhideshow.icon = 'img/show.png' self._checklist = ControlCheckBoxList( 'Chọn tiêu chí giữ trong danh sách import:') self._checklist.hide() self._buttonremovemerge = ControlButton('Hủy bỏ') self._buttonremovemerge.value = self._remove self._buttonremovemerge.icon = 'img/remove_icon.png' self._buttonremovemerge.hide() #formset as layout self.formset = [{ '1.Mở File và Heap Sort': [ ' ', '_file', ' ', (' ', '_butloadexcel', '_butremoveloadexcel', '_butheapsort', ' '), ' ' ], '2.Thêm': [ ' ', '_diachi', '_dientich', '_chusohuuhientai', '_loainha', '_mucdichsudung', '_giatien', ' ', (' ', '_but1', ' '), ' ' ], '3.Tìm kiếm và Xóa': [ ' ', '_textxoa', ' ', (' ', '_butxoa', '_buttonhideshowxoa', '_checklistxoa', ' '), ' ', '_timkiem', ' ', (' ', '_butsearch', '_buttonhideshowtimkiem', '_checklisttimkiem', '_buthuybo', ' '), ' ' ], '4.Xuất': [ ' ', '_directory', ' ', '_tenfilexuat', ' ', (' ', '_butxuat', ' '), ' ' ], '5.Merge danh sách': [ '_filemerge', (' ', '_butimport', '_butmerge', '_buttonremovemerge', '_buttonhideshow', '_checklist', ' '), '_listmerge' ], }, '', '', '_list'] #event for mainmenu def __open(self): self._file.click() def __import(self): self._filemerge.click() #event tab 1 #event for _butremoveloadexcel def __removeloadexcel(self): if not values: messagebox.showwarning("Warning", "Không có thông tin cần loại bỏ") else: values.clear() fsqc.clear() self._refresh() #event for _butheapsort def __heapsort(self): if self._list.rows_count <= 1: messagebox.showwarning("Warning", "không có list để sort") else: heap_sort() self._refresh() #event for load button def __load(self): if not self._file.value: tk.messagebox.showwarning("Warning", "Đường dẫn trống") else: try: if self._file.value != '': path = self._file.value read(path) self._list.value = [values_name] n = 0 for i in range(int(len(values) / numberofcols[0])): self._list.__add__(values[n:n + numberofcols[0]]) n = n + numberofcols[0] if self._checklistxoa.count < 1: for s in range(0, len(values_name)): self._checklistxoa.__add__((values_name[s])) if self._checklisttimkiem.count < 1: for s in range(0, len(values_name)): self._checklisttimkiem.__add__((values_name[s])) except: tk.messagebox.showwarning( "Warning", "Không thể đọc file khác excel hoặc đường dẫn không đúng") #event tab 2 #event for thêm button def __add(self): var = str(self._diachi.value).strip().split(',') var2 = var[0].split('/') var3 = var2[0] if self._list.rows_count < 1: messagebox.showwarning("Warning", "Không có list để thêm vào") elif len(var3) == 0 \ or (not var3[0].isdigit() and len(var3) == 1 ) \ or ( not var3[0:(len(var3) -1 )].isdigit() and len(var3) > 1 ) : messagebox.showwarning("Warning", "Địa chỉ không hợp lệ") elif not str(self._dientich.value).strip().isnumeric(): messagebox.showwarning("Warning", "Diện tích không hợp lệ") elif not str(self._chusohuuhientai.value).strip(): messagebox.showwarning("Warning", "Chủ sở hữu trống") elif not str(self._loainha.value).strip(): messagebox.showwarning("Warning", "loại nhà trống") elif not str(self._mucdichsudung.value).strip(): messagebox.showwarning("Warning", "mục đích sử dụng trống") elif not str(self._giatien.value).strip(): messagebox.showwarning("Warning", "giá tiền trống") else: index = self._list.rows_count values.append(index) values.append(str(self._diachi.value)) values.append(str(self._dientich.value)) values.append(str(self._chusohuuhientai.value)) values.append(str(self._loainha.value)) values.append(str(self._mucdichsudung.value)) values.append(str(self._giatien.value)) if var3.isdigit(): fsqc.append(int(var3[0:(len(var3))])) else: fsqc.append(int(var3[0:(len(var3) - 1)])) heap_sort() self._refresh() #event tab 3 #search : def __search(self): if self._list.rows_count <= 1: messagebox.showwarning("Warning", "Danh sách rỗng") elif not self._timkiem.value: messagebox.showwarning("Warning", "Vui lòng nhập nội dung tìm kiếm") elif self._checklisttimkiem.selected_row_index == -1: messagebox.showwarning("Warning", "Vui lòng chọn tiêu chí cần xóa") self._checklisttimkiem.show() self._buttonhideshowtimkiem.icon = 'img/hide_icon.png' self._buttonhideshowtimkiem.label = 'Ẩn tiêu chí' else: self._refresh() s = 1 while s < self._list.rows_count: if not (str(self._timkiem.value).strip()) in str( self._list.get_value( self._checklisttimkiem.selected_row_index, s)): self._list.__sub__(s) s = s - 1 s = s + 1 def _huybo(self): self._refresh() def _buthideshowtimkiem(self): if not values_name: tk.messagebox.showwarning("Warning", "Không có list để chọn tiêu chí") elif str(self._buttonhideshowtimkiem.label) == 'Ẩn tiêu chí': self._checklisttimkiem.hide() self._buttonhideshowtimkiem.icon = 'img/show.png' self._buttonhideshowtimkiem.label = 'Hiển thị tiêu chí' elif str(self._buttonhideshowtimkiem.label) == 'Hiển thị tiêu chí': self._checklisttimkiem.show() self._buttonhideshowtimkiem.icon = 'img/hide_icon.png' self._buttonhideshowtimkiem.label = 'Ẩn tiêu chí' #delete def __xoa(self): if self._list.rows_count <= 1: messagebox.showwarning("Warning", "Danh sách rỗng") elif not self._textxoa.value: messagebox.showwarning("Warning", "Vui lòng nhập nội dung cần xóa") elif self._checklistxoa.selected_row_index == -1: messagebox.showwarning("Warning", "Vui lòng chọn tiêu chí cần xóa") self._checklistxoa.show() self._buttonhideshowxoa.icon = 'img/hide_icon.png' self._buttonhideshowxoa.label = 'Ẩn tiêu chí' else: result = messagebox.askokcancel('Warning', 'Bạn có chắc muốn xóa?') startvaluescount = len(values) if result == 1: s = 1 while s < len(values): if (str(self._textxoa.value).strip()) in str( values[s + self._checklistxoa.selected_row_index - 1]): del fsqc[s // 7] del values[(s - 1):(s + 6)] s = s - 7 s = s + 7 self._refresh() if startvaluescount > len(values): messagebox.showinfo("Sucess!!", "Đã xóa dữ liệu thành công") self._checklistxoa.hide() self._buttonhideshowxoa.icon = 'img/show.png' self._buttonhideshowxoa.label = 'Hiển thị tiêu chí' else: messagebox.showinfo( "Opps", "Nội dung cần xóa không có trong cột tiêu chí trong danh sách" ) def _buthideshowxoa(self): if not values_name: tk.messagebox.showwarning("Warning", "Không có list để chọn tiêu chí") elif str(self._buttonhideshowxoa.label) == 'Ẩn tiêu chí': self._checklistxoa.hide() self._buttonhideshowxoa.icon = 'img/show.png' self._buttonhideshowxoa.label = 'Hiển thị tiêu chí' elif str(self._buttonhideshowxoa.label) == 'Hiển thị tiêu chí': self._checklistxoa.show() self._buttonhideshowxoa.icon = 'img/hide_icon.png' self._buttonhideshowxoa.label = 'Ẩn tiêu chí' #event tab 4 #event _butxuat def __xuat(self): # kiểm tra đường dẫn if not os.path.isdir(self._directory.value): messagebox.showwarning("Warning", "đường dẫn ko có") elif not self._tenfilexuat.value: messagebox.showwarning("Warning", "tên file rỗng") elif not values and not values_name: messagebox.showwarning("Warning", "không có dữ liệu để xuất") else: try: os.makedirs(self._tenfilexuat.value) os.rmdir(self._tenfilexuat.value) if os.path.isfile(self._directory.value + '/' + self._tenfilexuat.value + '.xls'): result = messagebox.askokcancel( 'Warning', 'File đã tồn tại bạn có muốn ghi đè lên ?') if result == 1: write(self._directory.value, self._tenfilexuat.value) myfile = Path(self._directory.value + '/' + self._tenfilexuat.value + '.xls') if myfile.is_file(): messagebox.showinfo("Sucess!!", "Đã xuất file thành công") else: result = messagebox.askokcancel('Warning', 'Bạn có chắc muốn xuất?') if result == 1: write(self._directory.value, self._tenfilexuat.value) myfile = Path(self._directory.value + '/' + self._tenfilexuat.value + '.xls') if myfile.is_file(): messagebox.showinfo("Sucess!!", "Đã xuất file thành công") except OSError: messagebox.showwarning( "Warning", "Tên file không hợp lệ hoặc đang được mở bởi ứng dụng khác" ) #event tab 5 #event _butmerge def _merge(self): if self._list.rows_count < 1: messagebox.showwarning("Warning", "Danh sách rỗng") else: result = messagebox.askokcancel('Warning', 'Bạn có chắc muốn gộp?') if result == 1: for i in range(1, len(valuesimport), 7): n = False for s in range(1, len(values), 7): if valuesimport[i] == values[s]: f = self._checklist.checked_indexes for c in range(0, len(f), 1): values[s + int(f[c]) - 1] = valuesimport[i + int(f[c]) - 1] n = True if not n: fsqc.append(fsqcimport[int(i / 7)]) for s in range(i - 1, i + 6): values.append(valuesimport[s]) self._refresh() for i in range(0, self._listmerge.rows_count): self._listmerge.__sub__(i) for j in range(0, self._listmerge.rows_count): self._listmerge.__sub__(j) self._clearimportdata() self._checklist.hide() self._buttonhideshow.icon = 'img/show.png' self._buttonhideshow.label = 'Hiển thị tùy chọn' self._buttonremovemerge.hide() self._butmerge.hide() self._buttonhideshow.hide() tk.messagebox.showinfo("Success", "Đã merge thành công") #event _buttonremovemerge def _remove(self): if self._listmerge.rows_count < 1: tk.messagebox.showwarning("Warning", "Đã xóa hết!") else: for i in range(0, self._listmerge.rows_count): self._listmerge.__sub__(i) for j in range(0, self._listmerge.rows_count): self._listmerge.__sub__(j) self._clearimportdata() self._checklist.clear() self._buttonremovemerge.hide() self._buttonhideshow.hide() self._checklist.hide() self._butmerge.hide() #event _buttonhideshow def _buthideshow(self): if str(self._buttonhideshow.label) == 'Ẩn tùy chọn': self._checklist.hide() self._buttonhideshow.icon = 'img/show.png' self._buttonhideshow.label = 'Hiển thị tùy chọn' elif str(self._buttonhideshow.label) == 'Hiển thị tùy chọn': self._checklist.show() self._buttonhideshow.icon = 'img/hide_icon.png' self._buttonhideshow.label = 'Ẩn tùy chọn' #event _buttonimport def _import(self): if not self._filemerge.value: tk.messagebox.showwarning("Warning", "Đường dẫn trống") else: path = self._filemerge.value try: importexcel(path) self._listmerge.value = [values_nameimport] n = 0 for i in range(int(len(valuesimport) / numberofcolsimport[0])): self._listmerge.__add__( valuesimport[n:n + numberofcolsimport[0]]) n = n + numberofcolsimport[0] if self._checklist.count < 1: for s in range(0, len(values_nameimport)): self._checklist.__add__((values_nameimport[s], True)) if self._listmerge and not self._buttonhideshow.visible: if str(self._buttonhideshow.label) == 'Ẩn tùy chọn': self._buttonhideshow.icon = 'img/show.png' self._buttonhideshow.label = 'Hiển thị tùy chọn' self._buttonhideshow.show() self._buttonremovemerge.show() self._butmerge.show() except: tk.messagebox.showwarning( "Warning", "Không thể đọc file khác excel hoặc đường dẫn không đúng") #reusable function def _refresh(self): for i in range(1, self._list.rows_count): self._list.__sub__(i) for j in range(1, self._list.rows_count): self._list.__sub__(j) n = 0 for i in range(int(len(values) / numberofcols[0])): self._list.__add__(values[n:n + numberofcols[0]]) n = n + numberofcols[0] # update STT for s in range(1, self._list.rows_count): values[(s - 1) * 7] = s self._list.set_value(0, s, s) def _clearimportdata(self): fsqcimport.clear() valuesimport.clear() sqcimport.clear() numberofcolsimport.clear() values_nameimport.clear() self._checklist.clear()
class VideoGUI(IModelGUI, Video, BaseWidget): def __init__(self, project): IModelGUI.__init__(self) Video.__init__(self, project) BaseWidget.__init__(self, 'Video window', parent_win=project) self._file = ControlFile('Video') self._addobj = ControlButton('Add object') self._addimg = ControlButton('Add Image') self._removevideo = ControlButton('Remove') self._fps_label = ControlLabel('Frames per second') self._width_label = ControlLabel('Width') self._height_label = ControlLabel('Height') self.formset = [ '_name', '_file', '_removevideo', '_fps_label', ('_width_label', '_height_label'), ' ' ] self._name.enabled = False self._addobj.icon = conf.ANNOTATOR_ICON_ADD self._addimg.icon = conf.ANNOTATOR_ICON_ADD self._removevideo.icon = conf.ANNOTATOR_ICON_REMOVE self._addobj.value = self.create_object self._addimg.value = self.create_image self._removevideo.value = self.__remove_video_changed_event self._file.changed_event = self.__file_changed_event self.treenode = self.tree.create_child('Video', icon=conf.ANNOTATOR_ICON_VIDEO) self.treenode.win = self def init_form(self): if not self._formLoaded: self.create_tree_nodes() super().init_form() ##################################################################################### ########### FUNCTIONS ############################################################### ##################################################################################### def __sub__(self, obj): super(VideoGUI, self).__sub__(obj) if isinstance(obj, VideoObject): tree = self.project.tree tree -= obj.treenode return self def draw(self, frame, frame_index): for obj in self.objects: obj.draw(frame, frame_index) def create_tree_nodes(self): self.tree.selected_item = self.treenode self.tree.add_popup_menu_option( label='Add object', function_action=self.create_object, item=self.treenode, icon=conf.ANNOTATOR_ICON_OBJECT ) self.tree.add_popup_menu_option( label='Add geometry', function_action=self.create_geometry, item=self.treenode, icon=conf.ANNOTATOR_ICON_GEOMETRY ) self.tree.add_popup_menu_option( label='Add note', function_action=self.create_note, item=self.treenode, icon=conf.ANNOTATOR_ICON_NOTE ) self.tree.add_popup_menu_option( label='Add image', function_action=self.create_image, item=self.treenode, icon=conf.ANNOTATOR_ICON_IMAGE ) self.tree.add_popup_menu_option('-', item=self.treenode) self.tree.add_popup_menu_option( label='Capture the current frame', function_action=self.__capture_current_frame, item=self.treenode, icon=conf.ANNOTATOR_ICON_IMAGE ) self.tree.add_popup_menu_option('-', item=self.treenode) self.tree.add_popup_menu_option( label='Remove', function_action=self.__remove_video_changed_event, item=self.treenode, icon=conf.ANNOTATOR_ICON_DELETE ) def choose_file(self): self._file.click() def create_object(self): return Object2D(self) def create_image(self): return Image(self) def create_geometry(self): return Geometry(self) def create_note(self): return Note(self) ##################################################################################### ########### EVENTS ################################################################## ##################################################################################### def __capture_current_frame(self): image = self.create_image() current_index = self.video_capture.get(cv2.CAP_PROP_POS_FRAMES) current_index = int(current_index if current_index==0 else (current_index-1)) self.video_capture.set(cv2.CAP_PROP_POS_FRAMES, current_index ) image.name = "frame({0})".format(current_index) res, img = self.video_capture.read() image.image = img def __remove_video_changed_event(self): project = self.project project -= self if len(project.videos)==0: self.close() def __file_changed_event(self): self.filepath = self._file.value ##################################################################################### ########### PROPERTIES ############################################################## ##################################################################################### @property def mainwindow(self): return self.project.mainwindow @property def tree(self): return self._project.tree @property def filepath(self): return self._file.value @filepath.setter def filepath(self, value): Video.filepath.fset(self, value) self.mainwindow.video = self.video_capture self._file.value = value fps = self.video_capture.get(cv2.CAP_PROP_FPS) width = self.video_capture.get(cv2.CAP_PROP_FRAME_WIDTH) height = self.video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT) self._fps_label.value = "FPS: {0}".format(fps) self._width_label.value = "Width: {0}".format(width) self._height_label.value = "Height: {0}".format(height)