wxPython: updating style of StaticText from event generated by button

K

KvS

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
 
K

KvS

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
 
K

KvS

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
 
A

Andrea Gavana

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.
 
A

Andrea Gavana

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top