简体   繁体   English

在 Tkinter 中使用后隐藏一个按钮,尝试观看这么多视频并尝试了以下所有操作:grid_forget()

[英]hide a button after use in Tkinter, tried watching so many videos and tried everything like: grid_forget()

the program supposed to search insideof an exel file with input of the first and sec name, if the child is there it will reply but if the child is not there it will ask the user if he wants to add him.该程序应该在输入名字和秒名的exel文件中搜索,如果孩子在那里,它会回复,但如果孩子不在那里,它会询问用户是否要添加他。 everything works fine but i cant hide the "yes" button for some reason一切正常,但由于某种原因我无法隐藏“是”按钮

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

this is the entire code for thos who wondring;这是给那些想知道的人的完整代码;

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

Your code does not run.你的代码没有运行。 Please provide a Minimal, Complete, and Verifiable example .请提供一个最小、完整和可验证的示例

However, you are using the name "yesbot" for the button and also for the button callback function, the only difference being the case.但是,您为按钮和按钮回调函数使用名称“yesbot”,唯一的区别是这种情况。 This may be the root of your problem.这可能是您问题的根源。

Should than not be the case I'm supplying an example of how hiding a widget can be done.应该不是这种情况,我提供了一个如何隐藏小部件的示例。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM