0

I am looking to update a button in my code by running a function later in the code that references a previous function, then update that said button with new text. When I run this code, it states that Source1 is not defined when the okay 3 function runs the if statement. New to coding and looking for tips. Thanks

from tkinter import *

import pandas as pd

##database = pd.ExcelFile('C:/Code/CODETEST.xlsx').parse(1)

excel_data_df = pd.read_excel('CODETEST.xlsx', sheet_name='Sheet1')

print(excel_data_df)

print(excel_data_df.columns.ravel())

##print(excel_data_df['Service Name'].tolist())


My_Service_List = (excel_data_df['Service Name'].tolist())

print(My_Service_List)

##Source1_Text = tk.StringVar()

root = Tk()

##Creating Entries
Group = Entry(root, width=50)
Group.grid(row=0, column=5)
Source = Entry(root, width=50)
Source.grid(row=1, column=5)
Service = Entry(root, width=50)
Service.grid(row=2, column=5)

global Source1
global Standard_window

def Standard_flow():
    ##global Source1_Text
    Standard_window = Tk()
    ##Source1_Text = tk.StringVar()
    ##Source1_Text.set("Original Text")
    Source1 = Button(Standard_window, text=My_Service_List[1])
    Source1.grid(row=1, column=1)
    Source2 = Button(Standard_window, text='HDA')
    Source2.grid(row=1, column=2)
    Source3 = Button(Standard_window, text='Router')
    Source3.grid(row=1, column=3)


# Definining Buttons
def Okay():
    hello = "Searching " + Group.get()
    myLabel = Label(root, text=hello)
    myLabel.grid(row=0, column=6)


def Okay2():
    hello2 = "Searching " + Source.get()
    myLabel2 = Label(root, text=hello2)
    myLabel2.grid(row=1, column=6)


def Okay3():
    hello3 = "Searching " + Service.get()
    myLabel3 = Label(root, text=hello3)
    myLabel3.grid(row=2, column=6)
    if My_Service_List.__contains__(Service.get()):
        Source1.config(text=My_Service_List[3])
        return Standard_flow()
    else:
        None


##Creating Buttons
myButton_Group = Button(root, text='Group Multicast IP', command=Okay)
myButton_Source = Button(root, text='Source IP', command=Okay2)
myButton_Service = Button(root, text='Service Name', command=Okay3)

##Displaying Buttons
myButton_Group.grid(row=0, column=1)
myButton_Source.grid(row=1, column=1)
myButton_Service.grid(row=2, column=1)

root.mainloop()
7
  • please keep a mention of which button you want to edit and and the error you get Commented Aug 5, 2020 at 20:51
  • The button I want to edit is "Source1", you see in the if statement "Source1.config(update text)" Error is.. "Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\206415779\Anaconda3\envs\FINDIT\lib\tkinter_init_.py", line 1883, in call return self.func(*args) File "C:/Users/206415779/Python/FINDIT/FINDIT START", line 66, in Okay3 Source1.config(text=My_Service_List[3]) NameError: name 'Source1' is not defined" Commented Aug 5, 2020 at 21:00
  • try saying global Source1 inside of Standard_flow() and not on the main block Commented Aug 5, 2020 at 21:01
  • def Okay3(): hello3 = "Searching " + Service.get() myLabel3 = Label(root, text=hello3) myLabel3.grid(row=2, column=6) if My_Service_List.__contains__(Service.get()): Source1.config(text=My_Service_List[3]) return Standard_flow() Are you saying return Standard_flow(global Source1")? Also I'm not sure I understand, the "not on the main block"? Commented Aug 5, 2020 at 21:06
  • no no ill add as an answer please wait Commented Aug 5, 2020 at 21:07

1 Answer 1

1

The problem is that your Source1 is a local variable and hence it is available only to the function that you defined it on, that is, Standard_flow(). To make this global and fix the error, just try saying this:

def Standard_flow():
    global Source1
    Standard_window = Tk()
    ......

With this change, the error should go and it should work. And just keep this global Source1 and remove all other global Source1 in your code(i mean the one outside the function)

Do let me know, if the error persists

Cheers

Sign up to request clarification or add additional context in comments.

10 Comments

Still no luck, I removed the global Source1 from above the Standard_flow function. I then added global Source1 to the Standard_flow. Still receive the same error when the if statement returns true. "NameError: name 'Source1' is not defined"
ive updated the Q with the whole code, take a look and let me know if its same to yours
I got it to work by defining Source1 prior to that function, but the problem is when the if statement is true and returns the Standard_flow function it shows the wrong text, when I click that button again I get the corrected updated text. do I have to return the new Source1 value prior to return Standard_flow?
Once again didn't come back to give thanks. @CoolCloud, thank you for the help!
@Josh oh, means alot, Happy coding
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.