简体   繁体   English

python中图像顶部的标签

[英]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.现在文本在图像下方,或者如果我在网格中放置 row=0 就会消失。 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.我尝试将行和列以及所有标签和所有行和列号放在 label0.grid(row=1, column=1) 中。

The outcome I want is label0 - label3 to be centered on top of the image.我想要的结果是 label0 - label3 以图像顶部为中心。 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在下面的代码中,原点位置设置为 (0,0) 它属于左上角

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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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