tkinter 查入背景图
时间: 2023-09-12 19:03:20 浏览: 67
在 Tkinter 中插入背景图可以通过以下步骤实现:
1. 导入 Tkinter 和 PIL 库
```python
from tkinter import *
from PIL import ImageTk, Image
```
2. 创建 Tkinter 窗口
```python
root = Tk()
root.geometry("500x500")
```
3. 打开并读取图片
```python
image = Image.open("image_name.jpg")
```
4. 将图片转换为 Tkinter 图像对象
```python
background_image = ImageTk.PhotoImage(image)
```
5. 将图片放置在标签中
```python
background_label = Label(root, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
```
6. 运行程序
```python
root.mainloop()
```
完整代码示例:
```python
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root.geometry("500x500")
image = Image.open("image_name.jpg")
background_image = ImageTk.PhotoImage(image)
background_label = Label(root, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
root.mainloop()
```
阅读全文