按下按钮后试图在tkinter中隐藏标签

时间:2019-05-25 20:48:53

标签: python-3.x opencv tkinter

我正在尝试在我的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()

我想隐藏所有这些标签,并在按下“预测”按钮后仅显示其中一个标签,但是当程序已经启动时,所有这些标签都应该被隐藏,因此我只应根据预测的类显示一个标签。 program image which starts with the label green/verde over all the labels

1 个答案:

答案 0 :(得分:1)

在这3个标签中,您都没有值,这是因为您正在将Label的方法place(..)返回值分配给这些Label的对象,例如为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)

现在可以隐藏标签。

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

img_label.config(image=ruta_pinton)

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

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()