Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 how to insert image into Text widget Tkinter
#1
Hello;

I created a small application for testing, but I encountered a problem displaying the image in the content of the Tkinter Text widget.

In fact, I have two image and I wish that except for one of them which must appear in the contents according to the state of a checkbutton
Here are the two images I would like them to display in Text tkinter:

İmage

or

İmage


#######################################################

And this depending of the state of the checkbutton ( checkmark or not ticked )

If the checkbutton is marked, I would like the display to be like this image:

İmage


And in the case where the checkbutton is unchecked , I would like the display to be like this image:

İmage


And so on, the data will be modified according to the Entry box dedicated to the introduction of the monitor data. I would have at the end the content subdivided into several paragraphs, each of the paragraphs has a header image 1 or image 2 according to the state of the checkbutton

Here is the code of my application with python 3.6:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

from tkinter import *
import pathlib  #pour creer un dossier qui n'existe encore pas 
import sqlite3
import time
import datetime

root = Tk()
 
case_correction_ddm=IntVar()
ent_monitor_cev=StringVar()



#fonction pour la creation de la table cev
def CreerTable_cev():
	
	#creation du dossier "bdd/Test" pour la bdd test.db
	pathlib.Path('bdd/Test').mkdir(parents=True, exist_ok=True)
	
	conn1 = sqlite3.connect('bdd/Test/Test.db')
	curseur1 = conn1.cursor()
	curseur1.execute('''CREATE TABLE IF NOT EXISTS cev (id_cev INTEGER PRIMARY KEY,time_cev TEXT NOT NULL,lecture_monitor_cev    TEXT NOT NULL,chemin_vers_image_message_cev TEXT NOT NULL)''')
	curseur1.close()


# ajout des valeurs à la table cev par la commande du bouton "Executer"
def Add_To_Table_cev():
	global chemin_image_message_cev_get
	
	if case_correction_ddm.get () :
		chemin_image_message_cev_get='images/correction_approuvee.gif'
	else:	
		chemin_image_message_cev_get='images/correction_non_approuvee.gif'

	time_cev_get = str(time.strftime('%d/%m/%y  à  %H:%M:%S', time.localtime()))
	lecture_monitor_cev_get = str(ent_monitor_cev.get())	

	conn2 = sqlite3.connect('bdd/Test/Test.db')
	curseur2 = conn2.cursor()
	curseur2.execute('''INSERT INTO cev (time_cev,lecture_monitor_cev,chemin_vers_image_message_cev) VALUES (?,?,?)''',(time_cev_get,lecture_monitor_cev_get,chemin_image_message_cev_get))
	conn2.commit()
	curseur2.close()


# lecture du contenu de la table cev
def AfficherTable_cev():

    T_cev.delete('1.0', END)
    T_cev.update()

    conn3 = sqlite3.connect('bdd/Test/Test.db')
    curseur3 = conn3.cursor()     
    for resultats_cev in curseur3.execute('SELECT * FROM cev ORDER BY id_cev DESC'): 

        indice1_modifie=str(resultats_cev[1]).center(70)

        chaine_etoile="*************************************************************************************\n"

        contenu_table_cev= str(indice1_modifie)+"\n\n"+"ddm lue par Monitor  : "+ str(resultats_cev[2])+"\n\n"
		#chemin de l'image est donne par str(resultats_cev[3])
        photo=PhotoImage(file=str(resultats_cev[3]))
        T_cev.insert(INSERT, chaine_etoile)  
        T_cev.image_create(INSERT, image=photo)       	       
        T_cev.insert(END,contenu_table_cev) 
    
    curseur3.close()

#appel a la creation de la table cev
CreerTable_cev()

S_cev = Scrollbar(root)
T_cev = Text(root, height=25, width=90)
S_cev.pack(side=RIGHT, fill=Y)
T_cev.pack(side=LEFT, fill=Y)
S_cev.config(command=T_cev.yview)
T_cev.config(yscrollcommand=S_cev.set)

# affichage du contenu de la table cev
AfficherTable_cev()


