dimanche 4 mai 2014

Python 2.7 - changement de couleur du seul caractère dans StaticText - Stack Overflow


I am writing a program that randomly generates encrypted quotations from famous people. My program displays a cryptoquote and the user must decode the quote. I want to take input from on which letter to decode. I've successfully implemented it such that, when they specify a letter to decode (example:"a=e"), the program replaces the encrypted text with the text that they specified.


I want it so that, when a letter is replaced, it is colored red so that the user can keep up with what letters they have changed.


def OnDec(self, e):
dlg = wx.TextEntryDialog(self, "Which letter do you wish to change? Use format: 'a=e'", "Decode Letter", "")
dlg.ShowModal()
decode = dlg.GetValue()
#Text entry filter
match = re.search(r'\w+=\w+|^\d*$', decode)
if not match:
err = wx.MessageDialog(self, "That is not a correct entry format.", "Entry Error", style=wx.ICON_HAND)
err.ShowModal()
#Letter replacement
origin = decode[0].upper()
replace = decode[2].upper()
for n in range(0, len(cryp)):
if cryp[n] == origin:
cryp[n] = replace
self.txt.SetLabel("".join(cryp))
self.txt.SetForegroundColour("RED")
self.sizer.Layout()

This procedure is the event that executes when the "Decode Letter" button is pressed. cryp here is a list of each individual letter that I made from the encrypted quotations. The requested format for the decode variable is "a=b".


I can change the whole label to RED, but I want to just change the color of the letters that have been decoded.




The StaticText widget does not support that behavior. You can probably do that with a read-only wx.TextCtrl that has the wx.TE_RICH or wx.TE_RICH2 flag enabled. Otherwise you might just want to draw the text using wx.GCDC or wx.PaintDC. That would be even more flexible.



I am writing a program that randomly generates encrypted quotations from famous people. My program displays a cryptoquote and the user must decode the quote. I want to take input from on which letter to decode. I've successfully implemented it such that, when they specify a letter to decode (example:"a=e"), the program replaces the encrypted text with the text that they specified.


I want it so that, when a letter is replaced, it is colored red so that the user can keep up with what letters they have changed.


def OnDec(self, e):
dlg = wx.TextEntryDialog(self, "Which letter do you wish to change? Use format: 'a=e'", "Decode Letter", "")
dlg.ShowModal()
decode = dlg.GetValue()
#Text entry filter
match = re.search(r'\w+=\w+|^\d*$', decode)
if not match:
err = wx.MessageDialog(self, "That is not a correct entry format.", "Entry Error", style=wx.ICON_HAND)
err.ShowModal()
#Letter replacement
origin = decode[0].upper()
replace = decode[2].upper()
for n in range(0, len(cryp)):
if cryp[n] == origin:
cryp[n] = replace
self.txt.SetLabel("".join(cryp))
self.txt.SetForegroundColour("RED")
self.sizer.Layout()

This procedure is the event that executes when the "Decode Letter" button is pressed. cryp here is a list of each individual letter that I made from the encrypted quotations. The requested format for the decode variable is "a=b".


I can change the whole label to RED, but I want to just change the color of the letters that have been decoded.



The StaticText widget does not support that behavior. You can probably do that with a read-only wx.TextCtrl that has the wx.TE_RICH or wx.TE_RICH2 flag enabled. Otherwise you might just want to draw the text using wx.GCDC or wx.PaintDC. That would be even more flexible.


0 commentaires:

Enregistrer un commentaire