-3

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. 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()
Maklonim
  • 1
  • 2
  • Continue using `pack_forget` but first follow the marked question. Does this answer your question? [Tkinter: AttributeError: NoneType object has no attribute ](https://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-attribute-name) – Delrius Euphoria Jun 06 '22 at 19:54
  • What does _"i cant seem to hide it."_ mean? Why can't you? What happens when you try? – Bryan Oakley Jun 06 '22 at 20:24
  • i just cant to hide it,grid_forget() doesnt work on it – Maklonim Jun 06 '22 at 20:32
  • Update the question with any errors you get. And follow the link posted by me in the last comment. – Delrius Euphoria Jun 06 '22 at 20:38
  • Since the problem is not at all related to pandas can you create a smaller [mcve] that doesn't depend on an external file or on pandas? You've posted a relatively large amount of code for what seems to be a simple problem. – Bryan Oakley Jun 06 '22 at 20:51
  • Seems like the variable is out of scope. Try saying `global askMan, nobot` inside of `findchild()` – Delrius Euphoria Jun 06 '22 at 20:57
  • saying global solved it,can you explain more about this and when/how i can use it? thank you very much! – Maklonim Jun 06 '22 at 21:11

1 Answers1

0

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. 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()
figbeam
  • 7,001
  • 2
  • 12
  • 18