wxPython: updating style of StaticText from event generated by button

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • KvS

    #1

    wxPython: updating style of StaticText from event generated by button

    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,"Klikkerdekli k")
    self.button.Set Position((200,4 0))
    self.Bind(wx.EV T_BUTTON, self.VeranderLa bel, self.button)
    self.text1 = wx.StaticText(s elf.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(s elf, event):
    if self.text1.GetW indowStyle() == wx.SUNKEN_BORDE R:
    self.text1.SetW indowStyle(wx.R AISED_BORDER)
    self.text1.Refr esh()
    self.text1.Upda te()
    else:
    self.text1.SetW indowStyle(wx.S UNKEN_BORDER)
    self.text1.Refr esh()
    self.text1.Upda te()

    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

    #2
    Re: wxPython: updating style of StaticText from event generated by button

    Hello Kees,
    [color=blue]
    > and via the even handler I try to give StaticText a different style:[/color]

    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(s elf, event):

    if self.text1.GetW indowStyle() == wx.SUNKEN_BORDE R:
    MyStyle = wx.RAISED_BORDE R
    else:
    MyStyle = wx.SUNKEN_BORDE R

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


    HTH.

    Andrea.

    --
    "Imaginatio n Is The Only Weapon In The War Against Reality."

    "KvS" <keesvanschaik@ gmail.com> ha scritto nel messaggio
    news:1130886746 .328723.109570@ f14g2000cwb.goo glegroups.com.. .[color=blue]
    > 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,"Klikkerdekli k")
    > self.button.Set Position((200,4 0))
    > self.Bind(wx.EV T_BUTTON, self.VeranderLa bel, self.button)
    > self.text1 = wx.StaticText(s elf.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(s elf, event):
    > if self.text1.GetW indowStyle() == wx.SUNKEN_BORDE R:
    > self.text1.SetW indowStyle(wx.R AISED_BORDER)
    > self.text1.Refr esh()
    > self.text1.Upda te()
    > else:
    > self.text1.SetW indowStyle(wx.S UNKEN_BORDER)
    > self.text1.Refr esh()
    > self.text1.Upda te()
    >
    > 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
    >[/color]


    Comment

    • KvS

      #3
      Re: wxPython: updating style of StaticText from event generated by button

      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

      Comment

      • Andrea Gavana

        #4
        Re: wxPython: updating style of StaticText from event generated by button

        Hello Kees,
        [color=blue]
        > 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?[/color]

        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:


        [color=blue]
        > 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?[/color]

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

        wxWindow::SetWi ndowStyleFlag
        virtual void SetWindowStyleF lag(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.

        SetWindowStyleF lag 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.


        "Imaginatio n Is The Only Weapon In The War Against Reality."



        Comment

        • KvS

          #5
          Re: wxPython: updating style of StaticText from event generated by button

          [color=blue]
          > 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.[/color]

          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. :(.
          [color=blue]
          > This is more a wxWidgets question that a wxPython one. However, quoting the
          > wxWidgets manual:[/color]

          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

          Comment

          Working...