简体   繁体   中英

label on top of image in python

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

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")

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM