如何在通过askopenfilename插入的labelframe中显示图像?

时间:2018-12-13 17:34:50

标签: python tkinter

我是python的初学者,度过了艰难的时光。无法在标签框中显示图像。 (PS-函数OpenFile的语法正确。在这里,我无法正确缩进它:P)

import tkinter as tk
from tkinter import ttk
from tkinter import *
from tkinter.filedialog import askopenfilename
from PIL import ImageTk,Image

root=tk.Tk()
root.title("python gui")
root.geometry("1200x600")

def OpenFile():
    name=askopenfilename(initialdir="C:/Users/Batman/Documents/Programming/tkinter/",
                       filetypes =(("JPEG FILE", "*.jpg"),("PNG FilES","*.png"),("bitmap","*.bmp")),
                       title = "Choose a file."
                       )
    print (name)

    try:
        with open(name,'r') as UseFile:
            print(UseFile.read())
    except:
        print("No file exists")

buttone=Button(embedtab,text="select image",command=OpenFile)
buttone.grid(row=1,column=0)

labelframe3=tk.LabelFrame(embedtab, text="originsal image", padx=100,pady=100)
labelframe3.grid(column=0,row=0)
ttk.Label(labelframe3, text="original image will display here").grid(column=0, row=0, sticky=tk.W)

1 个答案:

答案 0 :(得分:0)

您不清楚case 'REMOVE_FROM_CART': const updatedProductList = state.productList.map( (product) => { if(product.id === payload.product.id){ // this is the item we care about return { ...product, // this will avoid a state mutation qty : product.qty - payload.qty // update the qty } }else { // just return the element as it is return cartItem } }) return updatedCartItemList; break; default : return state 函数如何工作。为什么不让Pillow打开图像,请参见我的示例。

在我的示例中,我正在创建标签以首先保留图像。我将其配置为仅在有图像要显示时才显示图像。

当我在包含的标签中显示图像时,我将标签框架设置为特定大小,并使用OpenFile()来防止其更改大小。

图像出现在标签框的左上角。您可以通过根据图像大小等计算适当的填充来更改此设置。

这是你的追求吗?

grid_propagate()