while loop inside Tkinter

Viewed 80

so I used threading so I can use a while loop inside Tkinter. the code as follows (the main idea is to have a intro screen, where you can press a botton and it opens up the game):

def start_game_thread():
threading.Thread(Target=root.after(3000,start_game()).start())

def searching_loop(start_point, goal):
while True:
    pure_goal = goal.current_url
    start_point_temp = start_point.current_url
    if pure_goal != goal.current_url:
        goal.get(pure_goal)
        print("I am the goal, you cannot change me")
    if start_point_temp != start_point.current_url:
        start_point_temp = start_point.current_url
        if goal.current_url == start_point.current_url:
            start_point.quit()
            goal.quit()



def start_game():
    start_point = exction()
    set_main_page(start_point, 'https://en.wikipedia.org/wiki/Main_Page')
    goal = exction()
    set_main_page(goal, 'https://en.wikipedia.org/wiki/Main_Page')
    clicker(start_point, 'Random article')
    clicker(goal, 'Random article')
    searching_loop(start_point, goal)

and here is the main :

root = Tk()
root.geometry("1920x1080")
EnterTheGame = Button(root, text="Enter The Game", command=lambda :start_game_thread())
EnterTheGame.place(x=960, y=540)
root.mainloop()

for some reason, it just doesn't seem to work, I am new to threading and any help can help me out :)

Update 1 :

def start_game():
    root.withdraw()
    start_point = execution()
    set_main_page(start_point, 'https://en.wikipedia.org/wiki/Main_Page')
    goal = execution()
    set_main_page(goal, 'https://en.wikipedia.org/wiki/Main_Page')
    clicker(start_point, 'Random article')
    clicker(goal, 'Random article')
    searching_loop(start_point, goal)
    root.deiconify()

after doing this, the problem is solved and the web browser runs right after I clicked the button with no problem, but the problem begins when I try to reopen my root after the game was complete.

[WinError 10061] No connection could be made because the target machine actively refused it

the screen does work but it keeps printing a lot of errors and also slow down my CPU.

update 2: adding a break to the searching_loop loop managed to solve the error problem, but the deiconify() retrieves me back to the intro screen

update 3: as @ TheLizzard suggested, I just changed my button using .config(text="") and it worked perfectly :)

Thank you A lot.

1 Answers

I have not yet run your code, but this should get you closer. Remember when you are passing a function to thread, you don't want to CALL the function. When you say threading.Thread(Target=start_game()), that immediately calls the "start_game" function (in the current thread), and passes its return value to threading.Thread.

def start_game_button():
    root.after(3000,start_game_thread)
def start_game_thread():
    threading.Thread(Target=start_game).start()
...
EnterTheGame = Button(root, text="Enter The Game", command=start_game_button)
Related