【问题标题】:Trying to hide a label in tkinter after pressed a button按下按钮后试图在 tkinter 中隐藏标签
【发布时间】:2019-05-25 20:48:52
【问题描述】:

我试图在我的 tkinter 代码中隐藏一些标签。 我正在加载图像,然后我现在应用了一些预处理技术。

情况是,我有三个标签被添加到名为“miframe”的小部件中,所有这些标签都在相同的坐标中:

labelmaduro = Label(miframe, image=ruta_maduro).place(x=25, y=60)
labelpinton = Label(miframe, image=ruta_pinton).place(x=25, y=60)
labelverde = Label(miframe, image=ruta_verde).place(x=25, y=60)

所有标签都被添加到另一个框架中,我正在寻找一种方法来在按下按钮“选择图像”后隐藏所有这些标签,当我按下按钮时调用“预测”显示例如“标签成熟”。 当程序已经启动时,我应该隐藏所有这些标签,但到那时我不能这样做, 我试过用label1.lower()place_forget()隐藏这些标签并显示它,我试过mylabel.pack()

from tkinter import * 
import cv2
from PIL import Image
from PIL import ImageTk
from tkinter import filedialog as fd
import numpy as np


def select_image():
    # grab a reference to the image panels
    global panelA, panelB
    # open a file chooser dialog and allow the user to select an input
    # image
    #ocultar clase predecida anteriormente:

    path = fd.askopenfilename()

        # ensure a file path was selected
    if len(path) > 0:
        # load the image from disk, convert it to grayscale, and detect
        # edges in it
        image = cv2.imread(path)
        median = cv2.medianBlur(image, 9)
        #Resize
        #BGT TO RGB
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        median = cv2.cvtColor(median, cv2.COLOR_BGR2RGB)
        dim = (340, 257)
        original = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
        median = cv2.resize(median, dim, interpolation = cv2.INTER_AREA)

        # Convertir imagenes al formato PIL
        original = Image.fromarray(original)
        median = Image.fromarray(median)

        #Pass to ImageTk format
        original = ImageTk.PhotoImage(original)
        median = ImageTk.PhotoImage(median)

        #if the panels are none
        if panelA is None or panelB is None:
            #El primer panel guarda la primera imagen
            panelA = Label(image=original)
            panelA.image = original
            panelA
            panelA.pack(side="left", padx=10, pady=10)

            #the second panel show the pre-processed image
            panelB = Label(image=median)
            panelB.image = median
            panelB.pack(side="right", padx=10, pady=10)

            hideLabels()
        #in other cases update the panels
        else:
            # update the pannels
            panelA.configure(image=original)
            panelB.configure(image=median)
            panelA.image = original
            panelB.image = median

            hideLabels()

def hideGreen():
    labelverde.place_forget()

def hideLabels():
    hideGreen()

def showMature():
    labelmaduro.pack() #show label after push Predict Button

#Initialize the main window
root = Tk()
root.configure(background='black')
root.title("Opencv test")
panelA = None
panelB = None

#Frame which contains the labels
miframe = Frame(bg="#0F2D80", width="200", height="200")

ruta_maduro = PhotoImage(file="maduro.png")
ruta_pinton = PhotoImage(file="pinton.png")
ruta_verde = PhotoImage(file="verde.png")

#Create labels to show and hidde according to the prediction
labelmaduro = Label(miframe, image=ruta_maduro).place(x=25, y=60)
labelpinton = Label(miframe, image=ruta_pinton).place(x=25, y=60)
labelverde = Label(miframe, image=ruta_verde).place(x=25, y=60)


#add frame to root
miframe.pack(side="right", anchor="n", padx=10)

#User buttons
btn2 = Button(root, text="Predict Button", command=showMature)
btn2.pack(side="bottom", fill="both", expand="yes", padx="10", pady="10")

btn = Button(root, text="Select Image", command=select_image)
btn.pack(side="bottom", fill="both", expand="yes", padx="10", pady="10")

root.resizable(0,0)
# kick off the GUI
root.mainloop()

我想隐藏所有这些标签并在按下“预测”按钮后只显示其中一个,但是当程序已经启动时,所有这些标签都应该被隐藏,因此我应该根据预测的类只显示一个标签。

【问题讨论】:

    标签: python-3.x opencv tkinter


    【解决方案1】:

    您在所有这 3 个标签中都有 None 值,这是因为您将标签的方法 place(..) 返回值分配给这些标签的对象,因为 place 返回 None (对于包或网格相同)

    总是这样,

    labelmaduro = Label(miframe, image=ruta_maduro)
    labelmaduro.place(x=25, y=60)
    

    或者,如果您不想要 Label 的对象并且只想分配它而不在代码中进一步修改它,那么您可以像这样使用它。

    Label(miframe, image=ruta_maduro).place(x=25, y=60)
    

    现在用于隐藏标签。

    您不需要三个标签来显示/隐藏来实现这种功能。可以通过修改现有 Labelimage 来完成,因此在这种情况下,您只需要一个 Label 并将其 image 资源配置为不同的资源,具体取决于您的需要这样...

    img_label.config(image=ruta_pinton)
    

    以下是从不同的Buttons 更改单个Label 的图像的示例。

    from tkinter import * 
    
    root = Tk()
    
    ruta_maduro = PhotoImage(file="maduro.png")
    ruta_pinton = PhotoImage(file="pinton.png")
    ruta_verde = PhotoImage(file="verde.png")
    
    img_label = Label(root)
    img_label.pack()
    
    Button(root, text='maduro', command=
        lambda: img_label.config(image=ruta_maduro)).pack()
    Button(root, text='maduro', command=
        lambda: img_label.config(image=ruta_pinton)).pack()
    Button(root, text='maduro', command=
        lambda: img_label.config(image=ruta_verde)).pack()
    
    mainloop()
    

    【讨论】:

    • 非常感谢 Saad,现在我可以解决这个问题了。我将这些标签放在一个框架中。我还将尝试改进界面的设计并最终实现 svm 分类器。
    猜你喜欢
    • 2016-09-02
    • 2019-04-14
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多