How do you run a python script where the tkinter GUI is in the same file? How do you process user entries?

Viewed 148

I have a python script that does stuff with pandas. I've created a GUI in tkinter that will assign user inputs to different global variables. I need to keep everything in the same file. Here is a snippet of what I'm doing:

#Global Variables with default values
Winrate = 0

# Button Functions
def getWinRate():
    global Winrate
    Winrate = win_rate_entry.get()
    messagebox.showinfo('Successful Entry!')

def RunPipelineScript():
    print("Do stuff")

start_script_btn = Button(window, text="Start Script", command= RunPipelineScript())
# Rest of the script is below. It uses the values inputted by the user, e.g. WinRate entry, in the #calculations

I essentially want to make it so that when the Start Script button is clicked the rest of the script executes. I'm not sure how to go about this. Should I just paste the rest of the script into the "RunPipelineScript" function? If so will the global variables have been changed based on the users entries? Could I just make it so that once the user has inputted the appropriate entries the RunPipelineScript function will just jump to a certain line and continue to execute the program as normal?

As you can see I'm a novice at Python and Pandas, and any help you can provide would be most appreciated :)

1 Answers

The solution is simple. You just need to do some simple steps; that is you just need to do a function in function thing.

Let's take your code for explanation. Here: This button, when clicked will execute the RunPipelineScript function.

Well if you want to execute the whole script you have to place everything inside the RunPipeLine function.

Also you have declare a global variable in the function too. Thus add this just after declaring the funtion:

global Winrate

The code after alteration:

Winrate = 0

def RunPipelineScript():
    global Winrate
    print("Do stuff")
    # Button Functions
    def getWinRate():
        global Winrate
        Winrate = win_rate_entry.get()
        messagebox.showinfo('Successful Entry!')

        # Add all the code inside this function you want. That will execute the whole script.

start_script_btn = Button(window, text="Start Script", command= RunPipelineScript)
# Rest of the script is below. It uses the values inputted by the user, e.g. WinRate entry, in the #calculations
Related