label on top of image in python

2024/10/14 23:12:33

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 timeroot = 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

Answer

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://en.xdnf.cn/q/117898.html

Related Q&A

plot multiple graphs from multiple files gnuplot

I have a set of files named like this:qd-dPZ-z1-1nn.dat qd-dPZ-z2-1nn.dat qd-dPZ-z4-1nn.dat qd-dPZ-z8-1nn.dat qd-dPZ-z16-1nn.dat qd-dPZ-z32-1nn.dat qd-dPZ-z1-2nn.dat qd-dPZ-z2-2nn.dat qd-dPZ-z4…

Python writing to CSV... TypeError: coercing to Unicode: need string or buffer, file found

outputList is a list of lists. [ [a,b,c], [d,e,f], [g,h,i] ] and I want to output it to a csv file with each list as a separate row. Im getting this error TypeError: coercing to Unicode: need string or…

Preserve Signature in Decorator python 2

I am writing a decorator which will catch TypeError for incorrect number of arguments in a function call and will print a customised message. The code is here:import inspectdef inspect_signature(f):def…

Gimp: start script without image

Well, Im trying to write a python plug-in for Gimp, but it wont start without first loading an image... What can I do about that?

Pywinauto: how the `findbestmatch` module works?

Im trying to understand how the findbestmatch module works. Here is an example.from pywinauto.application import Application from pywinauto.findbestmatch import find_best_match ditto=Application().conn…

How to get the surface from a rect/line

I am trying to find the point where a line collides with a brick in the arkanoid that i am making. The most logical way i found is getting the mask from the line and use collidemask as it returns the p…

Python readin .txt and put in an array with numpy

i want to create an array with numpy. The base is a .txt file which is given in the following form:i tried it with loadtxt:data = np.loadtxt("myfile.txt",delimiter=\n,skiprows = 1)The first r…

Running a PyQt4 script without a display

I would like to run a Python script that normally opens a Qt window remotely over a connection with no X11 forwarding. Is there any way to create some kind of virtual display that the window drawing ca…

Scrapy : Program organization when interacting with secondary website

Im working with Scrapy 1.1 and I have a project where I have spider 1 scrape site A (where I aquire 90% of the information to fill my items). However depending on the results of the Site A scrape, I ma…

How do I use openpyxl and still maintain OOP structure?

I am using python to do some simulations and using openpyxl to generate the reports. Now the simulation is results are to be divided into several sheets of an excel file. By the principles of OOP my st…