zoukankan      html  css  js  c++  java
  • 使用tkinter加载png,jpg

    最近来使用tkinter加载图片时遇到了困难,按照资料写了

    photo = PhotoImage(file='ques.png')
    imglabel = Label(root, image=photo)
    imglabel.grid(row=0, column=0, columnspan=3)

    却意外报错

    经过多种尝试无果,最后发现tkinter是只支持gif的格式,如果要加载png或者jpg的话就要使用PIL模块

    from Tkinter import *
    from PIL import Image, ImageTk
    
    root = Tk()
    root.title('测试组python毕业题')
    
    img = Image.open('ques.png')  # 打开图片
    photo = ImageTk.PhotoImage(img)  # 用PIL模块的PhotoImage打开
    imglabel = Label(root, image=photo)
    imglabel.grid(row=0, column=0, columnspan=3)
    
    Label(root, text="Answer:").grid(row=1, column=0, sticky=S + N)
    
    answerEntry = Entry(root)
    btn = Button(root, text="Submit", command=submit)
    
    answerEntry.grid(row=1, column=1)
    btn.grid(row=1, column=2)
    
    mainloop()

     最后正确显示了png图片

  • 相关阅读:
    五、MapReduce 发布服务
    四、MapReduce 基础
    三、Hadoop 的 API
    二、HDFS 架构
    php身份证号的验证
    php性能优化
    PHP网站开发方案
    php一个不错的分页
    2013年最流行的php框架盘点
    程序员之路
  • 原文地址:https://www.cnblogs.com/semishigure/p/8124397.html
Copyright © 2011-2022 走看看