grid_forget 或 grid_remove 不会隐藏画布上的所有小部件

时间:2021-05-29 03:47:05

标签: tkinter tkinter-canvas

这不是实际的代码,我删除了许多项目只是为了分享完整的代码来重现问题。 以下是重现问题的步骤。

  1. 选中/取消选中启用配置文件图像复选框。(启用或禁用工作正常)
  2. 单击“设置”按钮并重试取消选中复选框以从 Canvas 隐藏项目。这次它只是删除了图片而不是画布上的所有项目。当有人取消选中复选框时,我希望一切都消失。

这里的问题是,当第一次在 init 中加载设置功能时,它工作得很好,但是如果您单击任何其他按钮并再次返回设置以隐藏画布项目,它只是隐藏图像而不是画布上的其他两个项目。

import os
from pathlib import Path
import tkinter as tk
from PIL import Image, ImageTk

class AppLayout(tk.Tk):
    def __init__(self,path,BackGround_LeftPane,BackGround_RightPane):
        tk.Tk.__init__(self)
        self.BackGround_LeftPane            = BackGround_LeftPane
        self.BackGround_RightPane           = BackGround_RightPane

        self.path = path
        self.ProfileImagePath = ''
        #Open Application on Full screen
        self.state('zoomed')
        
        self.masterPane = tk.PanedWindow(self,bd=0,sashwidth =0 )
        self.leftPane   = tk.Frame(self.masterPane,bg = self.BackGround_LeftPane,height = 600,width = 400,relief = 'raised')
        self.masterPane.add(self.leftPane)
        
        self.rightPane   = tk.Frame(self.masterPane,bg = self.BackGround_RightPane)
        self.masterPane.add(self.rightPane)
        
        self.masterPane.pack(fill = 'both',expand = True)
        
        self.isProfileImageEnabled =0
   
        self.AddButtonsToLeftPane()
        
        self.Settings()


    def InfraChecks(self):
   
        self.InfraChecks_Frame = tk.Frame(self.rightPane,bg =self.BackGround_RightPane)
        self.MenuName_Label = tk.Label(self.InfraChecks_Frame,text = 'Infra Checks',relief ='raised',
                                       bg =self.BackGround_RightPane,fg ='white',bd=0,font=("Arial", 15,'bold','underline'))
        self.MenuName_Label.grid(row = 1,column =0,sticky ='wn',padx=20,pady =(10,0))
        self.InfraChecks_Frame.grid(row=0,column =0,sticky ='nsew')
        

        
    def History(self):
     
        self.History_Frame = tk.Frame(self.rightPane,bg =self.BackGround_RightPane)
        self.MenuName_Label = tk.Label(self.History_Frame,text = 'About',relief ='raised',
                                       bg =self.BackGround_RightPane,fg ='white',bd=0,font=("Arial", 15,'bold','underline'))
        self.MenuName_Label.grid(row = 1,column =0,sticky ='wn',padx=20,pady =(10,0))
        self.History_Frame.grid(row=0,column =0,sticky ='nsew')
        
    
    def Settings(self):
        self.isProfileImageOn =tk.IntVar()
        
        def enableProfileImage():
            if self.isProfileImageOn.get()==1:
                try:
                    self.profileImage()
                except:
                    print("Image Update Error")
            else:
                self.canvas.grid_forget()
 
        self.Settings_Frame = tk.Frame(self.rightPane,bg =self.BackGround_RightPane)
        self.MenuName_Label = tk.Label(self.Settings_Frame,text = 'Settings',relief ='raised',
                                       bg =self.BackGround_RightPane,fg ='white',bd=0,font=("Arial", 15,'bold','underline'))
        
        self.SettingBtn1 = tk.Checkbutton(self.Settings_Frame, text='Enable Profile Image',variable=self.isProfileImageOn, onvalue=1, offvalue=0, command=enableProfileImage)
        
    
        
        if self.isProfileImageEnabled ==1:
            self.profileImage()
            self.SettingBtn1.select()
        else:
            self.SettingBtn1.deselect()
            
        
        self.MenuName_Label.grid(row = 1,column =0,sticky ='wn',padx=20,pady =(10,0))
        self.SettingBtn1.grid(row = 2,column =0)
        self.Settings_Frame.grid(row=0,column =0,sticky ='nsew')
   

    def profileImage(self):
        self.img_path = ('test.png')
        self.canvas = tk.Canvas(self.leftPane, width=200, height=180, borderwidth=0, highlightthickness=0, bg=self.BackGround_LeftPane)
              
        self.img = ImageTk.PhotoImage(Image.open(self.img_path).resize((150,150), Image.ANTIALIAS))
        self.image_on_canvas  = self.canvas.create_image(120, 110, image=self.img)
        
        self.canvas.create_oval(30,30,210,180,width =25,outline ='green')
        self.canvas.create_oval(0,0,240,210,width =80,outline='orange')
        self.canvas.grid(row=1,column=0,sticky='n')
        
    
    def AddButtonsToLeftPane(self):
        btn4 = tk.Button(self.leftPane, text = 'History        ',command = self.History,bg = self.BackGround_LeftPane,relief ='raised',
                   bd =4,padx =50,pady=10,font ='Arial',fg='white')
    
        btn5 = tk.Button(self.leftPane, text = 'Checks',command = self.InfraChecks,bg =self.BackGround_LeftPane,relief ='raised',
                  bd =4,padx =50,pady=10,font ='Arial',fg='white')
    
        btn6 = tk.Button(self.leftPane, text = 'Settings      ',command = self.Settings,bg =self.BackGround_LeftPane,relief ='raised',
                  bd =4,padx =50,pady=10,font ='Arial',fg='white')

        btn4.grid(row =5,column =0,sticky ='we',ipady=5,pady =(0,10))
        btn5.grid(row =5,column =0,sticky ='we',ipady=5,pady =(0,10))
        btn6.grid(row =6,column =0,sticky ='we',ipady=5)
        self.leftPane.rowconfigure(6, weight=1)

path = str(Path.home())

app = AppLayout(path,'red','green')

app.mainloop()

0 个答案:

没有答案
相关问题