📜  如何在 Tkinter 中使用图像作为背景?

📅  最后修改于: 2022-05-13 01:55:34.609000             🧑  作者: Mango

如何在 Tkinter 中使用图像作为背景?

先决条件Python GUI – tkinter , Frame

在本文中,我们将编写一个在后台使用图像的程序。在 Tkinter 中,没有内置的图像函数,因此可以将其用作背景图像。它可以通过多种方法完成:

方法 1:使用照片图像方法。

对于基于 GUI 的应用程序,图像起着至关重要的作用。从应用程序图标到动画,它很有用。

为了在标签、按钮、画布和文本小部件中显示图像,使用了PhotoImage类,它存在于 Tkinter 包中。

代码:

Python3
# Import module 
from tkinter import *
  
# Create object 
root = Tk()
  
# Adjust size 
root.geometry("400x400")
  
# Add image file
bg = PhotoImage(file = "Your_image.png")
  
# Show image using label
label1 = Label( root, image = bg)
label1.place(x = 0, y = 0)
  
label2 = Label( root, text = "Welcome")
label2.pack(pady = 50)
  
# Create Frame
frame1 = Frame(root)
frame1.pack(pady = 20 )
  
# Add buttons
button1 = Button(frame1,text="Exit")
button1.pack(pady=20)
  
button2 = Button( frame1, text = "Start")
button2.pack(pady = 20)
  
button3 = Button( frame1, text = "Reset")
button3.pack(pady = 20)
  
# Execute tkinter
root.mainloop()


Python3
# Import module 
from tkinter import *
  
# Create object 
root = Tk()
  
# Adjust size 
root.geometry("400x400")
  
# Add image file
bg = PhotoImage( file = "Your_img.png")
  
# Show image using label
label1 = Label( root, image = bg)
label1.place(x = 0,y = 0)
  
# Add text
label2 = Label( root, text = "Welcome",
               bg = "#88cffa")
  
label2.pack(pady = 50)
  
# Create Frame
frame1 = Frame( root, bg = "#88cffa")
frame1.pack(pady = 20)
  
# Add buttons
button1 = Button( frame1, text = "Exit")
button1.pack(pady = 20)
  
button2 = Button( frame1, text = "Start")
button2.pack(pady = 20)
  
button3 = Button( frame1, text = "Reset")
button3.pack(pady = 20)
  
# Execute tkinter
root.mainloop()


Python3
# Import module 
from tkinter import *
  
# Create object 
root = Tk()
  
# Adjust size 
root.geometry("400x400")
  
# Add image file
bg = PhotoImage(file = "Your_img.png")
  
# Create Canvas
canvas1 = Canvas( root, width = 400,
                 height = 400)
  
canvas1.pack(fill = "both", expand = True)
  
# Display image
canvas1.create_image( 0, 0, image = bg, 
                     anchor = "nw")
  
# Add Text
canvas1.create_text( 200, 250, text = "Welcome")
  
# Create Buttons
button1 = Button( root, text = "Exit")
button3 = Button( root, text = "Start")
button2 = Button( root, text = "Reset")
  
# Display Buttons
button1_canvas = canvas1.create_window( 100, 10, 
                                       anchor = "nw",
                                       window = button1)
  
button2_canvas = canvas1.create_window( 100, 40,
                                       anchor = "nw",
                                       window = button2)
  
button3_canvas = canvas1.create_window( 100, 70, anchor = "nw",
                                       window = button3)
  
# Execute tkinter
root.mainloop()


输出:

如您所见,按钮和标签的背景颜色与图像颜色不同。

解决办法是设置按钮的背景色,并用“#88cffa”这个颜色标记为图片的颜色。

蟒蛇3

# Import module 
from tkinter import *
  
# Create object 
root = Tk()
  
# Adjust size 
root.geometry("400x400")
  
# Add image file
bg = PhotoImage( file = "Your_img.png")
  
# Show image using label
label1 = Label( root, image = bg)
label1.place(x = 0,y = 0)
  
# Add text
label2 = Label( root, text = "Welcome",
               bg = "#88cffa")
  
label2.pack(pady = 50)
  
# Create Frame
frame1 = Frame( root, bg = "#88cffa")
frame1.pack(pady = 20)
  
# Add buttons
button1 = Button( frame1, text = "Exit")
button1.pack(pady = 20)
  
button2 = Button( frame1, text = "Start")
button2.pack(pady = 20)
  
button3 = Button( frame1, text = "Reset")
button3.pack(pady = 20)
  
# Execute tkinter
root.mainloop()

输出:

注意:此方法不适用于图像中的多种颜色。

方法 2:使用 Canvas 方法。

方法:

  • 与上面的实现相同。
  • 添加图像文件。
  • 创建 Canvas 并设置宽度和高度。
  • 使用 create_image 显示图像。
  • 使用 create_text 设置文本。
  • 创建按钮。
  • 最后一步使用 create_window 添加按钮。

代码:

蟒蛇3

# Import module 
from tkinter import *
  
# Create object 
root = Tk()
  
# Adjust size 
root.geometry("400x400")
  
# Add image file
bg = PhotoImage(file = "Your_img.png")
  
# Create Canvas
canvas1 = Canvas( root, width = 400,
                 height = 400)
  
canvas1.pack(fill = "both", expand = True)
  
# Display image
canvas1.create_image( 0, 0, image = bg, 
                     anchor = "nw")
  
# Add Text
canvas1.create_text( 200, 250, text = "Welcome")
  
# Create Buttons
button1 = Button( root, text = "Exit")
button3 = Button( root, text = "Start")
button2 = Button( root, text = "Reset")
  
# Display Buttons
button1_canvas = canvas1.create_window( 100, 10, 
                                       anchor = "nw",
                                       window = button1)
  
button2_canvas = canvas1.create_window( 100, 40,
                                       anchor = "nw",
                                       window = button2)
  
button3_canvas = canvas1.create_window( 100, 70, anchor = "nw",
                                       window = button3)
  
# Execute tkinter
root.mainloop()

输出: