我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用wx.TextAttr()。
def _log(self, line, status='info'): """Writing given line to the log-textCtrl. :Parameters: - `line`: text to log - `status`: status should be 'info' or 'error'. If 'info' the text will be colored blue, if 'error' the text will be red. """ start = self.txtCtrlLog.GetLastPosition() self.txtCtrlLog.AppendText('%s\n' % line) color = wx.BLACK if status == 'info': color = wx.BLUE elif status == 'error': color = wx.RED self.txtCtrlLog.SetStyle(start, self.txtCtrlLog.GetLastPosition(), wx.TextAttr(color))
def ClearHighlights(self, highlight_type=None): if highlight_type is None: self.Highlights = [] else: self.Highlights = [(infos, start, end, highlight) for (infos, start, end, highlight) in self.Highlights if highlight != highlight_type] for control in self.HighlightControls.itervalues(): if isinstance(control, (wx.ComboBox, wx.SpinCtrl)): control.SetBackgroundColour(wx.NullColour) control.SetForegroundColour(wx.NullColour) elif isinstance(control, wx.TextCtrl): value = control.GetValue() control.SetStyle(0, len(value), wx.TextAttr(wx.NullColour)) elif isinstance(control, wx.gizmos.EditableListBox): listctrl = control.GetListCtrl() for i in xrange(listctrl.GetItemCount()): listctrl.SetItemBackgroundColour(i, wx.NullColour) listctrl.SetItemTextColour(i, wx.NullColour) self.StructureElementsTable.ClearHighlights(highlight_type) self.RefreshView()
def ShowHighlights(self): type_infos = self.Controler.GetDataTypeInfos(self.TagName) for infos, start, end, highlight_type in self.Highlights: if infos[0] == "struct": self.StructureElementsTable.AddHighlight(infos[1:], highlight_type) else: control = self.HighlightControls.get((type_infos["type"], infos[0]), None) if control is not None: if isinstance(control, (wx.ComboBox, wx.SpinCtrl)): control.SetBackgroundColour(highlight_type[0]) control.SetForegroundColour(highlight_type[1]) elif isinstance(control, wx.TextCtrl): control.SetStyle(start[1], end[1] + 1, wx.TextAttr(highlight_type[1], highlight_type[0])) elif isinstance(control, wx.gizmos.EditableListBox): listctrl = control.GetListCtrl() listctrl.SetItemBackgroundColour(infos[1], highlight_type[0]) listctrl.SetItemTextColour(infos[1], highlight_type[1]) listctrl.Select(listctrl.FocusedItem, False)
def Advance(self): """Advance to the next error. This method advances the SpellChecker to the next error, if any. It then displays the error and some surrounding context, and well as listing the suggested replacements. """ # Disable interaction if no checker if self._checker is None: self.EnableButtons(False) return False # Advance to next error, disable if not available try: self._checker.next() except StopIteration: self.EnableButtons(False) self.error_text.SetValue("") self.replace_list.Clear() self.replace_text.SetValue("") if self.IsModal(): # test needed for SetSpellChecker call # auto-exit when checking complete self.EndModal(wx.ID_OK) return False self.EnableButtons() # Display error context with erroneous word in red. # Restoring default style was misbehaving under win32, so # I am forcing the rest of the text to be black. self.error_text.SetValue("") self.error_text.SetDefaultStyle(wx.TextAttr(wx.BLACK)) lContext = self._checker.leading_context(self._numContext) self.error_text.AppendText(lContext) self.error_text.SetDefaultStyle(wx.TextAttr(wx.RED)) self.error_text.AppendText(self._checker.word) self.error_text.SetDefaultStyle(wx.TextAttr(wx.BLACK)) tContext = self._checker.trailing_context(self._numContext) self.error_text.AppendText(tContext) # Display suggestions in the replacements list suggs = self._checker.suggest() self.replace_list.Set(suggs) self.replace_text.SetValue(suggs and suggs[0] or '') return True
def initMessageArea(self): """Initialize the message log area. """ # font for messages msgFont = wx.Font(pointSize=11, family=wx.FONTFAMILY_MODERN, style=wx.FONTSTYLE_NORMAL, weight=wx.FONTWEIGHT_NORMAL, underline=False) # font for CEBL introduction message helloFont = wx.Font(pointSize=24, family=wx.FONTFAMILY_ROMAN, style=wx.FONTSTYLE_NORMAL, weight=wx.FONTWEIGHT_BOLD, underline=True) # the message log messageControlBox = widgets.ControlBox(self.scrolledPanel, label='Message Log', orient=wx.VERTICAL) self.messageArea = wx.TextCtrl(self.scrolledPanel, style=wx.TE_MULTILINE | wx.TE_READONLY | wx.TE_RICH) self.messageArea.SetMinSize((150,150)) messageControlBox.Add(self.messageArea, proportion=1, flag=wx.ALL | wx.EXPAND, border=10) # intro message self.messageArea.SetDefaultStyle( #wx.TextAttr(colText=wx.Colour('black'), font=helloFont)) # wxpython3 wx.TextAttr(font=helloFont)) self.messageArea.AppendText('Welcome to CEBL!\n\n') # setup message style self.messageArea.SetDefaultStyle(wx.TextAttr()) #self.messageArea.SetDefaultStyle(wx.TextAttr(colText=wx.Colour('black'), font=msgFont)) # wxpython3 self.messageArea.SetDefaultStyle(wx.TextAttr(font=msgFont)) # add the message area text ctrl widget as a log target self.mgr.logger.addTextCtrl(self.messageArea) messageControlSizer = wx.BoxSizer(orient=wx.HORIZONTAL) # button for saving the message log to a file self.saveMessagesButton = wx.Button(self.scrolledPanel, label='Save') messageControlSizer.Add(self.saveMessagesButton, proportion=0, flag=wx.LEFT | wx.BOTTOM | wx.RIGHT, border=10) self.Bind(wx.EVT_BUTTON, self.saveMessages, self.saveMessagesButton) # button for clearing the message log self.clearMessagesButton = wx.Button(self.scrolledPanel, label='Clear') messageControlSizer.Add(self.clearMessagesButton, proportion=0, flag=wx.BOTTOM | wx.RIGHT, border=10) self.Bind(wx.EVT_BUTTON, self.clearMessages, self.clearMessagesButton) # set up verbose logging self.verboseMessagesCheckBox = wx.CheckBox(self.scrolledPanel, label='Verbose') messageControlSizer.Add(self.verboseMessagesCheckBox, proportion=0, flag=wx.BOTTOM | wx.RIGHT, border=10) messageControlBox.Add(messageControlSizer, proportion=0, flag=wx.EXPAND) # sizer for message log area self.messageSizer = wx.BoxSizer(orient=wx.VERTICAL) self.messageSizer.Add(messageControlBox, proportion=1, flag=wx.ALL | wx.EXPAND, border=10)
def GenerateSearchResultsTreeBranch(self, root, infos): to_delete = [] if infos["name"] == "body": item_name = "%d:" % infos["data"][1][0] else: item_name = infos["name"] self.SearchResultsTree.SetItemText(root, item_name) self.SearchResultsTree.SetPyData(root, infos["data"]) self.SearchResultsTree.SetItemBackgroundColour(root, wx.WHITE) self.SearchResultsTree.SetItemTextColour(root, wx.BLACK) if infos["type"] is not None: if infos["type"] == ITEM_POU: self.SearchResultsTree.SetItemImage(root, self.TreeImageDict[self.ParentWindow.Controler.GetPouType(infos["name"])]) else: self.SearchResultsTree.SetItemImage(root, self.TreeImageDict[infos["type"]]) text = None if infos["text"] is not None: text = infos["text"] start, end = infos["data"][1:3] text_lines = infos["text"].splitlines() start_idx = start[1] end_idx = reduce(lambda x, y: x + y, map(lambda x: len(x) + 1, text_lines[:end[0] - start[0]]), end[1] + 1) style = wx.TextAttr(wx.BLACK, wx.Colour(206, 204, 247)) elif infos["type"] is not None and infos["matches"] > 1: text = _("(%d matches)") % infos["matches"] start_idx, end_idx = 0, len(text) style = wx.TextAttr(wx.Colour(0, 127, 174)) if text is not None: text_ctrl_style = wx.BORDER_NONE | wx.TE_READONLY | wx.TE_RICH2 if wx.Platform != '__WXMSW__' or len(text.splitlines()) > 1: text_ctrl_style |= wx.TE_MULTILINE text_ctrl = wx.TextCtrl(id=-1, parent=self.SearchResultsTree, pos=wx.Point(0, 0), value=text, style=text_ctrl_style) width, height = text_ctrl.GetTextExtent(text) text_ctrl.SetClientSize(wx.Size(width + 1, height)) text_ctrl.SetBackgroundColour(self.SearchResultsTree.GetBackgroundColour()) text_ctrl.Bind(wx.EVT_LEFT_DOWN, self.GetTextCtrlClickFunction(root)) text_ctrl.Bind(wx.EVT_LEFT_DCLICK, self.GetTextCtrlDClickFunction(root)) text_ctrl.SetInsertionPoint(0) text_ctrl.SetStyle(start_idx, end_idx, style) self.SearchResultsTree.SetItemWindow(root, text_ctrl) if wx.VERSION >= (2, 6, 0): item, root_cookie = self.SearchResultsTree.GetFirstChild(root) else: item, root_cookie = self.SearchResultsTree.GetFirstChild(root, 0) for child in infos["children"]: if item is None: item = self.SearchResultsTree.AppendItem(root, "") item, root_cookie = self.SearchResultsTree.GetNextChild(root, root_cookie) self.GenerateSearchResultsTreeBranch(item, child) item, root_cookie = self.SearchResultsTree.GetNextChild(root, root_cookie)