Python Forum
[Tkinter] How to create a delay for AI without freezing the GUI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How to create a delay for AI without freezing the GUI
#3
So I have read the Threads Guide, but got stucked on this point:

class Program:

    def __init__(self):
        self.width = 1000
        self.height = 800
        self.panel_width = 200
        #self.root = Tk()
        self.canvas = tkinter.Canvas(width=self.width, height=self.height, bg="black")
        self.canvas.pack()
        self.canvas.bind("<ButtonPress>", self.click)
        self.figures_images = {"0": tkinter.PhotoImage(file="white_pawn.png"), ...}
        self.figures = {"0":[<class Pawn...>, (x,y), image_id],"1":[<class Pawn...>, (x,y), image_id],"2":[<class Pawn...>, (x,y), image_id],...}
        ...

    def create_layout(self):
        # not important for this problem

    def start_ai(self):
        ...
        move = Move_ai(self)

    # next methods are not important now

class Pawn:
    def __init__(self, ...):
        ...
        # not important for this problem

class Move_ai:
    def __init__(self, program):
        self.main = program
        thread = threading.Thread(target=self.move)
        thread.start()

    def move(self):
        time.sleep(3)    # just to pretend PC is thinking
        ... # some code calculating which figure to move
        selected_id = ...    # id of figure it decided to move with
        self.main.canvas.coords(self.main.figures_images[selected_id], tuple_of_coords)
       # ^ and this is that line raising an error to me, this one:
Error:
Exception in thread Thread-1: Traceback (most recent call last): File "C:\python\lib\threading.py", line 917, in _bootstrap_inner self.run() File "C:\python\lib\threading.py", line 865, in run self._target(*self._args, **self._kwargs) File "C:\Users\user\Documents\PYTHON\chess\hra.py", line 581, in move self.main.canvas.coords(self.main.figures_images_[selected_id], tuple_of_coords) File "C:\python\lib\tkinter\__init__.py", line 2469, in coords self.tk.call((self._w, 'coords') + args))] RuntimeError: main thread is not in main loop
To be honest, I don´t understand that solution described here very well, could you help me to solve this problem using and showing only that version of this solution, which my program really needs?
Reply


Messages In This Thread
RE: How to create a delay for AI without freezing the GUI - by kom2 - May-04-2019, 12:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] GUI keeps freezing Fre3k 2 3,634 May-23-2020, 05:41 PM
Last Post: Fre3k
  [PyQt] How to open a program with python without freezing LavaCreeperKing 9 8,295 Aug-17-2019, 08:48 PM
Last Post: LavaCreeperKing
  Gi module window freezing problem loss 0 2,251 May-05-2018, 04:42 PM
Last Post: loss

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020