I am trying to do a simple task in Python but I keep getting error. I have a string stored in a variable and a simple GUI with a button (in 1st.py) which will call another function in a separate file (twond.py) which will split that string and store and return that in 1st.py
The function calling works fine. The function does the job of splitting the string and storing it. But when I ask to print in the main file (1st.py), it shows error global variable not defined. I have tried defining the variable global but it didn't work.
Can anyone help me with this?
Here is the code:
#this is the main file 1st.py which has a string stored in variable s and a GUI function that calls the function # from Tkinter import * import string, twond class App: def __init__(self,master): frame = Frame(master.title("New Title")) frame.pack() self.bt=Button(frame, text='click', command=self.first) self.bt.grid(row=1, column=0) #global L def first(self): s = 'this is a string' twond.second(str(s)) #this passes the variable s to the function second() in the file twond.py #print(L) #if I uncomment this i get error message root = Tk() app = App(root) root.mainloop() #this is the file twond.py which splits the string s and stores in variable L #I am trying to make it to pass this variable L back to the main file 1st.py import string def second(s): global L L=string.split(s) print(s) print(L)
Edited 7 Years Ago by romes87: showing code