entry_lecture_monitor=Entry(root,textvariable=ent_monitor_cev,font=('arial',12,'bold'))
entry_lecture_monitor.pack(side=BOTTOM,pady=10 )

bouton_executer=Button(root,text='Executer',command=lambda:[Add_To_Table_cev(),AfficherTable_cev()])
bouton_executer.pack(side=BOTTOM )
 
case_correction=Checkbutton(root,fg='black',variable=case_correction_ddm)
case_correction.configure(text="confirmation")
case_correction.pack(side=BOTTOM)
 
root.mainloop()

Thanks for the help
Quote
#2
First you must create an instance of Image:
myImage = Image("ImageFileType", name = "fileName")
Then you create an instance of Label; I found that counterintuitive as well. You pass the image object into the image parameter of the labels constructor:
myLabel = Label(root, image = myImage)
I'm new to tkinter myself. Please let me know whether this was helpful.
Quote
#3
Hi;
but my pictures are included inside Text widget and not into Label widget ; see the code after improving the comments :

#!/usr/bin/python3
# -*- coding: utf-8 -*-
 
from tkinter import *
import pathlib  #to create directory if not exits
import sqlite3
import time
import datetime
 
root = Tk()
  
case_correction_ddm=IntVar()
ent_monitor_cev=StringVar()
 

def CreerTable_cev():
     
    #create directories and sub directories "bdd/Test" for test.db
    pathlib.Path('bdd/Test').mkdir(parents=True, exist_ok=True)
     
    conn1 = sqlite3.connect('bdd/Test/Test.db')
    curseur1 = conn1.cursor()
    curseur1.execute('''CREATE TABLE IF NOT EXISTS cev (id_cev INTEGER PRIMARY KEY,time_cev TEXT NOT NULL,lecture_monitor_cev    TEXT NOT NULL,chemin_vers_image_message_cev TEXT NOT NULL)''')
    curseur1.close()
  
# add values to the cev table  by button "Run"
def Add_To_Table_cev():
    global chemin_image_message_cev_get
     
    if case_correction_ddm.get () :
        chemin_image_message_cev_get='images/correction_approuvee.gif'
    else:   
        chemin_image_message_cev_get='images/correction_non_approuvee.gif'
 
    time_cev_get = str(time.strftime('%d/%m/%y  à  %H:%M:%S', time.localtime()))
    lecture_monitor_cev_get = str(ent_monitor_cev.get())    
 
    conn2 = sqlite3.connect('bdd/Test/Test.db')
    curseur2 = conn2.cursor()
    curseur2.execute('''INSERT INTO cev (time_cev,lecture_monitor_cev,chemin_vers_image_message_cev) VALUES (?,?,?)''',(time_cev_get,lecture_monitor_cev_get,chemin_image_message_cev_get))
    conn2.commit()
    curseur2.close()
 
 
# reading the content of the cev table 
def AfficherTable_cev():
 
    T_cev.delete('1.0', END)
    T_cev.update()
 
    conn3 = sqlite3.connect('bdd/Test/Test.db')
    curseur3 = conn3.cursor()     
    for resultats_cev in curseur3.execute('SELECT * FROM cev ORDER BY id_cev DESC'): 
 
        indice1_modifie=str(resultats_cev[1]).center(70)
 
        chaine_etoile="*************************************************************************************\n"
 
        contenu_table_cev= str(indice1_modifie)+"\n\n"+"ddm lue par Monitor  : "+ str(resultats_cev[2])+"\n\n"
        #picture path is given by : str(resultats_cev[3])
        photo=PhotoImage(file=str(resultats_cev[3]))
        T_cev.insert(INSERT, chaine_etoile)  
        T_cev.image_create(INSERT, image=photo)                
        T_cev.insert(END,contenu_table_cev) 
     
    curseur3.close()
 
#call for the creation of the cev table
CreerTable_cev()
 
S_cev = Scrollbar(root)
T_cev = Text(root, height=25, width=90)
S_cev.pack(side=RIGHT, fill=Y)
T_cev.pack(side=LEFT, fill=Y)
S_cev.config(command=T_cev.yview)
T_cev.config(yscrollcommand=S_cev.set)
 
# display the content of the  table cev
AfficherTable_cev()
 

entry_lecture_monitor=Entry(root,textvariable=ent_monitor_cev,font=('arial',12,'bold'))
entry_lecture_monitor.pack(side=BOTTOM,pady=10 )
 
bouton_executer=Button(root,text='Run',command=lambda:[Add_To_Table_cev(),AfficherTable_cev()])
bouton_executer.pack(side=BOTTOM )
  
case_correction=Checkbutton(root,fg='black',variable=case_correction_ddm)
case_correction.configure(text="confirmation")
case_correction.pack(side=BOTTOM)
  
root.mainloop()
Quote
#4
Hi atlass218

Make following modifications and test it again:
# lecture du contenu de la table cev
def AfficherTable_cev():
 
    T_cev.delete('1.0', END)
    T_cev.update()
 
    root.images = []
    conn3 = sqlite3.connect('bdd/Test/Test.db')
    curseur3 = conn3.cursor()     
    for resultats_cev in curseur3.execute('SELECT * FROM cev ORDER BY id_cev DESC'): 
 
        indice1_modifie=str(resultats_cev[1]).center(70)
 
        chaine_etoile="*************************************************************************************\n"
 
        contenu_table_cev= str(indice1_modifie)+"\n\n"+"ddm lue par Monitor  : "+ str(resultats_cev[2])+"\n\n"
        #chemin de l'image est donne par str(resultats_cev[3])
        photo=PhotoImage(file=str(resultats_cev[3]))
        root.images.append(photo)
        T_cev.insert(INSERT, chaine_etoile)  
        T_cev.image_create(INSERT, image=photo)                
        T_cev.insert(END,contenu_table_cev) 
     
    curseur3.close()
wuf :-)
Quote
#5
My mistake. I must have been half asleep when I replied to your post, hence the misunderstanding. I should be more careful. Sadly, this is somewhat over my head, but I'll keep following this thread. I would like to add screenshots to the help section of my own application.
Quote
#6
Hi; keames
I tried the modified code you proposed to me and it worked Thanks

Normally, at the beginning I created my code with strings of characters:
-approved correction. (should be written in green color)
-correction not approved. (should be written in red color)
only one of these two strings of characters which should be displayed according to the state of the checkbutton.
when I tried the code, it did not work, that's why I introduced images instead of character strings

my question is what you can help me modify my code so that it is with strings of characters instead of images

thanks fr help
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Restoring Tkinter widget background to original color pythonprogrammer 1 161 Dec-16-2019, 04:59 AM
Last Post: woooee
  [Tkinter] Image editor using Tkinter and PIL Raulica 2 194 Dec-03-2019, 10:10 PM
Last Post: joe_momma
  Make Label Text background (default color) transparent using tkinter in python barry76 1 1,127 Nov-28-2019, 10:19 AM
Last Post: Larz60+
  Unable to put background image on Tkinter Frame jenkins43 2 438 Nov-27-2019, 11:38 AM
Last Post: jenkins43
  Zooming a tkinter image GalaxyCoyote 2 370 Nov-09-2019, 04:36 PM
Last Post: GalaxyCoyote
  Active tkinter text output during loop dvanommen 2 654 Oct-18-2019, 02:23 PM
Last Post: dvanommen
  [Tkinter] Paste Operation not working in Text Widget Code_Enthusiast 1 275 Sep-11-2019, 08:49 PM
Last Post: Larz60+
  Unable to update or refresh label text in tkinter jenkins43 3 911 Jul-24-2019, 02:09 PM
Last Post: Friend
  [Tkinter] Line Number Tkinter Text Are smabubakkar 1 590 Jun-22-2019, 07:05 PM
Last Post: woooee
  GUI Tkinter Widget Positions punksnotdead 3 483 Jun-12-2019, 06:06 PM
Last Post: Yoriz

Forum Jump:


Users browsing this thread: 1 Guest(s)