Home > Software design >  Add a custom image to my digital clock as background
Add a custom image to my digital clock as background

Time:11-11

im 100% new to coding and just started to follow some guides a few days ago. I am now making a digital clock for my desktop and i want to change my background to a custom image that i have.

Can someone explain to me where i can change it so that the background becomes my image that i have? BR

from tkinter import Tk
from tkinter import Label
import time
from PIL import Image, ImageTk


root = Tk()
root.title("Klocka")
root.attributes("-topmost", 1)
root.attributes('-alpha', 1)
root.iconbitmap('klocka.ico (1).ico')
root.geometry('600x400 50 50')


window_width = 255
window_height = 50

screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()

center_x = int(screen_width/3500 - window_width / 0.97)
center_y = int(screen_height/1 - window_height / 0.62)

root.geometry(f'{window_width}x{window_height} {center_x} {center_y}')


def present_time():
    display_time = time.strftime("%I:%M:%S")
    digi_clock.config(text=display_time)
    digi_clock.after(200,present_time)

digi_clock = Label(root, font=("Arial",50),bg="black",fg="red")
digi_clock.pack()

present_time()

root.mainloop()

CodePudding user response:

you can try image path and background image concept. First you have to choose image, then it's size and it's position.

IMAGE_PATH = 'PicNameHere.png' WIDTH, HEIGTH = 200, 50

bkrgframe = BkgrFrame(root, IMAGE_PATH, WIDTH, HEIGTH) bkrgframe.pack()

Code sample is below.

from tkinter import Tk
from tkinter import Label
import time
from PIL import Image, ImageTk

IMAGE_PATH = 'PicNameHere.png'
WIDTH, HEIGTH = 200, 50

root = Tk()
root.title("Klocka")
root.attributes("-topmost", 1)
root.attributes('-alpha', 1)
root.iconbitmap('klocka.ico (1).ico')
root.geometry('600x400 50 50')


window_width = 255
window_height = 50

screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()

center_x = int(screen_width/3500 - window_width / 0.97)
center_y = int(screen_height/1 - window_height / 0.62)

root.geometry(f'{window_width}x{window_height} {center_x} {center_y}')

bkrgframe = BkgrFrame(root, IMAGE_PATH, WIDTH, HEIGTH)
bkrgframe.pack()

def present_time():
    display_time = time.strftime("%I:%M:%S")
    digi_clock.config(text=display_time)
    digi_clock.after(200,present_time)

digi_clock = Label(root, font=("Arial",50),bg="black",fg="red")
digi_clock.pack()

present_time()

root.mainloop()

CodePudding user response:

from tkinter import Tk
from tkinter import Label
import time
from PIL import Image, ImageTk



root = Tk()
root.title("Klocka")
root.attributes("-topmost", 1)
root.attributes('-alpha', 1)
root.iconbitmap('klocka.ico (1).ico')
root.geometry('600x400 50 50')

bg = PhotoImage(file = "Your_img.png")
canvas1 = Canvas( root, width = 400,
                 height = 400)
  
canvas1.pack(fill = "both", expand = True)
canvas1.create_image( 0, 0, image = bg, 
                     anchor = "nw")
window_width = 255
window_height = 50

screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()

center_x = int(screen_width/3500 - window_width / 0.97)
center_y = int(screen_height/1 - window_height / 0.62)

root.geometry(f'{window_width}x{window_height} {center_x} {center_y}')


def present_time():
    display_time = time.strftime("%I:%M:%S")
    digi_clock.config(text=display_time)
    digi_clock.after(200,present_time)

digi_clock = Label(root, font=("Arial",50),bg="black",fg="red")
digi_clock.pack()

present_time()

root.mainloop()
  • Related