1

I have a function that is going to be the main area of this program, but when I add in a new function that needs to be executed by a button, it prevents the window from appearing, or gives out an error. The function I am trying to get to appear after a button press is new_function.

def bowler():
    global bowler_win
    global batsmen
    bowler_win = Toplevel(master)
    batsmen_win_2.withdraw()
    variable = StringVar(bowler_win)
    variable.set(fielding_team[0])
    title = Label(bowler_win, text = "Please choose the bowler").grid(row = 0, column = 1)

    w = OptionMenu(bowler_win, variable, *fielding_team)
    w.grid(row = 1, column = 1)

    def ok2():
        current_bowler = variable.get()
        for players in batting_team:
            if players == current_bowler:
                fielding_team.remove(current_bowler)
        main_play()

    button = Button(bowler_win, text="OK", command=ok2).grid(row = 2, column = 1)

#####CODE ABOVE IS ONLY TO SHOW WHERE THE FUNCTION BELOW IS EXECUTED FROM


def main_play():
    while innings != 3:
        facing_length = len(batsmen)
        while over != over_amount or out_full == True or facing_length <= 1:
            main_win = Toplevel(master)
            bowler_win.withdraw()
            ws = Label(main_win, text = "  ").grid(row = 1, column = 1)
            title = Label(main_win, text = "Current Game").grid(row = 0, column = 1)
            score = Label(main_win, text = "Current Score:").grid(row = 2, column = 1)
            line = Label(main_win, text = "--------------").grid(row = 1, column = 1)
            score = Label(main_win, text = str(runs) + "/" + str(out)).grid(row = 3, column = 1)
            line = Label(main_win, text="--------------").grid(row=4, column=1)
            cur_bat = Label(main_win, text = "Facing Batsmen: " + batsmen[0]).grid(row = 5, column = 1)
            other_bat = Label(main_win, text = "Other Batsmen: " + batsmen[1]).grid(row = 6, column = 1)
            current_patner = Label(main_win, text = "Patnership: " + str(partnership_runs)).grid(row = 7, column = 1)
            button = Button(main_win, text = "Next Play", command = new_function).grid(row = 8, column = 1) ###THIS IS WHERE THE NEW FUNCTION IS EXECUTED 

If I call the function new_function after the button, the main_win window does not appear, this is the same for if I call new_function above the main_play function, the same error occurs.

If I try to nest new_function below the button, I get the error UnboundLocalError: local variable 'new_func' referenced before assignment Even though its a function(and I don't have a variable named that)

If anyone can help, that would be amazing

  • Better post the code of `new_function()`. – acw1668 Feb 09 '21 at 23:50
  • @acw1668 right now its just a flag ```def new_function(): print("Flag")``` – Eamon Sharma Feb 09 '21 at 23:52
  • You keep creating new windows inside the inner while loop inside `main_play()` without letting tkinter mainloop to update, so the created windows do not show up. Adding `master.update()` at the end of inner while loop will show the windows. The issue is nothing related to `new_function()`. – acw1668 Feb 10 '21 at 00:36
  • Even if you follow @acw1668's advice, what you're trying to do won't work because nothing in the `while` loop will change the values of the variables (`over`, `over_amount`, `out_full`, and `facing_length`) that are controlling its execution. Suggest you read answers to [Tkinter — executing functions over time](https://stackoverflow.com/questions/9342757/tkinter-executing-functions-over-time) to get a better idea of how event-driven programming works. – martineau Feb 10 '21 at 01:28
  • @martineau Maybe they are changed inside `new_function()` later. The main issue is that there is no logic in the inner while loop to wait for user to click the `Next Play` button. – acw1668 Feb 10 '21 at 01:32
  • @acw1668: It doesn't matter what `new_function` does because it's not called from inside the `while` loop (which does nothing but make it the callback if one of the "Next Play" `Buttons` created in it were ever clicked). – martineau Feb 10 '21 at 01:35

0 Answers0