I have created a chat app where i want to share images and pictures as well and i am displaying picture on the chat box every thing is going right till here but i want when any one click on displayed image or picture it will open in an image viewer for this i took help from this question and try something https://stackoverflow.com/questions/49070292/how-to-bind-an-image-within-a-tkinter-text-widget-to-an-event/49070786#49070786
when i run the code picture open right after displaying on chat box but i want to open picture when someone click on it. here is my code:
import functools
import json
import sys
from functools import partial
import tooltip as t
from tkinter import *
from upload import *
import PIL.Image
import PIL.ImageTk
import os
import pathlib
import cgitb
from tkinter import filedialog
from tkinter.filedialog import askopenfile
import threading
import ctypes
cgitb.enable ( )
class ChatInterface ( Frame ) :
def __init__ ( self , master = None ) :
Frame.__init__ ( self , master )
self.imglist = [ ]
self.master = master
# sets default bg for top level windows
self.text_frame = Frame ( self.master , bd = 6 , bg = "black" )
self.text_frame.pack ( expand = True , fill = BOTH )
self.text_box_scrollbar = Scrollbar ( self.text_frame , bd = 0 , bg = "#426CB4" )
self.text_box_scrollbar.pack ( fill = Y , side = RIGHT )
self.text_box = Text ( self.text_frame , yscrollcommand = self.text_box_scrollbar.set , state = DISABLED ,
bd = 1 , padx = 6 , pady = 6 , spacing3 = 8 , wrap = WORD , bg = "black" ,
font = "Times 10" , relief = GROOVE ,
width = 10 , height = 1 , fg = "white" )
self.text_box.pack ( expand = True , fill = BOTH )
self.text_box_scrollbar.config ( command = self.text_box.yview )
self.entry_frame = Frame ( self.master , bd = 1 , bg = "black" )
self.entry_frame.pack ( side = LEFT , fill = BOTH , expand = True )
self.entry_field = Entry ( self.entry_frame , bd = 1 , justify = LEFT )
self.entry_field.pack ( fill = X , padx = 6 , pady = 6 , ipady = 3 )
self.upload_button_frame = Frame ( self.master , bd = 0 , bg = 'black' )
self.upload_button_frame.pack ( fill = BOTH )
self.pic_button = Button ( self.upload_button_frame , text = 'upload' ,
relief = GROOVE ,
borderwidth = 0 , bg = 'slateblue2' , bd = 0 , fg = "Black" ,
command = lambda : self.open_img ( ) , activebackground = "black" ,
activeforeground = "#000000" )
self.pic_button.pack ( in_ = self.upload_button_frame , side = RIGHT )
self.master.bind ( "<Return>" , self.open_img )
self.pictccp = t.CreateToolTip ( self.pic_button , "Click here to share pictures" )
def open_img ( self ) :
global img
tag = "imageclick"
index = "2.0"
f_types = [ ('Jpg Files' , '*.jpg') , ('Png Files' , '*.png') , ('Jpeg Files' , '*.jpeg') ]
filename = filedialog.askopenfilename ( filetypes = f_types )
file = pathlib.PurePath ( filename )
fop = open ( file , "rb" )
img = PIL.Image.open ( filename )
img_resized = img.resize ( (200 , 100) ) # new width & height
img = PIL.ImageTk.PhotoImage ( img_resized )
self.text_box.configure ( state = NORMAL )
self.text_box.image_create ( END , image = img )
fop = open ( file , "rb" )
def onclick ( ) :
os.startfile ( file , 'open' )
self.text_box.insert ( END , "\n" )
self.text_box.bind ( "<Button-1>" , onclick ( ) )
self.text_box.tag_configure ( "right" , justify = 'right' )
self.text_box.tag_configure ( "right" , foreground = 'slate blue2' )
self.text_box.tag_add ( "right" , 1.0 , "end" )
self.text_box.configure ( state = DISABLED )
self.text_box.see ( END )
self.imglist.append ( img )
def chating ( ) :
root = Tk()
a = ChatInterface ( root )
root.geometry ( "340x500" )
root.title ( "Chat" )
root.mainloop ( )
chating()