我是 100% 的編碼新手,幾天前才開始遵循一些指南。我現在正在為我的桌面制作一個數字時鐘,我想將我的背景更改為我擁有的自定義影像。
有人可以向我解釋我可以在哪里更改它,以便背景成為我擁有的影像嗎?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()
uj5u.com熱心網友回復:
您可以嘗試影像路徑和背景影像概念。首先你必須選擇影像,然后是它的大小和它的位置。
IMAGE_PATH = 'PicNameHere.png' 寬度、高度 = 200、50
bkrgframe = BkgrFrame(root, IMAGE_PATH, WIDTH, HEIGTH) bkrgframe.pack()
代碼示例如下。
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()
uj5u.com熱心網友回復:
Label您可以使用text,image和compound選項在小部件中顯示影像和文本:
img_tk = ImageTk.PhotoImage(file="path/to/image.png")
digi_clock = Label(root, font=("Arial",50), fg="red", image=img_tk, compound="center")
digi_clock.pack()

uj5u.com熱心網友回復:
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/532096.html
