如何在 tkinter 中將影像用作背景?
tkinter 中的背景影像用途廣泛,其功能可用於製作 3D、2D 遊戲、螢幕保護程式、桌面視覺化軟體等。Tkinter 畫布用於在應用程式中使用所有這些功能。
示例
在此示例中,我們將在畫布小部件中使用 create_image() 方法新增背景影像。
#Import the required library
from tkinter import *
from PIL import Image, ImageTk
from tkinter import ttk
#Create an instance of tkinter window
win= Tk()
#Define the geometry of the window
win.geometry("750x650")
#Load the image
bg= ImageTk.PhotoImage(file="./tutorialspoint.png")
#Create a canvas
canvas= Canvas(win,width= 400, height= 200)
canvas.pack(expand=True, fill= BOTH)
#Add the image in the canvas
canvas.create_image(0,0,image=bg, anchor="nw")
#Add a text in canvas
canvas.create_text(310,550,text="</Hello,Devs!_", font= ('Courier 45 bold'))
win.mainloop()輸出
現在,執行以上程式碼以顯示一個視窗,其中包含一個帶有一定文字的背景影像。

廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP