0

I have a little problem... In fact I am coding with asynchronous methods in classes and I also use Tkinter and the fact is that when I try to press on a button to call a object method it doesn't work beaxause of the async that create an istance of the methods....

I tried many things like using the lambda in the button command attribute but without any results...

May you have an idea ?

I'll be really grateful.

Here is a portion of my code :

class App(customtkinter.CTk, JeuDeLaVie):
    # Some code like __init__() and other boring methods
    
    self.startButton = customtkinter.CTkButton(self, text = 'Start', fg_color = '#0D8A66',  hover_color = '#0B7657', command = """My command that is supposed to call self.startGame() right below""")
        self.startButton.place(relx = .40, rely = .930)

    
    async def startGame(self):
            """
            Permet de lancer le jeu avec differentes conditions comme le temps entre chaque tour, la duree maximale, le nombre de tour maximum.
            :param nombreDeTours -> int [default value: 100 turns]
            :param delai -> int [default value: 1]
            :param maxTime -> int [default value: 5 min]
            :param model -> str [default value: Manuel]
            """
            print(0)
            self.startButton.destroy() # Detruit le bouton start

            while self.isGameStarted:
                 await asyncio.create_task(self.run())
    #

And that's it....

  • PS : Sorry for the french sentences, my teachers love them so much that they prefer break programming conventions and use french... –  Nov 25 '22 at 22:39
  • You can't run an `async def` coroutine from a TKinter trigger, not directly. If you need to have a *long running* asynchronous event loop *next* to your tkinter event loop you'll have to use _threads_ to run one or the other. You can't run them in the same main thread, because both want full control over that thread. See for an extensive answer of mine detailing how that would work: [Python3 Flask asyncio subprocess in route hangs](https://stackoverflow.com/a/58614689) – Martijn Pieters Nov 25 '22 at 22:45
  • But, let me be frank and fair: **these are more advanced techniques** and probably not that suitable for someone trying to solve a coding class problem. Are you **certain** that you must use asyncio coroutines with tkinter? – Martijn Pieters Nov 25 '22 at 22:48
  • Honestly, I don't even know how to use something else... I mean, js and python are so far away one from the other than It's a bit difficult to make the transition. Moreover, I am trying to use a 2 loops at the same time and by two I mean the tkinter one and a personnal one... It's for that reason that I am using this asynchronous system but If you have an other solution I'll be interested. Btw, I am working on the game of life and I need to make a pause on the game and make stop my cells or at least the proccessing of the cells position and I don't know how to do it differently as I said... –  Nov 25 '22 at 22:53
  • See [How can I schedule updates (f/e, to update a clock) in tkinter?](https://stackoverflow.com/q/2400262) for a technique you can use; update your game-of-life game state using the tkinter loop. It'll call a designated function repeatedly, have it update your game state. Have a variable that says 'running', if not set, don't update the game state. Pausing is simply setting 'running' to false. – Martijn Pieters Nov 25 '22 at 23:00

0 Answers0