Jumat, 10 Oktober 2025

WXPYTHON - import time 2

 




import wx

import threading

import time


class MyFrame( wx.Frame ):


    def __init__( self, parent ):

        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Seconds Counter", 

            pos = wx.DefaultPosition, size = wx.Size( 236,173 ), 

            style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )


        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        self.SetBackgroundColour( wx.Colour( 0, 0, 255 ) )


        bSizer1 = wx.BoxSizer( wx.VERTICAL )


        self.m_textCtrl = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, 

            wx.DefaultSize, 0 )

        self.m_textCtrl.SetFont( wx.Font( 20, 74, 90, 92, False, "Comic Sans MS" ) )


        bSizer1.Add( self.m_textCtrl, 0, wx.ALL, 5 )


        self.m_button_start = wx.Button( self, wx.ID_ANY, u"Start", wx.DefaultPosition, 

            wx.DefaultSize, 0 )

        bSizer1.Add( self.m_button_start, 0, wx.ALL, 5 )


        self.m_button_stop = wx.Button( self, wx.ID_ANY, u"Stop", wx.DefaultPosition, 

            wx.DefaultSize, 0 )

        bSizer1.Add( self.m_button_stop, 0, wx.ALL, 5 )


        self.SetSizer( bSizer1 )

        self.Layout()


        # Connect Events

        self.m_button_start.Bind( wx.EVT_LEFT_DOWN, self.m_button_startOnLeftDown )

        self.m_button_stop.Bind( wx.EVT_LEFT_DOWN, self.m_button_stopOnLeftDown )


    def m_button_startOnLeftDown( self, event ):

        self.counter = Counter(0, 1, self.m_textCtrl)

        self.counter.start()

        pass


    def m_button_stopOnLeftDown( self, event ):

        count = self.counter.finish()

        print(count)  # test

        pass



class Counter(threading.Thread):

    """

    create a thread object that will do the counting in a separate thread

    """

    def __init__(self, value, increment, display_widget):

        # init the thread

        threading.Thread.__init__(self)

        # initial value

        self.value = value

        # amount to increment

        self.increment = increment

        # controls the while loop in the run command

        self.alive = False

        # display widget

        self.display = display_widget


    def run(self):

        "this will run in a separate thread"

        self.alive = True

        while self.alive:

            # delay for 1 second

            time.sleep(1)

            self.value += self.increment

            print(self.value)  # test

            self.display.SetValue(str(self.value))


    def peek(self):

        "return the current value"

        return self.value


    def finish(self):

        "close the thread, return final value"

        # stop the while loop in 'run'

        self.alive = False

        return self.value




app = wx.App(0)

# create a MyFrame instance and show the frame

frame = MyFrame(None)

frame.Center()

frame.Show()

app.MainLoop()

Tidak ada komentar: