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

    Join Date
    Aug 2004
    Posts
    17
    Rep Power
    0

    Writing A Prog with wxPython


    I've made a Python file that has sliders. I noticed when using the wxSL_LABELS style in wxslider, a box on the side of the slider appears and you can see the value of the slider. The only problem with that is I want the user to be able to type a number in that box and the slider's value will be that number.

    So I stopped using wxSL_LABELS and instead i made a small textbox in which the user can type in a number and the slider thumb will move to that number. And when you move the slider the number in the textbox changes to the value of the slider.

    To do this i used lists (aka arrays). Right now my prog's sliders only go up to 10. And as you can guess by looking at my program(below) that I would like the slider to be able to go up to 255(since the sliders are going to be changing the RBG values of an object). Now my problem is that I don't want to have to write an list with 256 values. I was wondering if anybody could show me an easier way about this.

    -----------------------------------------------------------
    from wxPython.wx import *

    value1 = ["0","1","2","3","4","5","6","7","8","9","10"]
    value1a = {"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,"10":10}

    value2 = ["0","1","2","3","4","5","6","7","8","9","10"]
    value2a = {"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,"10":10}

    value3 = ["0","1","2","3","4","5","6","7","8","9","10"]
    value3a = {"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,"10":10}


    class MainWindow(wxFrame):
    def __init__(self,parent,id,title):
    wxFrame.__init__(self, parent,wxID_ANY,title, wxDefaultPosition,
    wxSize(800,500))

    #StatusBar
    self.CreateStatusBar(2)
    self.SetStatusText("")

    #Sliders
    self.Text1 = wxStaticText(self, -1, "Red Value", (30, 45))
    self.slider1 = wxSlider(self,101,5, 0, 10, (30, 60), (250, -1), wxSL_HORIZONTAL | wxSL_AUTOTICKS)
    self.slider1.SetTickFreq(1, 1)
    self.textBox1 = wxTextCtrl(self,1001,value1[self.slider1.GetValue()],(5,60),(20,20))
    self.textBox1.SetMaxLength(2)

    self.Text2 = wxStaticText(self, -1, "Green Value", (30, 85))
    self.slider2 = wxSlider(self,102,5, 0, 10, (30, 100), (250, -1), wxSL_HORIZONTAL | wxSL_AUTOTICKS)
    self.slider2.SetTickFreq(1, 1)
    self.textBox2 = wxTextCtrl(self,1002,value2[self.slider2.GetValue()],(5,100),(20,20))
    self.textBox2.SetMaxLength(2)

    self.Text3 = wxStaticText(self, -1, "Blue Value", (30, 125))
    self.slider3 = wxSlider(self,103,5, 0, 10, (30, 140), (250, -1), wxSL_HORIZONTAL | wxSL_AUTOTICKS)
    self.slider3.SetTickFreq(1, 1)
    self.textBox3 = wxTextCtrl(self,1003,value3[self.slider3.GetValue()],(5,140),(20,20))
    self.textBox3.SetMaxLength(2)


    #Event Table
    EVT_COMMAND_SCROLL(self,101,self.OnSlide1)
    EVT_COMMAND_SCROLL(self,102,self.OnSlide2)
    EVT_COMMAND_SCROLL(self,103,self.OnSlide3)

    EVT_TEXT(self,1001,self.OnType1)
    EVT_TEXT(self,1002,self.OnType2)
    EVT_TEXT(self,1003,self.OnType3)

    #Functions
    def OnSlide1(self, event):
    self.textBox1.Clear()
    self.textBox1.AppendText(value1[self.slider1.GetValue()])

    def OnSlide2(self, event):
    self.textBox2.Clear()
    self.textBox2.AppendText(value2[self.slider2.GetValue()])

    def OnSlide3(self, event):
    self.textBox3.Clear()
    self.textBox3.AppendText(value3[self.slider3.GetValue()])



    def OnType1(self, event):
    sliderValue1 = value1a[self.textBox1.GetValue()]
    self.slider1.SetValue(sliderValue1)

    def OnType2(self, event):
    sliderValue2 = value1a[self.textBox2.GetValue()]
    self.slider2.SetValue(sliderValue2)

    def OnType3(self, event):
    sliderValue3 = value1a[self.textBox3.GetValue()]
    self.slider3.SetValue(sliderValue3)

    #All the Rest

    class MyApp(wxApp):
    def OnInit(self):
    frame = MainWindow(NULL, -1, "XP Custumizer")
    frame.Show(true)
    self.SetTopWindow(frame)
    return true

    app = MyApp(0)
    app.MainLoop()

    -----------------------------------------------------------
    Also the reason i'm not just using "self.textBox1.GetValue() = self.slider1.GetValue()" is because textBox1 uses strings and slider1 uses integers.

    And for you wiseguys out there I thought about the fact that I might have been able to write the list with the time a spent typing this message.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    109
    Rep Power
    10
    Can't you just use a range?

    Code:
    >> value = range(255)
    >> print value
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254]
    >>> dict = {}
    >>> print dict
    {}
    >>> for item in value:
    	dict["%d" % item] = item
    
    	
    >>> print dict
    {'216': 216, '217': 217, '214': 214, '215': 215, '212': 212, '213': 213, '210': 210, '211': 211, '165': 165, '218': 218, '219': 219, '133': 133, '132': 132, '131': 131, '130': 130, '137': 137, '136': 136, '135': 135, '134': 134, '139': 139, '138': 138, '166': 166, '24': 24, '25': 25, '26': 26, '27': 27, '20': 20, '21': 21, '22': 22, '23': 23, '160': 160, '28': 28, '29': 29, '161': 161, '0': 0, '4': 4, '8': 8, '163': 163, '119': 119, '120': 120, '121': 121, '122': 122, '123': 123, '124': 124, '125': 125, '126': 126, '127': 127, '128': 128, '129': 129, '167': 167, '118': 118, '59': 59, '58': 58, '55': 55, '54': 54, '57': 57, '56': 56, '51': 51, '50': 50, '53': 53, '52': 52, '164': 164, '201': 201, '199': 199, '179': 179, '200': 200, '195': 195, '194': 194, '197': 197, '178': 178, '191': 191, '190': 190, '193': 193, '192': 192, '115': 115, '114': 114, '88': 88, '89': 89, '111': 111, '110': 110, '113': 113, '112': 112, '82': 82, '83': 83, '80': 80, '81': 81, '86': 86, '87': 87, '84': 84, '85': 85, '251': 251, '198': 198, '206': 206, '226': 226, '3': 3, '177': 177, '254': 254, '7': 7, '247': 247, '225': 225, '245': 245, '244': 244, '108': 108, '109': 109, '241': 241, '240': 240, '243': 243, '242': 242, '102': 102, '103': 103, '100': 100, '101': 101, '106': 106, '107': 107, '104': 104, '105': 105, '39': 39, '38': 38, '33': 33, '32': 32, '31': 31, '30': 30, '37': 37, '36': 36, '35': 35, '34': 34, '246': 246, '252': 252, '205': 205, '223': 223, '176': 176, '60': 60, '61': 61, '62': 62, '63': 63, '64': 64, '65': 65, '66': 66, '67': 67, '68': 68, '69': 69, '175': 175, '174': 174, '173': 173, '172': 172, '171': 171, '170': 170, '203': 203, '222': 222, '181': 181, '253': 253, '248': 248, '182': 182, '183': 183, '180': 180, '2': 2, '162': 162, '187': 187, '184': 184, '6': 6, '220': 220, '186': 186, '188': 188, '189': 189, '202': 202, '196': 196, '221': 221, '185': 185, '99': 99, '98': 98, '168': 168, '169': 169, '229': 229, '228': 228, '91': 91, '90': 90, '93': 93, '92': 92, '95': 95, '94': 94, '97': 97, '96': 96, '11': 11, '10': 10, '13': 13, '12': 12, '15': 15, '14': 14, '17': 17, '16': 16, '19': 19, '18': 18, '117': 117, '116': 116, '204': 204, '224': 224, '151': 151, '150': 150, '153': 153, '152': 152, '155': 155, '154': 154, '157': 157, '156': 156, '159': 159, '158': 158, '234': 234, '238': 238, '239': 239, '227': 227, '207': 207, '235': 235, '236': 236, '237': 237, '230': 230, '231': 231, '232': 232, '233': 233, '48': 48, '49': 49, '46': 46, '47': 47, '44': 44, '45': 45, '42': 42, '43': 43, '40': 40, '41': 41, '1': 1, '5': 5, '9': 9, '146': 146, '147': 147, '144': 144, '145': 145, '142': 142, '143': 143, '140': 140, '141': 141, '209': 209, '208': 208, '148': 148, '149': 149, '77': 77, '76': 76, '75': 75, '74': 74, '73': 73, '72': 72, '71': 71, '70': 70, '79': 79, '78': 78, '249': 249, '250': 250}
    I'm a little out of me league here but this seems easy enough on the surface
  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
    You don't need an array of strings at all, or a dictionary. Just use 'str' to convert integers to strings, and 'int' to strings to integers.

    Code:
    >>> int('123')
    123
    >>> str(123)
    '123'
    >>>
    Dave - The Developers' Coach

    P.S. Please, please, please remember to put your source in [ code ] tags - I feel that if someone can't be bothered to make their code readable, I can't be bothered to read it.
    Last edited by DevCoach; August 19th, 2004 at 03:42 PM.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    17
    Rep Power
    0
    thanks for your help

IMN logo majestic logo threadwatch logo seochat tools logo