label on top of image in python

混江龙づ霸主 提交于 2019-12-12 04:16:22

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/33460872/label-on-top-of-image-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!