weixin_51695958 2022-10-22 22:48 采纳率: 40%
浏览 21
已结题

点击任意按钮,上一个按钮插入的图片仍然存在

问题遇到的现象和发生背景
用代码块功能插入代码,请勿粘贴截图
import tkinter

import cv2
from PIL import Image, ImageTk

topwin = tkinter.Tk()         #创建顶层窗口
topwin.geometry('1000x600')     #初始化窗口大小
topwin.title('实验一') #设置窗口标题

img_open = Image.open('computer.jpg')
# img = ImageTk.PhotoImage(img_open)
img = ImageTk.PhotoImage(img_open.resize((250,300),Image.ANTIALIAS))

def SoucerImg():
    label = tkinter.Label(topwin,
                          text='Rice原图',
                          image=img,
                          font=('黑体', 20),
                          fg='red',  # 字体颜色
                          compound=tkinter.BOTTOM  # 设置文本和图像的混合模式
                          )
    label.pack(side=tkinter.LEFT)



def Laplacianshow():
    label = tkinter.Label(topwin,
                          text='Laplacian算子',
                          image=imgout_1,
                          font=('黑体', 20),
                          fg='red',  # 字体颜色
                          compound=tkinter.BOTTOM  # 设置文本和图像的混合模式
                          )
    label.pack(side=tkinter.LEFT)

def Laplacian():
    # 灰度化处理图像
    grayImage = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(grayImage, (3, 3), 0)
    dst = cv2.Laplacian(grayImage, cv2.CV_16S, ksize=3)
    Laplacian = cv2.convertScaleAbs(dst)
    cv2.imwrite('Laplacianimg.png',Laplacian)

def LoG():
    # 灰度化处理图像
    grayImage = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(grayImage, (3, 3), 0)
    # LoG算法
    dst1 = cv2.Laplacian(blurred, cv2.CV_16S, ksize=3)
    LoG = cv2.convertScaleAbs(dst1)
    cv2.imwrite('LoGimg.png',LoG)

def LoGshow():
    label = tkinter.Label(topwin,
                          text='LoG算子',
                          image=imgout_2,
                          font=('黑体', 20),
                          fg='red',  # 字体颜色
                          compound=tkinter.BOTTOM  # 设置文本和图像的混合模式
                          )
    label.pack(side=tkinter.LEFT)

def Canny():
    # 灰度化处理图像
    grayImage = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(grayImage, (3, 3), 0)
    # LoG算法
    dst2 = cv2.Canny(blurred, 20, 40)
    Canny = cv2.convertScaleAbs(dst2)
    cv2.imwrite('Cannyimg.png',Canny)

def Cannyshow():
    label = tkinter.Label(topwin,
                          text='Canny算子',
                          image=imgout_3,
                          font=('黑体', 20),
                          fg='red',  # 字体颜色
                          compound=tkinter.BOTTOM  # 设置文本和图像的混合模式
                          )
    label.pack(side=tkinter.LEFT)


# 读取图像
img1 = cv2.imread('computer.jpg')
lenna_img = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)# 灰度化处理图像
Laplacian()
imgout1=Image.open("Laplacianimg.png")
imgout_1 = ImageTk.PhotoImage(imgout1.resize((250,300),Image.ANTIALIAS))
LoG()
imgout2=Image.open("LoGimg.png")
imgout_2 = ImageTk.PhotoImage(imgout2.resize((250,300),Image.ANTIALIAS))
Canny()
imgout3=Image.open("LoGimg.png")
imgout_3 = ImageTk.PhotoImage(imgout3.resize((250,300),Image.ANTIALIAS))





button1 = tkinter.Button(topwin, text ="原图",command = SoucerImg,width=10)
button1.place(x=0,y=0)

button2 = tkinter.Button(topwin, text ="Laplacian算子",command = Laplacianshow,width=10)
button2.place(x=80,y=0)

button3 = tkinter.Button(topwin, text ="LoG算子",command = LoGshow,width=10)
button3.place(x=160,y=0)

button4 = tkinter.Button(topwin, text ="Canny算子",command = Cannyshow,width=10)
button4.place(x=240,y=0)



button5=tkinter.Button(topwin,text='退出',command=topwin.quit,width=10)
button5.place(x=320,y=0)



topwin.mainloop()#显示窗口

运行结果及报错内容

img

我想要达到的结果

按任意按钮只显示当前按钮下插入的图片

  • 写回答

2条回答 默认 最新

  • 请叫我问哥 Python领域新星创作者 2022-10-23 21:33
    关注

    简单啊,首先布局要想好,看你的意思,应该是原图是位置不动,其他三个效果图是按哪个就显示哪个对吧?那我们就需要先放两个空白的label占位,然后点击按钮的时候往label里塞图片和文字就可以了。增加两个label组件,并把那几个按钮对应的函数改成下面这样:

    label1 = tkinter.Label(topwin,
                              font=('黑体', 20),
                              fg='red',  # 字体颜色
                              compound=tkinter.BOTTOM  # 设置文本和图像的混合模式
                              )
    label1.pack(side=tkinter.LEFT)
    label2 = tkinter.Label(topwin,
                              font=('黑体', 20),
                              fg='red',  # 字体颜色
                              compound=tkinter.BOTTOM  # 设置文本和图像的混合模式
                              )
    label2.pack(side=tkinter.LEFT)
    def SoucerImg():
        label1['text']='Rice原图'
        label1['image']=img
     
    def Laplacianshow():
        label2['text']='Laplacian算子'
        label2['image']=imgout_1
    
    def LoGshow():
        label2['text']='LoG算子'
        label2['image']=imgout_2
     
    def Cannyshow():
        label2['text']='Canny算子'
        label2['image']=imgout_3
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月5日
  • 已采纳回答 10月28日
  • 创建了问题 10月22日

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