在Tkinter中使用后隐藏按钮,尝试观看了这么多视频并尝试了所有方法,如:grid_forget()



程序应该在exel文件中搜索第一个和SEC名称的输入,如果孩子在那里,它将回复,但如果孩子不在那里,它将询问用户是否要添加他。一切都很好,但我无法隐藏"是">

yes=Button(text="yes",command=lambda: yesBot(ChildGym,firstNameLabel1,secNameLabel1))
yes.grid(row=4,column=4)

这是完整的代码给那些好奇的人;

import pandas as pd
from tkinter import *
import tkinter.messagebox
root=Tk()
ChildGym = pd.read_excel('TheGloriousEvolution.xlsx')


def findchild(): #check if child is inside excel file 
firstNameLabel1=firstNameEntry.get()
secNameLabel1=secNameEntry.get()


for  index, row in ChildGym.iterrows():
if not firstNameLabel1:
ans.configure(text="pls enter child first name")
answer=True


break

if not secNameLabel1:
ans.configure(text="pls enter child sec name ")
answer=True


break
if row['FirstName']==firstNameLabel1 and row['SecName']==secNameLabel1: 
ans.configure(text="all good!")
answer=True

break
else:
ans.configure(text="the kid is not here")

answer=False


if answer==False:

yes=Button(text="yes",command=lambda: yesBot(ChildGym,firstNameLabel1,secNameLabel1))
yes.grid(row=4,column=4)
nobot.grid(row=4,column=3)
askMan.grid(row=4,column=2)


else:       
answer=True





def yesBot(ChildGym,firstNameLabel1,secNameLabel1):
ChildGym_new_row = pd.DataFrame({ 'FirstName': [firstNameLabel1], 'SecName': [secNameLabel1] })
ChildGym=pd.concat([ChildGym,ChildGym_new_row], axis=0)
ans2.configure(text="kid got added succesfully!")
ChildGym.to_excel("TheGloriousEvolution.xlsx", index=False)   

def noBot():

askMan.grid_forget()
nobot.grid_forget()


# botton "click me" to do the def "find child"
Bot=Button(root,text="click me",command=findchild)
Bot.grid(row=0,column=4)

#label to ask if he wants to add the user
askMan=Label(text="do you want to add this child?")

#button to press no if you dont want to add this child
nobot=Button(root,text="no",command=noBot)
# label and entry for the first name
firstNameLabel=Label(text="what is the first name of the kid you want to search?")
firstNameLabel.grid(row=0,column=0)
firstNameEntry=Entry(root)
firstNameEntry.grid(row=0,column=1)

# labels to give answer if the kid is inside the excel file 
ans=Label(root)
ans.grid(row=0,column=2)
# labels to give answer if they added to the excel file
ans2=Label(root)
ans2.grid(row=4,column=1)

# label and entry for the second name 
secNameLabel=Label(text="what is the sec name of the kid you want to search?")
secNameLabel.grid(row=1,column=0)
secNameEntry=Entry(root)
secNameEntry.grid(row=1,column=1)



root.mainloop()

您的代码无法运行。请提供一个最小的、完整的、可验证的示例。

但是,您使用的名称是"yesbot"对于按钮和按钮回调函数,唯一的区别是情况。这可能是问题的根源。

不应该是这样的,我提供了一个例子,如何隐藏一个小部件可以做到。

from tkinter import *
root = Tk()
def hide_button():
yesbot.grid_forget()
yesbot = Button(text="yes", command=hide_button)
yesbot.grid(row=4,column=4)
root.mainloop()

相关内容

  • 没有找到相关文章