数字键盘

时间:2016-07-13 22:02:59

标签: python wxpython real-time

我想设计一个数字键盘,当我点击一个按钮时,我不知道使用哪个功能来实时显示,当按钮1被点击时,如文本控制器显示为1,就像计算器显示一样。当串联点击1234个按钮时,它可以显示为1234。我想知道如何添加退格,清除函数

import wx

class iCal(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Text")
        panel = MainPanel(self)

class MainPanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent)
        self.frame = parent 

        text_1 = wx.TextCtrl(self, value=float(num))

        button_0 = wx.Button(self, label="0")        
        button_1 = wx.Button(self, label="1")
        button_2 = wx.Button(self, label="2")

        button_0.Bind(wx.EVT_LEFT_DOWN,self.output0)
        button_1.Bind(wx.EVT_LEFT_DOWN,self.output1)
        button_2.Bind(wx.EVT_LEFT_DOWN,self.output2)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(text_1, 0, wx.ALL|wx.CENTER, 5)
        sizer.Add(button_0, 0, wx.ALL|wx.CENTER, 5)
        sizer.Add(button_1, 0, wx.ALL|wx.CENTER, 5)
        sizer.Add(button_2, 0, wx.ALL|wx.CENTER, 5)
        self.SetSizer(sizer)

    def self.output0(self,event):
        if float(num) > 0:
            num = num + str(0)

    def self.output1(self,event):
        num = num + str(1)

    def self.output1(self,event):
        num = num + str(1)



if __name__ == '__main__':
    app = wx.App(False)
    frame = iCal()
    frame.Show()
    app.MainLoop()

Numberic Keyboard

1 个答案:

答案 0 :(得分:0)

import wx

class iCal(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Text")
        panel = MainPanel(self)

class MainPanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent)
        self.frame = parent
        self.text_1 = wx.TextCtrl(self, value="0")
        button_0 = wx.Button(self, label="0")
        button_1 = wx.Button(self, label="1")
        button_2 = wx.Button(self, label="2")
        clear = wx.Button(self, label="Clear")

        button_0.Bind(wx.EVT_LEFT_DOWN,self.output0)
        button_1.Bind(wx.EVT_LEFT_DOWN,self.output1)
        button_2.Bind(wx.EVT_LEFT_DOWN,self.output2)
        clear.Bind(wx.EVT_LEFT_DOWN,self.clear)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.text_1, 0, wx.ALL|wx.CENTER, 5)
        sizer.Add(button_0, 0, wx.ALL|wx.CENTER, 5)
        sizer.Add(button_1, 0, wx.ALL|wx.CENTER, 5)
        sizer.Add(button_2, 0, wx.ALL|wx.CENTER, 5)
        sizer.Add(clear, 0, wx.ALL|wx.CENTER, 5)
        self.SetSizer(sizer)

    def output0(self,event):
        num = "0"
        my_string = self.text_1.GetValue() + num
        self.text_1.SetValue(my_string)

    def output1(self,event):
        num = "1"
        my_string = self.text_1.GetValue() + num
        self.text_1.SetValue(my_string)

    def output2(self,event):
        num = "2"
        my_string = self.text_1.GetValue() + num
        self.text_1.SetValue(my_string)

    def clear(self,event):
        my_string = self.text_1.GetValue()
        my_string = my_string[:len(my_string)-1]
        self.text_1.SetValue(my_string)

if __name__ == '__main__':
    app = wx.App(False)
    frame = iCal()
    frame.Show()
    app.MainLoop()
  1. 如果您要发布代码,请至少先运行代码,以确保您不会发布垃圾邮件。
  2. 在发布之前阅读有关操纵字符串的信息:Strings