Python Forum
modify the color a string of characters in python 3.6
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
modify the color a string of characters in python 3.6
#1
hello;
I look for a method python associated to objects that type is str; like the methods : upper(), lower() ...
which can help me to change the color of the character string. and the character of string is contained in a TEXT field of a database sqlite3

In addition to this field I have other fields that add to a tkinter 'Text' widget

Currently this character string is written in black and I want to change its color without integrating it into the 'Label' widget of tkinter

thanks for the help
Reply
#2
python strings don't have a colour attribute.
Reply
#3
how can I modify the color of the string
Reply
#4
It's not clear what you want.
(Feb-24-2019, 12:13 PM)atlass218 Wrote: In addition to this field I have other fields that add to a tkinter 'Text' widget

Currently this character string is written in black and I want to change its color without integrating it into the 'Label' widget of tkinter
It looks like you talk about changing color of text in Tkinter widget (e.g. Text)
If this is the case check https://stackoverflow.com/questions/1478...ext-widget
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
hi;
normally , what I want to do is partially like this:

 
def CreateTable_loc35R():
	
	conn1_loc35R = sqlite3.connect('bdd/ILS35R/ILS35R.db')
	curseur1_loc35R = conn1_loc35R.cursor()
	curseur1_loc35R.execute('''CREATE TABLE IF NOT EXISTS loc35R (id INTEGER PRIMARY KEY,time_loc35R TEXT NOT NULL, monitor_loc35R  TEXT NOT NULL,message_loc35R TEXT NOT NULL)''')
	curseur1_loc35R.close()

#add events to the table by pressing the "save" button
def Add_To_Table_loc35R():

	string_loc35R_approved="information approved by LABO aircraft"
	message_loc35R_approved=string_loc35R_approved.center(56,'*')
		
	string_loc35R_not_approved="information not yet approved by LABO aircraft"
	message_loc35R_not_approved=string_loc35R_not_approved.center(56,'*')
    #here, according to the case of the state of the checkbutton
	if variable_case_correction_loc35R.get ()==0 : #checkbutton no checkmark
		message_loc35R_get=message_loc35R_non_approuve	
	else:	#checkbutton checkmark
		message_loc35R_get=message_loc35R_approuve

	time_loc35R_get = str(time.strftime('%d/%m/%y  à  %H:%M:%S', time.localtime()))
	
	monitor_get = str(ent_mon.get())
	
	conn2_loc35R = sqlite3.connect('bdd/ILS35R/ILS35R.db')
	curseur2_loc35R = conn2_loc35R.cursor()
	curseur2_loc35R.execute('''INSERT INTO loc35R (time_loc35R,monitor_loc35R,message_loc35R) VALUES (?,?,?)''',(time_loc35R_get,monitor_loc35R_get,message_loc35R_get))
	conn2_loc35R.commit()
	curseur2_loc35R.close()
I would like to color the string which is included in the variable 'message_loc35R_approved' in green color
Reply
#6
I forget to indicate the save and check button

#save button
bt_loc35R_save=Button(contenu_application_loc35R,text='save',font=('arial',12,'bold'),command=Add_To_Table_cev_loc35R)
bt_loc35R_save.pack()

#creation of the widget check box for confirmation of the correction
case_correction_loc35R=Checkbutton(loc35R,font=('arial',14,'bold'), variable=variable_case_correction_loc35R)
case_correction_loc35R_ddm_faisceau.configure(text="confirmation of the airplane LABO that the correction is good")
case_correction_loc35R.pack()
Reply
#7
Please, understand that string in database has no color. You will store just the string. No formatting properties like color, font, size, etc.
You may apply different color when presenting the data to the user. How you would do that will depend on your program - e.g. is it GUI, CLI, web-based...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
In this case how can I modify the color of the string.I have in my case two stringVar :

string_loc35R_approved
message_loc35R_not_approved

but only one will be displayed at the Text tkinter according to the state of the checkbutton
and only the content of the string_loc35R_approve will be displayed at green color if the checkbutton is CHECKED
Reply
#9
(Feb-25-2019, 12:34 PM)atlass218 Wrote: will be displayed at the Text tkinter according to the state of the checkbutton

(Feb-24-2019, 04:27 PM)buran Wrote: It looks like you talk about changing color of text in Tkinter widget (e.g. Text)
If this is the case check https://stackoverflow.com/questions/1478...ext-widget
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
I try the procedure of the previous link cited, but I have a problem :

this is the end of the code of the update fonction of the Text widget tkinter:

T_cev_loc35R.insert(END,contenu_table_cev_loc35R)
        if variable_case_correction_loc35R.get ()==0 :
			
            T_cev_loc35R.tag_add("start", "3.5", "3.53")
            T_cev_loc35R.tag_config("start", foreground="black")
        else :
			
            T_cev_loc35R.tag_add("start", "3.9", "3.49")
            T_cev_loc35R.tag_config("start",  foreground="green")
   
curseur3_cev_loc35R.close()
the coloring of the character string is relative to the indexing of the line
when I introduce another data into my database
the line 3 is changed and is not colored at green because of the condition about "variable_case_correction_loc35R"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Extract continuous numeric characters from a string in Python Robotguy 2 533 Jan-16-2021, 12:44 AM
Last Post: snippsat
  Python win32api keybd_event: How do I input a string of characters? JaneTan 3 695 Oct-19-2020, 04:16 AM
Last Post: deanhystad
  How to get first two characters in a string scratchmyhead 2 688 May-19-2020, 11:00 AM
Last Post: scratchmyhead
  Remove escape characters / Unicode characters from string DreamingInsanity 5 3,040 May-15-2020, 01:37 PM
Last Post: snippsat
  Python Consecutive characters vijju56 4 1,326 Apr-22-2020, 11:03 AM
Last Post: TomToad
  Replacing characters in a string with a list cjms981 1 777 Dec-30-2019, 10:50 PM
Last Post: micseydel
  Strange Characters in JSON returned string fioranosnake 4 1,825 Dec-02-2019, 07:25 PM
Last Post: fioranosnake
  Split a long string into other strings with no delimiters/characters krewlaz 4 967 Nov-15-2019, 02:48 PM
Last Post: ichabod801
  Make color for string when print to console hadoan 6 1,244 Oct-31-2019, 01:40 PM
Last Post: hadoan
  How to iterate over some characters in a string and the others will stay as it is. ? sodmzs 9 1,660 Jun-17-2019, 06:45 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020