Tkinter grid#u遗忘或grid#u移除不';不要隐藏画布上的所有小部件
这不是实际的代码,我删除了许多项只是为了共享完整的代码来重新创建问题。 以下是重新创建问题的步骤Tkinter grid#u遗忘或grid#u移除不';不要隐藏画布上的所有小部件,tkinter,tkinter-canvas,Tkinter,Tkinter Canvas,这不是实际的代码,我删除了许多项只是为了共享完整的代码来重新创建问题。 以下是重新创建问题的步骤 选中/取消选中启用配置文件图像复选框。(启用或禁用效果非常好) 单击“设置”按钮,然后重试以取消选中复选框以从画布隐藏项目。这一次,它只是删除了图片,而不是画布上的所有项目。我希望当有人取消选中复选框时,所有内容都消失 这里的问题是,当设置函数第一次加载到init中时,它工作得非常好,但是如果您单击任何其他按钮并再次返回设置以隐藏画布项目,它只会隐藏图像,而不会隐藏画布上的其他两个项目 import
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()
grid\u forget
只会忘记添加了grid的项目。它对画布上绘制的对象一无所知。你知道吗?你用新对象覆盖了你的一些小部件(例如:方法.History
和.Settings
中的self.MenuName\u Label
)。因此,对原始窗口小部件的引用将丢失,并且无法再删除或更改它们。每个小部件只定义一次,不要重新启动它们。@MartinWettstein,很抱歉我是python/tkinter新手,我不明白你说我覆盖了其他菜单中的几个小部件。self.MenuName_标签和我在右窗格中编写的其他小部件。我试图保留这些对象的不同名称。设置,-历史记录,但仍然是同一个问题。@MartinWettstein在多次阅读您的评论后,我明白我把事情搞砸了。每次我点击设置或任何其他按钮时,它都会为每个小部件创建一个新的对象,因为我保持对象名称不变,所以它会覆盖它。为了处理这种情况,我在init方法中创建了self.objectcreated=False变量,并在创建对象时将其打开,以避免一次又一次地创建相同的对象。有没有更好的办法?不用说你帮了大忙我很高兴它成功了。您还可以在一开始就创建所有小部件,并在需要时将它们放置在网格中并从网格中移除。或者在创建一个新的之前尝试销毁它们。但使用全局设置也可以。