Simpel counter (new wxpython user)

Hi

im new to wxpython and im trying to make a simpel counter. (a static text that you can chance with a plus and a minus button). Here is what i have done so far.

import wx

class my_frame(wx.Frame):

def init(self):

t1 = 0

t2 = 0

t3 = 0

#Make frame

wx.Frame.init(self, None, style=wx.MINIMIZE_BOX | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX, title=‘Diablo 3 Counter’, size=(200, 190))

panel = wx.Panel(self)

#Make text

self.st1 = wx.StaticText(panel,pos=(60,10), size=(80, 40))

self.st1.SetFont(wx.Font(24, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, u’Consolas’))

self.st1.SetLabel(‘0’)

#Make minus button

self.m1 = wx.Button(panel, label=’-’, pos=(10,10), size=(40, 40))

self.m1.SetFont(wx.Font(24, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, u’Consolas’))

self.m1.Bind(wx.EVT_BUTTON, self.mClick(self.st1))

#Make plus button

self.p1 = wx.Button(panel, label=’+’, pos=(150,10), size=(40, 40))

self.p1.SetFont(wx.Font(24, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, u’Consolas’))

self.p1.Bind(wx.EVT_BUTTON, self.pClick(self.st1))

self.Show()

def mClick(self, st):

t = int(st.GetLabel())

st.SetLabel(str(t-1))

def pClick(self, st):

t = int(st.GetLabel())

st.SetLabel(str(t+1))

app = wx.App()

my_frame()

app.MainLoop()

When i run the program it seems to run the pClick and mClick once and then nothing works

Hope someone can help

Hi Morten,

Hi

im new to wxpython and im trying to make a simpel counter. (a static text that you can chance with a plus and a minus button). Here is what i have done so far.

...

When i run the program it seems to run the pClick and mClick once and then nothing works
Hope someone can help

Attached is a changed version which I think does what you want.

You might also want to check out the wxPython demo, in particular the SpinCtrl and SpinCtrlDouble widgets.

Werner

plusminus.py (1.32 KB)

···

On 9/17/2014 10:56, Morten Presmann wrote:

You have gotten wrong how events work in wxPython. An exemplary explanation is given in http://wiki.wxpython.org/wxPython%20by%20Example#Adding_an_Event_Handler, see the naked documentation of “Bind” (in the Phoenix docs).

Basically you have to bind as follows:
self.m1.Bind(wx.EVT_BUTTON, self.mClick) # no argument for handler function!

``

and in the event handler do the following:

def mClick(self, event):
    t = int(st.GetLabel())
    self.st1.SetLabel(str(t-1))

``

If you want to pass on custom arguments in your event handling function, see http://wiki.wxpython.org/Passing%20Arguments%20to%20Callbacks, but you should hardly ever need something like that.

···

On Wednesday, September 17, 2014 10:56:54 AM UTC+2, Morten Presmann wrote:

self.m1.SetFont(wx.Font(24, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, u’Consolas’))
self.m1.Bind(wx.EVT_BUTTON, self.mClick(self.st1))

def mClick(self, st):
t = int(st.GetLabel())

st.SetLabel(str(t-1))

Morten Presmann wrote:

Hi

im new to wxpython and im trying to make a simpel counter. (a static
text that you can chance with a plus and a minus button). Here is what
i have done so far.

import wx

class my_frame(wx.Frame):
    def __init__(self):
        t1 = 0
        t2 = 0
        t3 = 0

        #Make frame
        wx.Frame.__init__(self, None, style=wx.MINIMIZE_BOX |
wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX, title='Diablo 3 Counter',
size=(200, 190))

        panel = wx.Panel(self)

        #Make text
        self.st1 = wx.StaticText(panel,pos=(60,10), size=(80, 40))
        self.st1.SetFont(wx.Font(24, wx.DEFAULT, wx.NORMAL, wx.NORMAL,
0, u'Consolas'))
        self.st1.SetLabel('0')

        #Make minus button
        self.m1 = wx.Button(panel, label='-', pos=(10,10), size=(40, 40))
        self.m1.SetFont(wx.Font(24, wx.DEFAULT, wx.NORMAL, wx.NORMAL,
0, u'Consolas'))
        self.m1.Bind(wx.EVT_BUTTON, self.mClick(self.st1))

You have several problems.

First, the "Bind" method expects you to supply a function. That's not
what you are doing. You are CALLING a function, which happens to return
None. You then pass the result of that function (that is, None) to
Bind. You should use:

        self.m1.Bind(wx.EVT_BUTTON, self.mClick)
and similarly:
        self.p1.Bind(wx.EVT_BUTTON, self.pClick)

    def mClick(self, st):
        t = int(st.GetLabel())
        st.SetLabel(str(t-1))

The object that gets passed to an event callback is an wx.Event object.
That event object does contain the object that triggered the event, in
this case your button. That's NOT the control that has your text,
however. That control is in self.st1. So, do this:

    def mClick(self, st):
        t = int(self.st1.GetLabel())
        self.st1.SetLabel(str(t-1))

    def pClick(self, st):
        t = int(self.st1.GetLabel())
        self.st1.SetLabel(str(t+1))

That allows your app to run as expected.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.