#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Posts
    4
    Rep Power
    0

    Q:convert to integer in wxpython


    i'm using wxpython textcontrol object, i want to enter numbers .....
    how can i convert str to int or float.
    int(text_ctrl1.GetValue()) dosen't work !!!!
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Code:
    self.Bind(EVT_TEXT, self.SaveText, self.text_ctrl1);
    def SaveText(self, event):
        self.current_text = event.GetString();
    self.your_new_value = int(self.current_text); # or float(self.current_text)
    This might be what you're looking for. Why doesn't your code contain 'self'? You should be using a class (unless you are, and you just didn't type in the self part).
    This code records the current text, as it is being typed, into the class variable, current_text. This would work only for something you can type into.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Originally Posted by a.barazi
    i'm using wxpython textcontrol object, i want to enter numbers .....
    how can i convert str to int or float.
    int(text_ctrl1.GetValue()) dosen't work !!!!
    int(text_ctrl1.GetValue()) should work, unless the user enters a string that is not a valid number (e.g. an empty string), in which case it will throw an exception. What happened when you tried?

    You can use a wxValidator to restrict the user to only entering numbers - see the docs and wxPython demo for details. You will still need to handle the special case of him entering an empty string.

    There is also a wxIntCtrl specifically to do what you want, and will return an int value directly.

    You could also use a wxSpinButton if you only want the user to enter a small number.

    Dave - The Developers' Coach
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Posts
    4
    Rep Power
    0

    thanks guy


    i'm not an expert, but i'm using python as a way to make my mind work, discovering python is like having sex!!!! the simple of this languge make u love it too much.
    yes DevCoach may be there was something wrong in may code because i write it very fast, i'll try to write it again with more caution, would u put just a simple example showing how to use wxIntCtrl, wxValidator.
    yegg ur help was realy very usefull for me, but i think there is some thing wrong in ur code.
    here is the code which i use (it's not a usful program, it's just to explain the idea of converting the string to integer)

    #!/usr/bin/env python
    # -*- coding: ISO-8859-1 -*-

    import wx

    class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
    kwds["style"] = wx.DEFAULT_FRAME_STYLE
    wx.Frame.__init__(self, *args, **kwds)
    self.panel_1 = wx.Panel(self, -1)
    self.text_ctrl_1 = wx.TextCtrl(self.panel_1, -1, "")
    self.button_1 = wx.Button(self.panel_1, -1, "button_1")

    self.__set_properties()
    self.__do_layout()

    def __set_properties(self):

    self.SetTitle("frame_1")

    def __do_layout(self):

    sizer_1 = wx.BoxSizer(wx.VERTICAL)
    sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
    sizer_2.Add(self.text_ctrl_1, 0, wx.FIXED_MINSIZE, 0)
    sizer_2.Add(self.button_1, 0, wx.FIXED_MINSIZE, 0)
    self.panel_1.SetAutoLayout(True)
    self.panel_1.SetSizer(sizer_2)
    sizer_2.Fit(self.panel_1)
    sizer_2.SetSizeHints(self.panel_1)
    sizer_1.Add(self.panel_1, 1, wx.EXPAND, 0)
    self.SetAutoLayout(True)
    self.SetSizer(sizer_1)
    sizer_1.Fit(self)
    sizer_1.SetSizeHints(self)
    self.Layout()
    wx.EVT_BUTTON(self, self.button_1.GetId(), self.pushB)
    wx.EVT_TEXT(self, self.text_ctrl_1.GetId(),self.SaveText)
    def pushB(self, event):
    for i in range(0,int(self.current_text)):
    print i
    for i in range(0,int(self.text_ctrl_1.GetValue())):
    print i

    def SaveText(self, event):
    self.current_text = event.GetString()
    print self.current_text
    self.your_new_value = int(self.current_text) # or float(self.current_text)
    print self.your_new_value
    print type(self.your_new_value)
    class MyApp(wx.App):
    def OnInit(self):
    wx.InitAllImageHandlers()
    frame_1 = MyFrame(None, -1, "")
    self.SetTopWindow(frame_1)
    frame_1.Show(1)
    return 1


    if __name__ == "__main__":
    app = MyApp(0)
    app.MainLoop()

    it's working well
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    You cannot do:
    Code:
    wx.EVT_BUTTON(self, self.button_1.GetId(), self.pushB)
    wx.EVT_TEXT(self, self.text_ctrl_1.GetId(),self.SaveText)
    Remove the .GetID() part from that code.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Posts
    4
    Rep Power
    0
    Originally Posted by †Yegg†
    You cannot do:
    Code:
    wx.EVT_BUTTON(self, self.button_1.GetId(), self.pushB)
    wx.EVT_TEXT(self, self.text_ctrl_1.GetId(),self.SaveText)
    Remove the .GetID() part from that code.
    sorry it will not work
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Posts
    4
    Rep Power
    0

    yes 100% DevCoach


    there was a mistake in my code (not the code which i write here in this fourm)


    thaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaanks.

IMN logo majestic logo threadwatch logo seochat tools logo