0

I am trying to display text on top of an image. Right now the text is below the image or if I put a row=0 in the grid disappears. I am assuming it is behind the image. I can't seem to get it to work. My code is:

from Tkinter import *
from PIL import ImageTk
import csv
import time

root = Tk()

image = ImageTk.PhotoImage(file='C:\Users\Shawn\PycharmProjects\Test\Background.gif')

var0 = StringVar()
var1 = StringVar()
var2 = StringVar()
var3 = StringVar()

label0 = Label(root, fg="white", textvariable=var0)
label1 = Label(root, fg="white", textvariable=var1)
label2 = Label(root, fg="white", textvariable=var2)
label3 = Label(root, fg="white", textvariable=var3)

# ----------------------------------------------------------------------
def csv_dict_reader(file_obj):
    reader = csv.DictReader(file_obj, delimiter=',')
    for column in reader:
        if (column["Week"]) == (time.strftime("%U")):
            var0.set(column["Parashat"])
            var1.set(column["Torah"])
            var2.set(column["Haftarah"])
            var3.set(column["Gospel"])

# ----------------------------------------------------------------------
if __name__ == "__main__":
    with open("Torah.csv") as f_obj:
        csv_dict_reader(f_obj)

Label(root, image=image).grid(row=0, column=0)
label0.grid()
label1.grid()
label2.grid()
label3.grid()
mainloop()

I have tried putting the row and column in the label0.grid(row=1, column=1), along with all labels and all row and column numbers.

The outcome I want is label0 - label3 to be centered on top of the image. The image is black and dark blue so the white text will show well. Thank you.

Shawn

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
sdruch
  • 1
  • 1
  • 3

2 Answers2

0

You can do this by adding the text option to the label with your photo in. Then look at the compound feature to format it. It would look like this:

label=Label(image=image, text="Text", compound="center")
Jonah Fleming
  • 1,187
  • 4
  • 19
  • 31
  • I have tried that, but since I am labeling using an if statement all i get is numbers and not the data I am looking for. Its not static text that I am using, but data called from a .csv file that depends on the week what data is shown. I tried: – sdruch Nov 02 '15 at 01:35
  • I have tried that, but since I am labeling using an if statement all i get is numbers and not the data I am looking for. Its not static text that I am using, but data called from a .csv file that depends on the week what data is shown. I tried: label=Label(image=image, text=text0, compound="center") and my result is: the picture with .52036512 on it when it is supposed to be the picture with Chayei Sarah. Just not sure what I am doing wrong. – sdruch Nov 02 '15 at 01:46
0

In the below code the orgin postion is set to (0,0) it belong to the top left corner

import cv2
font = cv2.FONT_HERSHEY_SIMPLEX
orgin = (0, 0) 
fontScale = 1
color = (255, 0, 0) 
thickness = 2
image = cv2.putText(image, 'OpenCV', orgin, font,  
               fontScale, color, thickness, cv2.LINE_AA) 
Kukesh
  • 490
  • 1
  • 5
  • 18