Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9145477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:31:26+00:00 2026-06-17T10:31:26+00:00

I’m drawing little circles on a canvas with these functions : This is the

  • 0

I’m drawing little circles on a canvas with these functions :

This is the function that will draw the circles :

class Fourmis:

def __init__(self, can, posx, posy, name, radius):
    self.can = can

    self.largeur_can = int(self.can.cget("width"))
    self.hauteur_can = int(self.can.cget("height"))

    self.posx = posx
    self.posy = posy
    self.name = name 
    self.radius = radius

    self.ball1 = self.can.create_oval(self.posy, self.posx, self.posy+radius, self.posx+radius, outline=self.name, fill=self.name, width=2)

    self.nx = randrange(-10,10,1)
    self.nx /= 2.0
    self.ny = randrange(-10,10,1)
    self.ny /= 2.0

    #self.can.bind("<Motion>", self.destruction, add="+") 
    self.statut = True

    self.move()

def move(self):
    if self.statut == True :
        self.pos_ball = self.can.coords(self.ball1)
        self.posx_ball = self.pos_ball[0]
        self.posy_ball = self.pos_ball[1]

        if self.posx_ball < 0 or (self.posx_ball + self.radius) > self.largeur_can:
            self.nx = -self.nx         
        if self.posy_ball < 0 or (self.posy_ball + self.radius) > self.hauteur_can:
            self.ny = -self.ny

        self.can.move(self.ball1, self.nx, self.ny)

        self.can.after(10, self.move)

this one creates the canvas and the circles :

class App(Frame):

def __init__(self):
    self.root=Tk()
    self.can=Canvas(self.root,width=800,height=600,bg="black")
    self.can.pack()

    self.create(50, "green")
    self.create(50, "purple")



def mainloop(self):
    self.root.mainloop()


def create(self, i, name):
    for x in range(i):
        self.x=Fourmis(self.can,100,400, name,0)

I call these lines to run the project :

jeu = App()
jeu.mainloop()

What is the correct way to execute self.create(50, "green") and self.create(50, "purple") in different threads?

I have tried the following, but could not get it to work.:

class FuncThread(threading.Thread):
def __init__(self, i, name):
    self.i = i
    self.name = name
    threading.Thread.__init__(self)

def run(self):
    App.create(self, self.i, self.name)

Is someone able to tell me how to run these threads?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:31:27+00:00Added an answer on June 17, 2026 at 10:31 am

    When this functionality is needed, what you do is schedule the events you wish to perform by putting them in a queue shared by the threads. This way, in a given thread you specify that you want to run “create(50, …)” by queueing it, and the main thread dequeue the event and perform it.

    Here is a basic example for creating moving balls in a second thread:

    import threading
    import Queue
    import random
    import math
    import time
    import Tkinter
    
    random.seed(0)
    
    class App:
        def __init__(self, queue, width=400, height=300):
            self.width, self.height = width, height
            self.canvas = Tkinter.Canvas(width=width, height=height, bg='black')
            self.canvas.pack(fill='none', expand=False)
            self._oid = []
            self.canvas.after(10, self.move)
    
            self.queue = queue
            self.canvas.after(50, self.check_queue)
    
        def check_queue(self):
            try:
                x, y, rad, outline = self.queue.get(block=False)
            except Queue.Empty:
                pass
            else:
                self.create_moving_ball(x, y, rad, outline)
            self.canvas.after(50, self.check_queue)
    
        def move(self):
            width, height = self.width, self.height
            for i, (oid, r, angle, speed, (x, y)) in enumerate(self._oid):
                sx, sy = speed
                dx = sx * math.cos(angle)
                dy = sy * math.sin(angle)
                if y + dy + r> height or y + dy - r < 0:
                    sy = -sy
                    self._oid[i][3] = (sx, sy)
                if x + dx + r > width or x + dx - r < 0:
                    sx = -sx
                    self._oid[i][3] = (sx, sy)
                nx, ny = x + dx, y + dy
                self._oid[i][-1] = (nx, ny)
                self.canvas.move(oid, dx, dy)
            self.canvas.update_idletasks()
            self.canvas.after(10, self.move)
    
        def create_moving_ball(self, x=100, y=100, rad=20, outline='white'):
            oid = self.canvas.create_oval(x - rad, y - rad, x + rad, y + rad,
                    outline=outline)
            oid_angle = math.radians(random.randint(1, 360))
            oid_speed = random.randint(2, 5)
            self._oid.append([oid, rad, oid_angle, (oid_speed, oid_speed), (x, y)])
    
    def queue_create(queue, running):
        while running:
            if random.random() < 1e-6:
                print "Create a new moving ball please"
                x, y = random.randint(100, 150), random.randint(100, 150)
                color = random.choice(['green', 'white', 'yellow', 'blue'])
                queue.put((x, y, random.randint(10, 30), color))
            time.sleep(0) # Effectively yield this thread.
    
    root = Tkinter.Tk()
    running = [True]
    
    queue = Queue.Queue()
    
    app = App(queue)
    app.create_moving_ball()
    app.canvas.bind('<Destroy>', lambda x: (running.pop(), x.widget.destroy()))
    
    thread = threading.Thread(target=queue_create, args=(queue, running))
    thread.start()
    
    root.mainloop()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.