I have search far and wide on how you can paint the background color of a button or GenButton with a pattern such as lines or cross hatch. I have seen examples of wx DirectContext so that you can draw objects with patterns instead of just solid colors but it seems that this is only for specific shapes and not the color of button objects. Does the dc or gc library allow to paint on these objects. I know that I have to create an event handler for OnPaint and OnResize but I may be missing some steps so that it applies this to the button itself.
The wxPython package uses native widgets in its core widgets as much as possible. Thus, the wx.Button widget is going to be a native widget that you can only modify via the methods mentioned in the documentation. As Igor mentioned, you can try using SetBackgroundColour() or SetForegroundColour(), although depending on your platform's button widget, they may or may not work.
What you really want is a custom widget. I recommend checking out the GenericButtons, PlateButton and GradientButton for examples. You might even be able to use a GenericButton directly and paint its background as you mentioned.
wx.Button object represents a native control. And so unfortunately you can't manipulate how the native control paints itself.
You can try SetBackgroundColour()/SetForegroundColour() but this is as far as you can go.
Related
Is there some way I can create custom styles in wxPython? I searched all the docs and websites referring to wxPython styling and could find nothing. Do I have to create the custom style using the wxPython canvas?
wxPython uses native widgets in its core widgets as much as possible. Most of the widgets are going to be "native" to the system you are using, so unfortunately you can't fully manipulate how the control paints itself.
Sometimes, you can modify widgets via the methods mentioned in the documentation, such as using SetBackgroundColour() or SetForegroundColour(). Depending on your OS's widget, they may or may not work.
The other option is to use the wx.PaintDC, wx.ClientDC, wx.WindowDC, wx.ScreenDC and/or wx.MemoryDC to draw custom widgets directly.
I am working on a small app that has some checkboxes. The app will be run on Windows 7 machines with the Windows Classic theme set. This means that all the checkboxes will have bezeled borders as shown in the screenshot below:
I wonder if there is a way to set the check boxes to have no bezel. If that is not possible, is it possible to custom paint/draw check boxes with no bezel? I have not been able to find examples. Thanks!
It really depends on the platform and/or the theme, and whether the native widget supports alternate views and such. So, in other words, for questions about customizing the LnF of native widgets in general, the answer is "maybe" but it probably depends on a number of factors that are not under wx's control.
On the other hand, making custom controls is much easier than most people think it will be. For something like a checkbox you'll basically just need to provide an event handler for EVT_PAINT that draws the current state of the widget using a wx.PaintDC, plus handlers for mouse and keyboard events to interact with the user. There are several examples in the wx.lib package that you can use for inspiration, such as wx.lib.stattext or wx.lib.buttons.
Maybe I'm jumping into the deep end, but I'll give it a shot.
Here are some useful features of Tkinter:
The Tkinter Canvas widget is an object oriented drawing canvas. The elements of the drawing are essentially widgets themselves, as they can be moved, modified, and bound to events.
Tkinter uses bindings to trigger callbacks. The event is passed as a string. Custom events can be easily created with event_generate.
Tkinter has the after method, which waits for a specified amount of time without freezing the GUI.
Tkinter has predefined fonts like TkDefaultFont, and colors like systemButtonFace, which are dependant on the system.
My questions are:
What are the pyQt equivalents of these features (especially the bold ones)?
How can I "bind" elements of a widget (e.g. the label of a checkbutton only) to an event?
In Qt and PyQt events are called signals and you bind to them using slots (docs here). Generally speaking what you do define a slot with an # decorator.
class WindowImpl (QtGui.QMainWindow, Ui_TremorMain, Ui_Graphs):
def __init__ (self, buffer, parent = None, configuration = None):
# do some initialisation here (not GUI setup however)
#QtCore.pyqtSlot(int, name="on_confSelectorCombo_currentIndexChanged")
def confChanged (self, newConf):
# do some stuff here to handle the event
The above would be triggered on the currentIndexChanged event of an object called confSelectorCombo. The setup of the confSelectorCombo is done in the GUI builder or Qt Creator as Nokia has decided to call it. This really is what you want to use to get started. There's tutorials here on using Qt Creator. Obviously you'll want to go through the docs and see what signals are emitted by which widgets.
As for the font stuff all I know is what it says on the docs:
If you have not set a font for your application then the default font on your
machine will be used, and the default font can be different on different
machines. On Windows the default Windows font is used, on X11 the one in qtrc
can be used. If a default font can’t be found, then a font specified by Qt
will be used.
The QStyleSheet and QStyle act as proxies for changing the appearance of widgets (QStylesheet,QStyle).
As for making the application wait I found this
QTime dieTime = QTime::currentTime().addSecs(2);
while( QTime::currentTime() < dieTime ):
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
There is also QThread.sleep() (docs), depending on what kind of an effect you want. Probably also worth looking at the threading support over at Qt docs
Overall in finding information about how to do stuff in PyQt I have found it surprisingly useful to look at the Qt documentation and then just writing the stuff in Python. 9 times out of 10 this works. On another note, it's probably also worth looking into PySide which is another python Qt library. I've haven't used myself before as it has been in the works previously but I noticed that they had released a 1.0.6 version.
UPDATE
Just to reiterate Luke Woodward below, you can use QGraphicsScene and QGraphicsView to render stuff in an object oriented way. The QGraphicsScene doesn't actually render anything it just a scene graph, the QGraphicsView is then used to render the contents of the scene graph. For low level drawing there´s also QPainter - there's a basic drawing tutorial here. It's also worth looking at QGraphicsItem which is the base for all graphics items and
includes defining the item's geometry, collision detection, its painting
implementation and item interaction through its event handlers
docs here. The Context2D provides an HTML canvas (if I'm not mistaken through the use of WebKit). The canvas itself only has a changed slot, but any objects you place on the canvas will/can have more slots. There's a fairly complete looking tutorial on Context2D and Context2DCanvas here. For an explanation as to why so many different ways of rendering stuff, you'll have to ask someone else. My two cents is that is has something to do with the fact that Qt is supposed to work everywhere and Trolltech and later Nokia wanted to provide lots of choice. Luckily the docs are really good.
Do you know if there is an easy way to drag-and-drop elements (icons or buttons) into a canvas and create different drawings on it as a result?
The idea is to have a set of objects and let the user drag them into a drawing space.
In the worst case the user could just click on the icon/button, and then click on the canvas and draw the element in the position, but I think the dragging is more intuitive.
There is an example of something more or less like this here: http://wiki.wxpython.org/wxOGL Although it is about wxOGL, they recommend you to use either SimpleCanvas or FloatCanvas and adapt their examples.
I assume you are building a browser app? If so, the jQuery UI Droppable plugin provides most of the standard functionality, and is pretty easy to modify to fit custom needs. Here's the link: http://jqueryui.com/demos/droppable/
According to http://www.pygtk.org/docs/pygtk/gtk-constants.html, there are five state types: STATE_NORMAL, STATE_INSENSITIVE, etc. I want to set the background color of a Table, HBox, VBox, whatever, and I've tried setting every possible color of every kind of state:
style = self.get_style()
for a in (style.base, style.fg, style.bg,
style.light, style.dark, style.mid,
style.text, style.base, style.text_aa):
for st in (gtk.STATE_NORMAL, gtk.STATE_INSENSITIVE,
gtk.STATE_PRELIGHT, gtk.STATE_SELECTED,
gtk.STATE_ACTIVE):
a[st] = gtk.gdk.Color(0, 34251, 0)
Nothing has any effect. The only one that has any effect is when I manually created EventBoxes and specifically used the existing gtk.STATE_NORMAL color to blend with other colors. All the ones created by gtk without my intervention were not affected, though.
What's the proper way to go about doing this? I wouldn't mind having to make a gtkrc file or whatever. Is this because hbox, vbox, etc., don't have a color, but are transparent? Who provides the general color of the application, then?
Mostly google-fu (no windows here), posting as an "answer" mostly for a slightly better formatting. See if experimenting with the full-blown gtkrc like the one from here brings the fruits. The location, based on this and this appears to vary - so unless someone has the deterministic approach of finding it, filemon could be a sensible approach.
Being mostly a user for these kinds of apps, I would prefer the settings from my gtkrc over the hardcoded by what the programmer thought would be "the best", anytime.
It's because VBox and Hbox don't have an associated Window.
click here to see other widgets without windows. I would create event boxes and add the HBox or VBox inside the event box.
You need to call self.set_style(style) after changing the style. I used your code as a template and adding that line makes the background of everything green.