python: Threading classes from main_window class
<p>I'm trying to create a game using Tkinter that can run functions from multiple class objects simultaneously using threads. In the MainWindow class, I have "player" and "player2" assigned to the "Player" class. </p>

<p>In the "Player" class,  have a function called "move" that simply moves the canvas object.</p>

<p>When the right button is press , "player" starts moving.However, as soon as the left button is pressed, it seems that "player" is stopped in replacement for "player2".</p>

<p>Is there anyway to fix this?</p>

<pre><code>from tkinter import *
from threading import Thread
import time


class MainWindow(Frame):
    def __init__(self , parent):
        self.backround = '#%02x%02x%02x' % (180, 180, 180)
        self.main_width = 1905
        self.main_height = 1002

        Frame.__init__(self , parent , bg = self.backround)
        self.pack(fill=BOTH, expand=1)
        self.parent = parent
        self.parent.geometry('1905x1002+0+0')
        self.main_canvas = Canvas(self , width = self.main_width , height =       
        self.main_height , bg = 'white')
        self.main_canvas.pack()

        self.Keyboard_Events = Thread(target = self.keyboard_events)
        self.Keyboard_Events.start()
</code></pre>

<p>players</p>

<pre><code>        self.player = Player(self.main_canvas , [125 , 125] , self) #(canvas , coords)
        self.player2 = Player(self.main_canvas , [200 , 100] , self) #(canvas , coords)
</code></pre>

<p>callbacks</p>

<pre><code>    def keyboard_events(self):
        def callback_mouse_primary(event):
            self.player.move(0.01)
        def callback_mouse_secondary(event):
            self.player2.move(0.01)

        root.bind('&lt;Button-1&gt;' , callback_mouse_primary)
        root.bind('&lt;Button-3&gt;' , callback_mouse_secondary)
</code></pre>

<p>player class</p>

<pre><code>class Player(Thread):
    def __init__(self , canvas , coords , parent):
        Thread.__init__(self)
        self.setDaemon(True)
        self.canvas = canvas
        self.coords = coords
        self.player_object = self.canvas.create_rectangle(self.coords[0]-25 , self.coords[1]-25 , self.coords[0]+25 , self.coords[1]+25)

    def move(self , Time):

        for y in range(100):
            self.canvas.coords(self.player_object , self.coords[0]-25 , self.coords[1]-25 , self.coords[0]+25 , self.coords[1]+25)
            self.coords[0] += 0.1
            self.coords[1] += 0.1
            self.canvas.update()
            time.sleep(Time)

    def Print_info (self):
        print (self.coords)

if __name__ == '__main__':
    root = Tk()
    main = MainWindow(root)
    root.mainloop()
</code></pre>

<p>Just to make it clear. Player objects are created within the MainWindow class and functions within those players are run within the MainWindow class. Is there anyway to thread those player objects to run independently?</p>

<p>1) your need to fix your model and have a separation between the model/data and the representation/visualization :</p>

<p><code>players</code> need only to hold their data, and not be responsible for drawing anything. You should have a main rendering loop in charge of reading the components on the display, and position them accordingly. </p>

<p>2) <code>players</code> are not <code>threads</code> and there's not need to daemonize them. <code>Threads</code> runs function as a separate process. In your case, the separate process is to start moving the players at a certain rate, that being :</p>

<pre><code>def callback_mouse_primary(event):
        t=Thread(target=self.player.move,kwargs={'Time': 0.01})
        t.start()
</code></pre>

python class python-3.x tkinter python-multithreading