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

  • Home
  • SEARCH
  • 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 9138135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:13:01+00:00 2026-06-17T09:13:01+00:00

I have a timer that starts to ‘sample’ (just a random no. here for

  • 0

I have a timer that starts to ‘sample’ (just a random no. here for now) and plots to a graph – the timer is started when ‘Start’ is pressed. This works OK (plotting is not great but it works), though my issue here is to introduce a ‘Target’ value such that the sample value is altered by — though only after target is entered by the enter key or even a update button.

As it stands, any changes made to the target SpinCtrl widget are done so live and I don’t want this to be the case, I’d only like to update after Enter

So I understand my question relates to timers, binding events and *wx.EVT_TEXT_ENTER*; how many I capture a text entry whilst a timer is already running?

Forgive my lack of python knowledge.

My code:

print( "\n- Please Wait -- Importing Matplotlib and Related Modules...\n" )
import matplotlib
import numpy
import wx
import numpy as np
matplotlib.use('WXAgg')

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.figure import Figure
import random

SamplingTime      = 1# in ms


class GraphPanel( wx.Panel ) :
    def __init__( self, parent, position ) :
        wx.Panel.__init__( self, parent, pos=position, size=(800,320) )
        # initialize matplotlib 
        self.figure = matplotlib.figure.Figure( None, facecolor="white" )
        self.canvas = matplotlib.backends.backend_wxagg.FigureCanvasWxAgg( self, -1, self.figure )
        self.axes = self.figure.add_subplot(111)
        self.axes.grid(True, color="gray")
        self.axes.set_xbound( (0,100) )
        self.axes.set_ybound( (0,10) )
        self.axes.set_xlabel( "Sample" )
        self.axes.set_ylabel( "Data" )
        self._SetSize()
        self.Bind( wx.EVT_SIZE, self._SetSize )
        self.Data   = []

    def updateData(self, value):
        self.Data.append( value )
        x = np.arange( len(self.Data) )
        y = np.array(self.Data)

        yMin = round(min(y)) + 2
        yMax = round(max(y)) + 2            
        self.axes = self.figure.add_subplot(111)
        self.axes.grid(True, color="gray")
        self.axes.plot(x,y, "-k")

        self.axes.set_ybound( (yMin,yMax) )
        self.axes.set_xlabel( "Sample" )
        self.axes.set_ylabel( "Data" )

        self._SetSize()
        self.Bind( wx.EVT_SIZE, self._SetSize )      
        self.canvas = FigureCanvas(self, -1, self.figure)

    #-----------------------------------------------------------------------------------
    def _SetSize( self, event=None ):
        pixels = self.GetSize()
        self.SetSize( pixels )
        self.canvas.SetSize( pixels )

        dpi = self.figure.get_dpi()
        self.figure.set_size_inches( float( pixels[0] ) / dpi,float( pixels[1] ) / dpi )
    #------------------------------------------------------------------------------------

############################################ Basic Frame ###################################################                
class BasicFrame(wx.Frame):
    def __init__(self, parent, title):
        self.Data         = 0
        self.DataPrevious = 1
        # split the screen
        wx.Frame.__init__(self, parent, title="Data Collection", size=(1100,750))
        self.sp = wx.SplitterWindow(self)
        self.p1 = wx.Panel(self.sp, style = wx.SUNKEN_BORDER)  
        self.p2 = wx.Panel(self.sp, style = wx.SUNKEN_BORDER)
        self.sp.SplitVertically(self.p1, self.p2, 940)

        # graph
        self.Graph = GraphPanel( self.p1, position=(20, 20) )

        # define control input 
        wx.StaticBox(self.p2, -1, "System Control",  pos=(5, 5), size=(120, 210))
        wx.StaticText(self.p2, -1,"Target:",         pos=(15, 100))
        self.Target = wx.SpinCtrl(self.p2,-1,pos=(15,120), size=(80, 20), min=5, max=10)
        self.Target.Bind(wx.EVT_BUTTON, self.StartSystem)

        self.StartButton = wx.Button(self.p2, -1, "Start", pos=(20,230))
        self.StartButton.Bind(wx.EVT_BUTTON, self.StartSystem)

        self.PauseButton = wx.Button(self.p2, -1, "Pause", pos=(20,270))
        self.PauseButton.Bind(wx.EVT_BUTTON, self.PauseSystem)

        self.StopButton = wx.Button(self.p2, -1,  "Stop",  pos=(20,500))
        self.StopButton.Bind(wx.EVT_BUTTON, self.StopSystem)



        # set the timers 
        self.SampleTimer  = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.updateData, self.SampleTimer)
        print( "Ready!\n" )


    def sampleData(self):
        Error = random.uniform(0, 0.2)
        Target     = self.Target.GetValue()
        if( self.Data <= Target):
            self.Data += self.DataPrevious*Error
        elif( self.Data >= Target):
            self.Data -= self.DataPrevious*Error
        self.DataPrevious = self.Data

        print( "Target Aim: "+str(self.Data) )
        return self.Data


    def updateData(self, event):
        CurrentData       = round(self.sampleData(),2)   # obtain currnt data
        self.Graph.updateData(CurrentData)               # add data to graph   





    def StartSystem(self, event):
        self.SampleTimer.Start(SamplingTime)

    def PauseSystem(self, event):
        self.SampleTimer.Stop()

    def StopSystem(self, event):
        self.SampleTimer.Stop()
        self.Destroy()


