Reputation: 380
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()
Upvotes: 0
Views: 103
Reputation: 15098
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
Upvotes: 1