wxPython: updating style of StaticText from event generated by button

Discussion in 'Python' started by KvS, Nov 1, 2005.

  1. KvS

    KvS Guest

    Hi all,

    I'm pretty new to (wx)Python so plz. don't shoot me if I've missed
    something obvious ;). I have a panel inside a frame, on which a Button
    and a StaticText is placed:

    self.panel = wx.Panel(self,-1)
    self.button = wx.Button(self.panel,-1,"Klikkerdeklik")
    self.button.SetPosition((200,40))
    self.Bind(wx.EVT_BUTTON, self.VeranderLabel, self.button)
    self.text1 = wx.StaticText(self.panel, -1, "Dikke Henk en gekke
    Greetje", size=(100,50), pos=(15,20), style=wx.RAISED_BORDER)

    and via the even handler I try to give StaticText a different style:

    def VeranderLabel(self, event):
    if self.text1.GetWindowStyle() == wx.SUNKEN_BORDER:
    self.text1.SetWindowStyle(wx.RAISED_BORDER)
    self.text1.Refresh()
    self.text1.Update()
    else:
    self.text1.SetWindowStyle(wx.SUNKEN_BORDER)
    self.text1.Refresh()
    self.text1.Update()

    Although the style is indeed updated (checked by printing style to
    console) the appearance of the StaticText stays the same. What am I
    missing here?

    Thanks in advance.

    - Kees
    KvS, Nov 1, 2005
    #1
    1. Advertising

  2. KvS

    KvS Guest

    Thanks :), I'll give both of your hints a try. What I basically want to
    do is have something like an "old style" button in win xp that's either
    "up" or "down", since I couldn't find a more straightforward method I
    thought taking a text widget and adjusting the border at mouse click
    would be the best alternative no?

    One question then, what's the use of having a method like
    SetWindowStyle() if it doesn't really do anything at runtime? Is it
    indeed just intended for the few widgets that do support it?

    - Kees
    KvS, Nov 1, 2005
    #2
    1. Advertising

  3. KvS

    KvS Guest


    > I would suggest you to take a look to the wxPython "custom" buttons; you can
    > find them in the demo, under "Custom Controls" ==> "GenericButtons". There
    > you will find "old style" buttons, that can be simple buttons or toggle
    > buttons (with "up" and "down" states). I have XP, and they look like "old
    > style" buttons.


    Pff. Sorry, apparently I just completely missed that specific part of
    the demo although I've been using the demo all the time so far. :(.

    > This is more a wxWidgets question that a wxPython one. However, quoting the
    > wxWidgets manual:


    Yes, I had looked it up in the manual, but "Please note that some
    styles cannot be changed after the window creation [...]" is hardly the
    same as "In general you *can not* change in runtime the style of a
    widget. Only a
    very limited subset of the wxPython widgets supports style changes in
    runtime" right ;).

    Thanks for the great help!

    - Kees
    KvS, Nov 2, 2005
    #3
  4. Hello Kees,

    > and via the even handler I try to give StaticText a different style:


    In general you *can not* change in runtime the style of a widget. Only a
    very limited subset of the wxPython widgets supports style changes in
    runtime. I would suggest you 2 alternatives:

    1) Use wx.lib.stattext ==> GenStaticText (but I am not sure it will work, I
    don't think so, but you can try it);
    2) Every time you call your event binder, Destroy() the StaticText with the
    old style and create a new one with the style you need. For example
    (untested code!!!):

    def VeranderLabel(self, event):

    if self.text1.GetWindowStyle() == wx.SUNKEN_BORDER:
    MyStyle = wx.RAISED_BORDER
    else:
    MyStyle = wx.SUNKEN_BORDER

    self.text1.Destroy()
    self.text1 = wx.StaticText(self.panel, -1, "Dikke Henk en gekke Greetje",
    size=(100,50), pos=(15,20),
    style=MyStyle)
    self.Refresh()


    HTH.

    Andrea.

    --
    "Imagination Is The Only Weapon In The War Against Reality."
    http://xoomer.virgilio.it/infinity77
    "KvS" <> ha scritto nel messaggio
    news:...
    > Hi all,
    >
    > I'm pretty new to (wx)Python so plz. don't shoot me if I've missed
    > something obvious ;). I have a panel inside a frame, on which a Button
    > and a StaticText is placed:
    >
    > self.panel = wx.Panel(self,-1)
    > self.button = wx.Button(self.panel,-1,"Klikkerdeklik")
    > self.button.SetPosition((200,40))
    > self.Bind(wx.EVT_BUTTON, self.VeranderLabel, self.button)
    > self.text1 = wx.StaticText(self.panel, -1, "Dikke Henk en gekke
    > Greetje", size=(100,50), pos=(15,20), style=wx.RAISED_BORDER)
    >
    > and via the even handler I try to give StaticText a different style:
    >
    > def VeranderLabel(self, event):
    > if self.text1.GetWindowStyle() == wx.SUNKEN_BORDER:
    > self.text1.SetWindowStyle(wx.RAISED_BORDER)
    > self.text1.Refresh()
    > self.text1.Update()
    > else:
    > self.text1.SetWindowStyle(wx.SUNKEN_BORDER)
    > self.text1.Refresh()
    > self.text1.Update()
    >
    > Although the style is indeed updated (checked by printing style to
    > console) the appearance of the StaticText stays the same. What am I
    > missing here?
    >
    > Thanks in advance.
    >
    > - Kees
    >
    Andrea Gavana, Nov 2, 2005
    #4
  5. Hello Kees,

    > Thanks :), I'll give both of your hints a try. What I basically want to
    > do is have something like an "old style" button in win xp that's either
    > "up" or "down", since I couldn't find a more straightforward method I
    > thought taking a text widget and adjusting the border at mouse click
    > would be the best alternative no?


    I would suggest you to take a look to the wxPython "custom" buttons; you can
    find them in the demo, under "Custom Controls" ==> "GenericButtons". There
    you will find "old style" buttons, that can be simple buttons or toggle
    buttons (with "up" and "down" states). I have XP, and they look like "old
    style" buttons. Moreover (just to promote my web site and my widgets, freely
    available to all wxPython users ;-)))) ), if you would like to use
    "non-rectangular" buttons-toggle buttons, you could take a look at:

    http://xoomer.virgilio.it/infinity77/eng/freeware.html#shapedbutton

    > One question then, what's the use of having a method like
    > SetWindowStyle() if it doesn't really do anything at runtime? Is it
    > indeed just intended for the few widgets that do support it?


    This is more a wxWidgets question that a wxPython one. However, quoting the
    wxWidgets manual:

    wxWindow::SetWindowStyleFlag
    virtual void SetWindowStyleFlag(long style)

    Sets the style of the window. Please note that some styles cannot be changed
    after the window creation and that Refresh() might be called after changing
    the others for the change to take place immediately.

    SetWindowStyleFlag is the same as SetWindowStyle. I don't have an answer to
    your question, but maybe some wxWidgets expert can give you some more hints
    (I don't use wxWidgets, only wxPython).

    HTH.

    Andrea.


    "Imagination Is The Only Weapon In The War Against Reality."
    http://xoomer.virgilio.it/infinity77
    Andrea Gavana, Nov 3, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kasek
    Replies:
    0
    Views:
    2,999
    Kasek
    Jan 30, 2004
  2. python newbie
    Replies:
    3
    Views:
    374
    python newbie
    Sep 26, 2004
  3. python newbie
    Replies:
    1
    Views:
    362
    Cliff Wells
    Sep 27, 2004
  4. StvB
    Replies:
    2
    Views:
    601
    python newbie
    Sep 27, 2004
  5. David

    wxPython: StaticText Event

    David, Sep 8, 2006, in forum: Python
    Replies:
    4
    Views:
    850
    David
    Sep 8, 2006
Loading...

Share This Page