【问题标题】:wxPython: Calculator won't display value in TextctrlenterwxPython:计算器不会在 Textctrlenter 中显示值
【发布时间】:2018-10-16 11:33:09
【问题描述】:

我正在使用 wxPyhton 开发计算器。我在 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) )

【问题讨论】:

    标签: python wxpython


    【解决方案1】:

    您的lamba 函数不正确。
    getBtn 中的size(40,40) 参数应该是size=(40,40)
    Bind 应该在 getBtn 而不是 btn
    变量one根本没有使用
    您使用self.clickcount 作为按钮id,不要使用-1wx.ID_ANY,wxpython 将为您生成一个唯一的ID。
    我假设你正在尝试做这样的事情:

    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()
    

    关于你想要“11111111”的评论:
    将函数OnButton 更改为:

    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)
    

    【讨论】:

    • 感谢 Rolf of Saxony 的建议。
    • 您好,我想按一个 btn 像这样显示 10 次(1111111111),谢谢
    • Hello Rolf of Saxony 我尝试执行代码,但显示 Typing Error: can only concatenate str (not "int") to str
    • @vinaydoddapaneni 使用curr_value = curr_value + str(button) 而不是curr_value = curr_value + button
    • 你好,萨克森的罗尔夫,我正在寻找补充,例如拿一个计算器样本,首先按下按钮“one”“+”“2”“=”然后它必须显示答案,再次感谢您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    相关资源
    最近更新 更多