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 8170107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:04:09+00:00 2026-06-06T21:04:09+00:00

I’m trying to write a python code which create and initialize n timers in

  • 0

I’m trying to write a python code which create and initialize n timers in a for loop and bind them to some events. The code bellow is an example of the way I’m doing it:

import wx

#dm1 = {a dewell times values dictionary}
#screens = [a list of dm1 keys]
trials = range(1, 3)
timers = range(0, 4)


class DM1(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        panel = wx.Panel(self)

        self.Go = wx.Button(panel, label = 'Go!', pos = (600, 450))
        self.Bind(wx.EVT_BUTTON, self.BuildTimers, self.Go)

    def BuildTimers(self, event):
        self.timers = {} 
        cum_timer = 0

        for trial in trials:
            for timer in timers:
                key = (timer, trial)
                new_timer = wx.Timer(self)
                cum_timer += dm1[screens[timer]]
                new_timer.Start(cum_timer * 1000, False)

                new_timer.mykey = key 
                self.timers[key] = new_timer

        self.Bind(wx.EVT_TIMER, self.Screens)

    def Screens(self, event):
        if event.GetEventObject() == self.timers[event.GetEventObject().mykey]:
            print event.GetEventObject().mykey
            if event.GetEventObject().mykey[0] == 0:
                print 'You bastard!'

            if event.GetEventObject().mykey[0] == 1:
                print 'you vicious...'                

            if event.GetEventObject().mykey[0] == 2:
                print 'heartless bastard!'

            if event.GetEventObject().mykey[0] == 3:
                print 'Oh It makes me mad!'

app = wx.App()
frame = DM1(None, title = 'IG', size = (wx.DisplaySize()))
frame.Show()
app.MainLoop()

The timers don’t start at the time I specificate: the second loop of trial appear to overwrite the first. For example, the statement print event.GetEventObject().mykey, in the complete code, prints

(0, 1) (1, 1) (1, 2) (2, 1) (3, 1) (0, 2) (3, 1) (2, 1)

instead of

(0, 1) (1, 1) (2, 1) (3, 1) (0, 2) (1, 2) (2, 2) (3, 2)

I guess the problem is in the GetEventObject, but I don’t know a better way to bind the timers to the events. Does somebody have an idea?

Thanks a lot!

  • 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-06T21:04:10+00:00Added an answer on June 6, 2026 at 9:04 pm

    When using wxPython, I find it wiser to let widgets generate its own id using default parameters. Then you just ask the widget for its widget id. It is a dangerous practice, meaning really tricky to debug, to use manual magic number id values and sent the widget’s id values to that, especially for ids below 4000 as there are a bunch aready defined in the low range. Some internal methods will ask for “give me widget with id 1” for example and instead of getting some internal default widget, that method will get your timer widget instead.

    Working with predefineds like TIMER_ID = 4000 or even more elusively buggy TIMER_ID = 4000 + someoffset, add additional maintainence costs to code.

    I find the most flexable, reliable way is to user the id=-1 or id=wx.NewId(). Then say

    ident = myTimer.GetId()
    self.timers[ ident ] = myTimer
    

    or

    self.timers[ myTimer ] = myTimer
    

    or

    self.timers.append( myTimer ) 
    

    where order is maintained and the index acts like how you’ve used the ID field in your code.

    Another good practice is to avoid using “id” as a temp variable as there is the Python builtin function id() AND in wx, widgets have an id attribute too:

    w=Widget(..., id=int)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:

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.