def remove_row(self, n_cols, instance, **kwargs): print("remove_row()") childs = self.parent.children # DataGrid.children selected = 0 for ch in childs: # DataGrid.children[n] n = all children print("ch: " + str(ch)) for c in reversed(ch.children): # DataGrid.children[n].children print("c: " + str(c)) if c.id != "Header_Label": print("c.id: " + str(c.id)) # HINT; google kivy datagrid state and find source that references kivy objects. if c.state == "down": # state can be ('normal', 'down', ...) print(str(c.id) + ' - ' + str(c.state), end='') nml2 = MarkupLabel(c.text).markup print(" N_cols: " + str(n_cols), end='') print(" Id: " + str(c.id), end='') print(" Length: " + str(len(ch.children)), end='') print(" Value: " + nml2[1]) self.remove_widget(c) #print (str(c.id) + ' - ' + str(c.state)) selected += 1 if selected == 0: # None were found to be state='down' so delete something - the bottom row. # But, interestingly, the bottom row on the screen is the first row in memory. # Except that items that have .id == 'Header_Label' are the first row(s) in memory. for ch in childs: count_01 = n_cols count_02 = 0 count = 0 while ( count < n_cols ): # Number of columns in a row; must delete all columns in the row. if n_cols != len(ch.children): for c in ch.children: if c.id != "Header_Label": #print("Data: " + str(c.text)) nml2 = MarkupLabel(c.text).markup #print("~m: {}".format(nml2)) print("N_cols: " + str(n_cols), end='') print(" Count: " + str(count), end='') print(" Id: " + str(c.id), end='') print(" Length: " + str(len(ch.children)), end='') print(" Value: " + nml2[1]) self.remove_widget( c ) # there goes one of the columns in the row. count += 1 break else: break else: break print("Done removing items.")
def age_increment(self): # vibrator.vibrate(0.017) self._age = MarkupLabel(self._age).markup self._age = int(self._age[2]) self._age += 1 self._age = f'[b][color=b39ddb]{self._age}[/color][/b]' if main_app.theme == "purple" else f"[b][color=CFD8DC]{self._age}[/b][/color]" self.ids.age.text = self._age
def height_increment(self): # vibrator.vibrate(0.017) self._height = MarkupLabel(self._height).markup self._height = int(self._height[2]) self._height += 1 self._height = f'[b][color=b39ddb]{self._height}[/color][/b]' if main_app.theme == "purple" else f"[b][color=CFD8DC]{self._height}[/b][/color]" self.ids.height.text = self._height
def on_touch_down(self, touch): """ Add selection on touch down """ if super(SelectableLabel, self).on_touch_down(touch): return True if self.collide_point(*touch.pos): if self.selected: self.parent.clear_selection() else: # Not a fan of the following few lines, but they work. temp = MarkupLabel(text=self.text).markup text = "".join(part for part in temp if not part.startswith(("[color", "[/color]", "[ref=", "[/ref]"))) cmdinput = App.get_running_app().textinput if not cmdinput.text and " did you mean " in text: for question in ( "Didn't find something that closely matches, did you mean ", "Too many close matches, did you mean "): if text.startswith(question): name = Utils.get_text_between( text, question, "? (") cmdinput.text = f"!{App.get_running_app().last_autofillable_command} {name}" break elif not cmdinput.text and text.startswith("Missing: "): cmdinput.text = text.replace("Missing: ", "!hint_location ") Clipboard.copy( text.replace('&', '&').replace('&bl;', '[').replace('&br;', ']')) return self.parent.select_with_touch(self.index, touch)
def bmi_calc(self): # vibrator.vibrate(0.035) self._height = MarkupLabel(self.ids.height.text).markup self._height = int(self._height[2]) self.bmi = round(((self.ids.weight.value * 10000) / (self._height**2)), 1) self._height = f'[b][color=b39ddb]{self._height}[/color][/b]' if main_app.theme == "purple" else f"[b][color=CFD8DC]{self._height}[/b][/color]" self.ids.height.text = self._height
def __init__(self, text, pos=(0, 0), color=(1, 1, 1), font_size=20, stretch=(1, 1), **text_kwargs): text = MarkupLabel(text=text, font_size=font_size, **text_kwargs) text.refresh() # Create rectangle object for the text new_size = (text.size[0] * stretch[0], text.size[1] * stretch[1]) rect = Rectangle(size=new_size, pos=pos, texture=text.texture) super(TextSprite, self).__init__(rect, color)
def checkCorrect(self, testWordStr, inputStr, decodedChar): if (self.gTimerState["mainGame"]): mainWord = MarkupLabel(testWordStr).markup[1] if (len(inputStr) > len(mainWord)): self.nextWord() #Get new word, Reset text elif (len(inputStr) > 0): #Highlight correct/wrong if (mainWord[len(inputStr) - 1] == decodedChar ): #Extract text from markup, then extract char at index textHighlight = "#baed91" #Correct, green self.game_totalScore += 10 else: textHighlight = "#ff6961" #Wrong, red self.lblTestWord.text = "[color={}]{}[/color]".format( textHighlight, mainWord) self.lblScore.text = "Score: %05d" % self.game_totalScore if (len(inputStr) == len(mainWord)): #Check if it's last word self.cancelTimer(self.beforeNextWordClk) self.beforeNextWordClk = Clock.schedule_once( self.loadNextWord, 0.5) #Wait for 0.5 second before showing next word
def insert_text(self, substring, from_undo=False): self.page = self.parent.parent.parent if substring == "AC": self.text = "" return if substring == "Del": if self.last_press in "\r\n=": self.insert_text("AC") else: self.do_backspace() return if substring == "*": substring = "x" if substring == "**": substring = "^" if substring == "/": substring = "÷" if substring == "a\u00b2": substring = "^2" if substring == "ceil": if self.selection_text: self.text = self.text.replace(self.selection_text, f"ceil({self.selection_text})") else: self.text = f"ceil({self.text})" return if substring == "|a|": if self.selection_text: self.text = self.text.replace(self.selection_text, f"abs({self.selection_text})") else: self.text = f"abs({self.text})" return if substring == "floor": if self.selection_text: self.text = self.text.replace(self.selection_text, f"floor({self.selection_text})") else: self.text = f"floor({self.text})" return if substring == "\u221aa": if self.selection_text: self.text = self.text.replace(self.selection_text, f"sqrt({self.selection_text})") else: self.text = f"sqrt({self.text})" return if substring == "a!": substring = "!" if substring == str(config_data['base']) + '\u00aa': substring = str(config_data['base']) + '^' if self.last_press in "+-÷x^%\u00b1": if self.last_press == substring: return else: if self.text and substring in "+-÷x^%": self.do_backspace() if substring in "÷x^" and (self.text == "0" or not self.text): return if ((current_page[0] == "standard" and substring not in string.digits + "%()+-x÷^.!=\r\n" + string.ascii_letters and not substring.isdecimal() and substring != "^2") or (current_page[0] == "scientific" and substring not in string.digits + "%()e+-x÷^.!sincotae=\r\n" and substring != "^2" and substring not in ['sin', 'cos', 'tan', 'cosec', 'cot', 'sec', 'log'] and substring not in [ 'sin\u00af\u00b9', 'cos\u00af\u00b9', 'tan\u00af\u00b9', 'cosec\u00af\u00b9', 'cot\u00af\u00b9', 'sec\u00af\u00b9' ] and substring != str(config_data['base'] + '^') and substring != '\u03c0') or (current_page[0] == "convert" and substring not in string.digits + ".=\r\n") or (current_page[0] == "days" and substring not in string.digits + "=\r\n")): return self.last_press = substring if substring in ["\r", "\n", "="]: if self.text == 'pass': self.text = '' self.page.parent.parent.parent.parent.options_open( self.page.layout.buttons[-1][-1]) self.modal_pass = ModalView(size_hint=(0.8, 0.8)) self.modal_pass.add_widget(Pass()) self.modal_pass.open() self.modal_pass.bind( on_dismiss=lambda *args: self.page.parent.parent.parent.parent.options_close()) return if self.text[-1] in "+-÷x^(%": self.page.preview.text = "Complete the equation first!" return self.last_press = substring for opr in "+-÷x^()!%": if self.text.count(opr): break else: if current_page[0] not in ["scientific", "convert", "days"]: link = False if re.findall("[0-9]", self.text): return if self.text.count('.') == 0 and self.text.isalpha: self.text = "www."+self.text+".com" link = True elif self.text.count('.') == 1: if 'www' in self.text: self.text += ".com" else: self.text = "www."+self.text link = True if self.text.count('.') == 2 or link: webbrowser.get().open_new_tab(self.text) self.page.preview.text = "Opened in web browser!" Clock.schedule_once(lambda dt: setattr( self.page.preview, 'text', ''), 1) self.text = '' return self.page.old_text = self.text self.page.preview.text = "[ref=self.old_text]" + \ self.text + "[/ref]" if current_page[0] == "standard": substring = self.text substring = self.replace(substring) if "!" in self.text: substring = self.fac_solve(self.text) try: substring = Basic(exp=substring).solution except: self.page.preview.text = ( "There's some error!") return elif current_page[0] == "scientific": substring = self.text substring = self.replace(substring) if "!" in substring: substring = self.fac_solve(substring) rad = 1 if self.page.layout.buttons[0][2].text == "RAD" else 0 for r in ["sin", "tan", "cos", "cot", "cosec", "sec"]: substring = substring.replace(r, f"Basic(rad={rad}).{r}") for r in [ "sin\u00af\u00b9", "tan\u00af\u00b9", "cos\u00af\u00b9", "cot\u00af\u00b9", "cosec\u00af\u00b9", "sec\u00af\u00b9" ]: r1 = r.replace("\u00af\u00b9", "") substring = substring.replace(r, f"a{r1}") substring = substring.replace( "log", f"Basic(base={config_data['base']}).log") from math import factorial try: substring = Basic(exp=substring).solution except: self.page.preview.text = "Something went wrong!" return elif current_page[0] == "convert": if not self.quantity: self.quantity = self.page.layout.buttons[0][1].text if not self.from_unit: self.from_unit = self.page.layout.buttons[1][1].text if not self.to_unit: self.to_unit = self.page.layout.buttons[1][3].text try: substring = (str( eval("Convert." + self.quantity) (self.text.split()[0], self.from_unit, self.to_unit)) + " " + self.to_unit) self.page.preview.text = ("[ref=self.old_text]" + self.text + " " + self.from_unit + "[/ref]") except: self.page.preview.text = "There's some error!" return elif current_page[0] == "days": try: substring = days_number( self.page.layout.buttons[1][0].text, self.page.layout.buttons[1][2].text, ) if self.page.layout.buttons[2][-1].state == "down": substring += 1 if self.page.layout.buttons[3][-1].state == "down": substring -= 1 substring = str(substring) self.page.preview.text = f"{self.page.layout.buttons[1][0].text} to {self.page.layout.buttons[1][2].text}" except: self.page.preview.text = "Oops! Couldn't find that!" return self.solved = True write_history( self.page.preview.text if current_page[0] == "days" else MarkupLabel(self.page.preview.text).markup[1], substring, current_page[0], ) self.text = "" if self.text == "0" and substring != ".": self.do_backspace() for sub_ in substring: return super(Text, self).insert_text(substring, from_undo=from_undo)
def modal_update_old(self): print("modal_update_old()") #def modal_update(columnHeadings, rows): # How do I get this data from kivy; inside this function? drill down to the .text # Where are the columnHeading fields in kivy data? in cells with .id == "Header_Label". # grid object is present here. # - - - - - - - - - - print("From DataGrid.remove_row(self, n_cols({}), instance, **kwargs)". format(n_cols)) childs = grid.children print(str(childs)) #childs = self.parent.children selected = 0 # No cells selected, yet. doUpdate = True theData = {} for ch in childs: print("ch: " + str(ch)) for c in reversed(ch.children): print("c: " + str(c)) if c.id != "Header_Label": print("c.id: " + str(c.id)) if c.state == "down": #print(str(c.state)) #print("self.remove_widget(c) was here.") #self.remove_widget(c) nml2 = MarkupLabel(c.text).markup print(" Value: " + nml2[1]) print( str(c.id) + ' - ' + str(c.state) + ' - ' + nml2[1]) theData[selected] = nml2[1] # Keep this data for update. print("theData: " + theData) selected += 1 # Another cell selected. if selected > 4: print("Only a single row may be selected for update.") doUpdate = False if selected == 0: # If not selected, above; then pick the first row in memory which is the last row on the screen. print("You must select a row to be updated.") doUpdate = False #print(str(selected)) #for ch in childs: # print(str(ch)) # count_01 = n_cols # count_02 = 0 # count = -1 # #count = 0 # # TODO; next 3 lines loop forever. grid.children must be the wrong object to set this to? # while True: # count += 1 # if count >= n_cols: # break # #while (count + + < n_cols): # print(count) # if n_cols != len(ch.children): # for c in ch.children: # print(c) # if c.id != "Header_Label": # print("Length: " + str(len(ch.children)), end='') # print(" N_cols: " + str(n_cols + 1), end='') # # print(" self.remove_widget(c) was here.") # #self.remove_widget(c) # #count += 1 # break # else: # break # else: # break if not doUpdate: # TODO; Display the error message in kivy so folks can see it. print("Can not doUpdate because of some previous error.") exit() print("doUpdate") print("Data: " + theData) # - - - - - - - - - - columnHeadings = ['ID', 'Nome', 'Preco', 'IVA'] # TODO; where are the product fields inside kivy data? rows = { 1: ['000', 'Product Name 1', '123.45', '23'], 2: ['001', 'Product Name 2', '234.56', '34'], 3: ['002', 'Product Name 3', '345.67', '45'] } #def modal_update(self, columnHeadings, rows): print("modal_update {}".format(columnHeadings)) print("{}".format(rows)) # TODO; copied modal_insert to modal_update; now make modal_update work! # data should contain Column headings (labels) and Data. # columnHeadings = ['ID', 'Nome', 'Preco', 'IVA'] # rows = {1:['000', 'Product Name 1', '123.45', '23'], # 2:['001', 'Product Name 2', '234.56', '34'] # 3:['002', 'Product Name 3', '345.67', '45'] # } # modal_update(self, columnHeadings, rows) # TODO; Make this variable according to the count of items in columnHeadings and rows['1']! elementsCH = len(columnHeadings) elementsR = len( rows[1]) # TODO; shouldn't we iterate through the selected rows? if elementsCH != elementsR: # TODO; logg or msgbox this! # error() inform somebody print( "Something is wrong, the number of columnHeadings ({}) != the number of rows (())!" .format(elementsCH, elementsR)) exit() insertion_grid = GridLayout(cols=2) # TODO; Apply iteration here! # for elementsR: # lbl? = Label(text=columnHeadings[?], id="lbl") # txt? = TextInput(text=rows['?'][0], id="txtinp") lbl1 = Label(text=columnHeadings[0], id="lbl") txt1 = TextInput(text=rows[1][0], id="txtinp") insertion_grid.add_widget(lbl1) insertion_grid.add_widget(txt1) lbl2 = Label(text=columnHeadings[1], id="lbl") txt2 = TextInput(text=rows[1][1], id="txtinp") insertion_grid.add_widget(lbl2) insertion_grid.add_widget(txt2) lbl3 = Label(text=columnHeadings[2], id="lbl") txt3 = TextInput(text=rows[1][2], id="txtinp") insertion_grid.add_widget(lbl3) insertion_grid.add_widget(txt3) lbl4 = Label(text=columnHeadings[3], id="lbl") txt4 = TextInput(text=rows[1][3], id="txtinp") insertion_grid.add_widget(lbl4) insertion_grid.add_widget(txt4) #lbl1 = Label(text='ID', id="lbl") #lbl2 = Label(text='Nome', id="lbl") #lbl3 = Label(text='Preco', id="lbl") #lbl4 = Label(text='IVA', id="lbl") #txt1 = TextInput(text='000', id="txtinp") #txt2 = TextInput(text='Product Name', id="txtinp") #txt3 = TextInput(text='123.45', id="txtinp") #txt4 = TextInput(text='23', id="txtinp") # create content and assign to the view content = Button(text='Close me!') modal_layout = BoxLayout(orientation="vertical") modal_layout.add_widget(insertion_grid) def update_def(self): input_list = [] for text_inputs in reversed(self.parent.children[2].children): if text_inputs.id == "txtinp": input_list.append(text_inputs.text) print(input_list) # TODO; how do I make this an UPDATE? print(input_list) grid.update_row(input_list, body_alignment, col_size, self) # def update_row(self, n_cols, instance, **kwargs): #grid.add_row(input_list, body_alignment, col_size, self) # def add_row(self, row_data, row_align, cols_size, instance, **kwargs): # print view # view.dismiss update_btn = Button(text="Update", on_press=update_def) modal_layout.add_widget(update_btn) modal_layout.add_widget(content) view = ModalView(auto_dismiss=False) view.add_widget(modal_layout) # bind the on_press event of the button to the dismiss function content.bind(on_press=view.dismiss) update_btn.bind(on_release=view.dismiss) view.open()
def update_row(self, row_data, row_align, cols_size, instance, **kwargs): #def add_row(self, row_data, row_align, cols_size, instance, **kwargs): #def update_row(self, n_cols, instance, **kwargs): print("update_row()") doUpdate = True theData = {} childs = self.parent.children # DataGrid.children selected = 0 for ch in childs: # DataGrid.children[n] n = all children print("ch: " + str(ch)) for c in reversed(ch.children): # DataGrid.children[n].children print("c: " + str(c)) if c.id != "Header_Label": print("c.id: " + str(c.id)) # HINT; google kivy datagrid state and find source that references kivy objects. if c.state == "down": # state can be ('normal', 'down', ...) print(str(c.id) + ' - ' + str(c.state), end='') nml2 = MarkupLabel(c.text).markup print("A cell or column item on the selected row.", end='') print(" N_cols: " + str(n_cols), end='') print(" Id: " + str(c.id), end='') print(" Length: " + str(len(ch.children)), end='') print(" Value: " + nml2[1]) #self.remove_widget(c) #print (str(c.id) + ' - ' + str(c.state)) theData[selected] = nml2[ 1] # Keep this data for update. print("theData: " + str(theData)) selected += 1 if selected == 0: # None were found to be state='down' so delete something - the bottom row. # But, interestingly, the bottom row on the screen is the first row in memory. # Except that items that have .id == 'Header_Label' are the first row(s) in memory. for ch in childs: count_01 = n_cols count_02 = 0 selected = 0 while ( selected < n_cols ): # Number of columns in a row; must delete all columns in the row. if n_cols != len(ch.children): for c in ch.children: if c.id != "Header_Label": print( "A cell or column item on the selected row.", end='') #print("Data: " + str(c.text)) nml2 = MarkupLabel(c.text).markup #print("~m: {}".format(nml2)) print("N_cols: " + str(n_cols), end='') print(" Count: " + str(selected), end='') print(" Id: " + str(c.id), end='') print(" Length: " + str(len(ch.children)), end='') print(" Value: " + nml2[1]) theData[selected] = nml2[ 1] # Keep this data for update. print("theData: " + str(theData)) #self.remove_widget(c) # there goes one of the columns in the row. selected += 1 break else: break else: break if not doUpdate: # TODO; Display the error message in kivy so folks can see it. print("Can not doUpdate because of some previous error.") exit() print("If the data is OK then do the ENTRY and UPDATE here.") print("doUpdate") print("Data: " + str(theData)) print("TODO; ENTRY screen") print("TODO; rob entry screen from add_row?") print("TODO; UPDATE DataGrid") print("TODO; UPDATE database") print("Done updating items.")