Linked Questions

Popular Questions

I have been creating a basic Python Machine Learning GUI using Tkinter. I don't have experience in creating anything very long or spanning across multiple .py files or with a GUI.

I have found myself using a lot of global variables in order to access user input (e.g. a file path) but I know this is bad practice and will likely lead to spaghetti code in the end.

How can I better organise this application without global variables so that I can make use of variables that are set within the GUI through button presses etc?

I've tried global variables, which do work, but will probably lead to bad habits forming, without these I get errors regarding variables being out of scope and undefined.

The application works so far when everything is within one .py file but now that it is growing and I want to split it up into a proper structure I am struggling.

One example of this in my application is getting the user to choose a csv file to be used. So I have:

def csvOpen():
    csvFilePath = filedialog.askopenfilename()

Which is then called by a button. But I then want to use this csvFilePath elsewhere such as:

def csvDisplay():

    window2 = Toplevel() 
    csvFrame = Frame(window2)
    csvFrame.pack(fill=BOTH, expand=1)
    df = pd.read_csv(csvFilePath)
    window2.table = csvView = Table(csvFrame, dataframe=df, showtoolbar=False, showstatusbar=False)
    csvView.show()

and:

def pre_processing_split():
    np.random.seed(11)
    df = pd.read_csv(csvfile)

I want to be able to use variables from within a method tied to my GUI so that I can split the application up into multiple files rather than having it all within one file which doesn't seem practical or good practice. I'm not sure if it would be better to use classes instead or if there is a better approach for what I am trying to achieve.

Related Questions