简体   繁体   中英

wxPython: Calculator won't display value in Textctrlenter

I am working on a Calculator using wxPyhton. I'm having trouble on displaying the value in a Textctrlenter.

self.nameTxt = wx.TextCtrl( self, wx.ID_ANY,"",pos=(10,20),size=(260,30))

self.clickcount1 = 1
one = self.clickcount1

getBtn = wx.Button(self, self.clickcount1,label="1",pos=(10,60),size(40,40))
btn.Bind(wx.EVT_BUTTON, lambda btnClick, temp=button_name: 
self.OnButton(btnClick(1), temp) )

Your lamba function is incorrect.
The size(40,40) parameter in getBtn, should be size=(40,40)
The Bind should be on getBtn not btn
The variable one is not used at all
You're using self.clickcount as the button id , don't, use -1 or wx.ID_ANY and wxpython will generate a unique id for you.
I assume that you are trying to do something like this:

import wx

class TestFrame(wx.Frame):
    def __init__(self, *args):
        wx.Frame.__init__(self, *args)
        self.nameTxt = wx.TextCtrl( self, wx.ID_ANY,"",pos=(10,20),size=(260,30))
        getBtn1 = wx.Button(self, id=-1, label="1", pos=(10,60), size=(40,40))
        getBtn1.Bind(wx.EVT_BUTTON, lambda event: self.OnButton(event, button=1) )
        getBtn2 = wx.Button(self, id=-1, label="2", pos=(50,60), size=(40,40))
        getBtn2.Bind(wx.EVT_BUTTON, lambda event: self.OnButton(event, button=2) )
        getBtn3 = wx.Button(self, id=-1, label="3", pos=(90,60), size=(40,40))
        getBtn3.Bind(wx.EVT_BUTTON, lambda event: self.OnButton(event, button=3) )
        self.Show()

    def OnButton(self, event, button):
        print ("Button number ", button)
        curr_value = self.nameTxt.GetValue()
        # If a value exists add to it, otherwise display value of pressed button
        try:
            curr_value = int(curr_value) + button
            self.nameTxt.SetValue(str(curr_value))
        except:
            self.nameTxt.SetValue(str(button))

if __name__ == "__main__":
    app = wx.App()
    myframe = TestFrame(None, -1, "Calculator Test")
    app.MainLoop()

在此处输入图片说明

With regard to your comment about wanting "11111111":
change the function OnButton to:

def OnButton(self, event, button):
    print ("Button number ", button)
    curr_value = self.nameTxt.GetValue()
    curr_value = curr_value + str(button)
    self.nameTxt.SetValue(curr_value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM