How do I insert a JPEG image into a python Tkinter window?

前端 未结 3 897
我在风中等你
我在风中等你 2020-11-27 05:58

How do I insert a JPEG image into a Python 2.7 Tkinter window? What is wrong with the following code? The image is called Aaron.jpg.

#!/usr/bin/pyth         


        
相关标签:
3条回答
  • 2020-11-27 06:12
    from tkinter import *
    from PIL import ImageTk, Image
    
    window = Tk()
    window.geometry("1000x300")
    
    path = "1.jpg"
    
    image = PhotoImage(Image.open(path))
    
    panel = Label(window, image = image)
    
    panel.pack()
    
    window.mainloop()
    
    0 讨论(0)
  • 2020-11-27 06:19
    import tkinter as tk
    from tkinter import ttk
    from PIL import Image,  ImageTk
    win = tk. Tk()
    image1 = Image. open("Aoran. jpg")
    image2 =  ImageTk. PhotoImage(image1)
    image_label = ttk. Label(win , image =.image2)
    image_label.place(x = 0 , y = 0)
    win.mainloop()
    
    0 讨论(0)
  • 2020-11-27 06:23

    Try this:

    import tkinter as tk
    from PIL import ImageTk, Image
    
    #This creates the main window of an application
    window = tk.Tk()
    window.title("Join")
    window.geometry("300x300")
    window.configure(background='grey')
    
    path = "Aaron.jpg"
    
    #Creates a Tkinter-compatible photo image, which can be used everywhere Tkinter expects an image object.
    img = ImageTk.PhotoImage(Image.open(path))
    
    #The Label widget is a standard Tkinter widget used to display a text or image on the screen.
    panel = tk.Label(window, image = img)
    
    #The Pack geometry manager packs widgets in rows or columns.
    panel.pack(side = "bottom", fill = "both", expand = "yes")
    
    #Start the GUI
    window.mainloop()
    

    Related docs: ImageTk Module, Tkinter Label Widget, Tkinter Pack Geometry Manager

    0 讨论(0)
提交回复
热议问题