Python Forum
how to insert image into Text widget Tkinter
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:

[Image: mj72.png]
or

[Image: 6dv6.png]

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

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:

[Image: n2z7.png]

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

[Image: 6f44.png]

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
Reply


Messages In This Thread
how to insert image into Text widget Tkinter - by atlass218 - Apr-12-2019, 11:16 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter: An image and label are not appearing. emont 7 650 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  TKinter Widget Attribute and Method Quick Reference zunebuggy 3 864 Oct-15-2023, 05:49 PM
Last Post: zunebuggy
  My Background Image Is Not Appearing (Python Tkinter) HailyMary 2 4,344 Mar-14-2023, 06:13 PM
Last Post: deanhystad
  [Tkinter] How to insert data json to treeview tkinter? Shakanrose 8 4,469 Jan-19-2023, 03:58 PM
Last Post: Shakanrose
  [Tkinter] Updating tkinter text BliepMonster 5 6,068 Nov-28-2022, 01:42 AM
Last Post: deanhystad
  [Tkinter] Image in Frame in Tabbed Widget Columbo 4 2,162 Sep-28-2022, 08:04 PM
Last Post: deanhystad
  simple tkinter question function call not opening image gr3yali3n 5 3,459 Aug-02-2022, 09:13 PM
Last Post: woooee
  [Tkinter] Tkinter don't change the image DQT 2 1,632 Jul-22-2022, 10:26 AM
Last Post: menator01
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,932 Jun-26-2022, 06:26 PM
Last Post: menator01
  Tkinter Exit Code based on Entry Widget Nu2Python 6 3,010 Oct-21-2021, 03:01 PM
Last Post: Nu2Python

Forum Jump:

User Panel Messages

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