Python Forum

Full Version: modify the color a string of characters in python 3.6
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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
python strings don't have a colour attribute.
how can I modify the color of the string
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
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
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()
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...
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
(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
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"
Pages: 1 2