tkinter初始界面中加入图片当作背景,点击按钮触发事件进入程序
时间: 2024-03-01 21:51:16 浏览: 20
好的,以下是一个简单的使用Tkinter实现初始化页面,加入图片作为背景,并点击按钮触发事件进入程序的示例代码:
```
import tkinter as tk
from PIL import ImageTk, Image
def enter_program():
# 在这里编写进入程序的代码
print("进入程序!")
root = tk.Tk()
root.title("初始化页面")
# 加载背景图片
image = Image.open("background.jpg")
photo = ImageTk.PhotoImage(image)
label = tk.Label(root, image=photo)
label.pack()
# 添加Button控件
button = tk.Button(root, text="进入程序", command=enter_program)
button.place(relx=0.5, rely=0.5, anchor="center")
root.mainloop()
```
在这个示例中,我们使用Pillow库(即Python Imaging Library)加载了一张名为“background.jpg”的图片,并将其作为Label控件的背景。我们还添加了一个Button控件,当用户单击“进入程序”按钮时,将调用enter_program()函数,您可以在这个函数中编写进入程序的代码。
请注意,在这个示例中,我们使用了place()布局管理器将“进入程序”按钮放置在窗口的中心位置。您可以根据需要使用其他布局管理器。
希望这个示例对您有所帮助!