SOHAM DAS BISWAS.
SOHAM DAS BISWAS.

Reputation: 71

How to set png file as background and use Label in tkinter gui?

I am trying to set image as background. But I am facing problem like I can't put Label on that gui of tkinter

Here is my code:-

from tkinter import *

root=Tk()
root.title("SOHAM MAIL SENDER")
root.iconbitmap("F:\\PYTHON PROJECTS\\GMAIL\\img\\Mcdo-Design-Letter-Letter- 
GMail-pen.ico")

root.geometry("900x680")
file = PhotoImage(file = "F:\\PYTHON PROJECTS\\GMAIL\img\\gradient_2.png")
img = Label(root, image=file)
img.place(x=0, y=0, relwidth=1, relheight=1)
img.pack()
def time():
    string = strftime('%I:%M:%S %p')
    label.config(text=string)
    label.after(1000, time)
# time
label = Label(root, font=("ds-digital",35),  background= "#B7C3F9", 
foreground= "white")
time()
label.pack(side=TOP, pady=40)

root.mainloop()

Upvotes: 0

Views: 381

Answers (2)

acw1668
acw1668

Reputation: 47219

You can use one label for both the image background and the time:

import tkinter as tk
from time import strftime

root = tk.Tk()
root.title("SOHAM MAIL SENDER")
root.iconbitmap("F:\\PYTHON PROJECTS\\GMAIL\\img\\Mcdo-Design-Letter-Letter-GMail-pen.ico")
root.geometry("900x680")

image = tk.PhotoImage(file="F:\\PYTHON PROJECTS\\GMAIL\img\\gradient_2.png")
label = tk.Label(root, image=image, compound='c', font=("ds-digital", 35), fg='white')
label.place(x=0, y=0, relwidth=1, relheight=1)

def show_time():
    cur_time = strftime('%I:%M:%S %p')
    label.config(text=cur_time)
    label.after(1000, show_time)

show_time()

root.mainloop()

Upvotes: 0

Mike67
Mike67

Reputation: 11342

When you create the time label, use the background label as the parent. Also remove the pack call since it minimizes borders around widgets.

Try this code:

from tkinter import *
import datetime

root=Tk()
root.title("SOHAM MAIL SENDER")
root.iconbitmap("F:\\PYTHON PROJECTS\\GMAIL\\img\\Mcdo-Design-Letter-Letter-GMail-pen.ico")

root.geometry("900x680")
file = PhotoImage(file = "F:\\PYTHON PROJECTS\\GMAIL\img\\gradient_2.png")
img = Label(root, image=file)
img.place(x=0, y=0, relwidth=1, relheight=1)
#img.pack()
def time():
    string = datetime.datetime.now().strftime('%I:%M:%S %p')
    label.config(text=string)
    label.after(1000, time)
# time
label = Label(img, font=("ds-digital",35),  background= "#B7C3F9", foreground= "white")  # set background as parent
label.place(x=450, y=340, relwidth=.5, relheight=.1, anchor="center")
time()
#label.pack(side=TOP, pady=40)

root.mainloop()

Output (my background)

Time

Upvotes: 2

Related Questions