Home > Enterprise >  Tkinter window says (not responding) when I use infinite loop
Tkinter window says (not responding) when I use infinite loop

Time:08-24

I have a problem with my python tkinter code, I need to use an infinite loop but this make my window to says (not responding) How can I fix it?

Here is a part of code (trading bot) :

 def Trading(i) :
        check, id = Iq.buy(money[i], valuta, 'call', 1)
        if check:
            print('Good order')
            if Iq.check_win_v3(id) == 0 :
                pass
            elif Iq.check_win_v3(id) > 0:
                i = 0
            else:
                i  = 1
            Trading(i)
        else:
            print('Fail order')

CodePudding user response:

i suggest to use async and await for loops that are infinite , or you can use threading modules in python , if you search about it you can find your answer , hope to successfull

CodePudding user response:

As mentioned by other comments, the main loop is an infinite loop, therefore creating another infinite loop is blocking.

Due to no code provided, it is unclear to tell what kind of infinite loop you want to run. Most of the time it is not necessary to run one, even if you think it is, as it can be done with either calling the method after or multithreading. The latter should be considered in case the former does not work.

There is not a shortage of material on this topic due to the issue being so common, therefore finding multiple examples and tutorials will not be a problem. However, here are some examples:

CodePudding user response:

Running the root's mainloop function is an infinite loop (until a user clicks the exit button or something else that stops it explicitly). So, maybe your code doesn't need to have an internal infinite loop. Can you describe the problem more clearly?

Edit: Ok, so it looks like the problem is that your code is calling itself recursively, and it will never stop doing so. This will lead to stack overflow. Maybe you could change your code to perform the trading checks a certain number of times when a tkinter button is pressed. If you want it to run on its own indefinitely, you'll probably need to look at https://docs.python.org/3/library/tkinter.html (Scroll down to the header titled 'Threading model'), to understand how threading works with tkinter, and then using a separate thread for your trader bot, as others have suggested.

  • Related