app = wx.App(redirect=False)
frame = BasicFrame(None, -1)
frame.Show()
app.MainLoop()
  • 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-17T09:13:02+00:00Added an answer on June 17, 2026 at 9:13 am

    If I understand correctly, you want to update the internal value in you app using the SpinCtrl value only if 1) Enter inside SpinCtrl is pressed or 2) Update button is pressed. The following sample would do just that:

    import wx
    
    class MainWindow(wx.Frame):
        def __init__(self, *args, **kwargs):
            wx.Frame.__init__(self, *args, **kwargs)
    
            self.panel = wx.Panel(self)
            self.spin = wx.SpinCtrl(self.panel)
            self.button = wx.Button(self.panel, label="Update")
    
            self.sizer = wx.BoxSizer()
            self.sizer.Add(self.spin)
            self.sizer.Add(self.button)
    
            self.panel.SetSizerAndFit(self.sizer)  
            self.Show()
    
            # Use EVT_CHAR_HOOK on Frame insted of wx.EVT_KEY_UP on SpinCtrl
            # to disable "on Enter go to next widget" functionality
            self.Bind(wx.EVT_CHAR_HOOK, self.OnKey) 
            self.button.Bind(wx.EVT_BUTTON, self.OnUpdate)
    
            self.timer = wx.Timer(self)
            self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
            self.timer.Start(500)
    
            self.value = 0
    
        def OnKey(self, e):
            if e.GetKeyCode() == wx.WXK_RETURN: # Is the key ENTER?
                self.value = self.spin.GetValue() # Read SpinCtrl and set internal value
            else: # Else let the event out of the handler
                e.Skip()
    
        def OnUpdate(self, e):
            self.value = self.spin.GetValue() # Read SpinCtrl and set internal value
    
        def OnTimer(self, e):
            # Show internal value
            print(self.value)
    
    app = wx.App(False)
    win = MainWindow(None)
    app.MainLoop()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a very simple android activity that starts a timer when you
I have a webpage that, when you click start patrolling, starts counting the time
If we have a timer that starts from lets say CURRENT_TIMESTAMP - 1 Hour
i have a windows service that starts a timer on the main thread. The
I have created a javascript timer that starts at 40 and counts down by
I have a timer that I want to start an AsyncTask when the countdown
I have the script needed to generate a countdown timer that has a start
I have a form that starts a thread. Now I want the form to
I have an application that starts a timer and an ongoing notification. Whenever I
I have a simple class that stops and starts a timer when my program

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.