Delete image in wxpython? - python

I know this sounds easy but how can i delete a image with wxpython?
I am using wx.StaticBitmap and wx.Image to create the image, but is there a way to delete an image?
Maybe theres something like this:
bmp1 = wx.Image(filename, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
self.bitmap1 = wx.StaticBitmap(self.sizer, -1, bmp1, (0, 0))
delete_image(self.bitmap1)
delete_image(bmp1)
I have tried replacing the variable but the image still shows.

self.bitmap1.Hide() or self.bitmap1.Destroy(). The first will just hide it whereas the second will actually destroy the widget. You'll probably want to call self.Layout() to make your window redraw and update the layout of your widgets.

Related

Put a bitmap on top of StaticBitmap in WxPython

I have an application that has a panel
on which static bitmaps (wx.StaticBitmap) are placed
in a GridBagSizer.
I want to place a bitmap on top of certain of
the static bitmaps, that will partly cover it and its neighbours.
I've been trying like this:
self.panel = wx.Panel(self, -1, style=wx.TRANSPARENT_WINDOW)
self.panel.Bind(wx.EVT_PAINT, self.place_bitmap)
.
.
.
def place_bitmap(self, *args):
dc = wx.ClientDC(self.panel) # or wx.PaintDC(self.panel)
for xy in self.coordinates:
dc.DrawBitmap(self.top_bitmap, xy[0], xy[1], 0)
self.panel.Layout()
If I change the coordinates in dc.DrawBitmap()
to a place in self.panel where there is no static bitmap, it shows.
But I can't get it to show on top of the static bitmaps.
So it seems it's getting rendered under them?
How can I make the top_bitmap go on top?
You can't draw on top of the child windows portably. The general solution is to create a child window of that child window and draw on it but in your case it probably would be simpler to just use a custom window instead of wxStaticBitmap which is a trivially simple class anyhow -- then you'd be able to draw whatever you want in it.

How to make icon background transparent in PyQt>?

I use a menu icon with white background. When I start my program I see this white background on gray menu item. It does not look nice. How to make white background transparent for not to see white color?
Try these approaches:
self.setStyleSheet("background-color: transparent;)
self.setFlags(Qt.WA_TranslucentBackground)
You have also the option of setting up a mask
def make_transparent(self):
self.setStyleSheet("background: transparent;")
mMask = QPixmap(self.width(), self.height())
mMask.fill(Qt.transparent)
p = QPainter()
p.begin(mMask)
p.setPen(Qt.red)
p.setBrush(QBrush(Qt.red))
p.drawRect(QRect(0, 0, 1000, 1000))
p.end()
self.setMask(mMask.mask())
Just adapt to size and color you want.
You should have a transparent image to begin with, where everything except the menu are empty pixels( png format).
Your pyqt application can't differentiate between what's background and foreground.
You can try and remove the image background online here
This answer doesn't help with the current question but it's for the people who will come in the future looking for a way to set a transparent Icon.
So I wanted to get rid of the default windows Icon and I didn't have any logo for the application either so I thought that making the Icon transparent would get rid of that windows icon.
pixmap = QtGui.QPixmap(255, 255)
pixmap.fill(QtGui.QColor(0,0,0,0))
MainWindow.setWindowIcon(QtGui.QIcon(pixmap))

How to hide all widgets in a QGridLayout() in python

I'm currently looking for a smart way to start a game made in PyCharm by using the PyQt library and I wanted to try and hide several widgets, or complete Layouts but the function .hide() doesn't work on complete layouts.
Is there a code to hide layouts completely or is hiding all the widget in that box the way to go?
If the answer is the latter, how can I make it easier for me than to go through every single widget and add the .hide() extension to them. Can I collect all those widgets in some sort of parent and then just parent.hide() as well as parent.show() ?
The code looks like this atm:
playerbox = QGridLayout()
playerbox.addWidget(self.player1, 0, 0)
playerbox.addWidget(self.player2, 0, 1)
playerbox.addWidget(self.stake, 1, 0)
playerbox.addWidget(self.money, 1, 1)
playerbox.addWidget(betButton, 2, 0)
playerbox.addWidget(foldButton, 2, 1)
playerbox.addWidget(bettingtext,3,0)
And the playerbox is supposed to be a box in a game that displays several items. I just want this box not to show anything before the game has started (this is done when the user press start but this has already been dealt with).
Thanks in advance!
I like this one:
[playerbox.itemAt(i).widget().hide() for i in
range(playerbox.rowCount()*playerbox.columnCount()) if
playerbox.itemAt(i)]
the if clause is required since you could have empty spots in your grid, and then itemAt() returns None.

Can I make a custom pygtk tooltip stay on when the cursor is over it?

Please look at the following snippet :
import gtk
def callback(widget, x, y, keyboard_mode, tooltip):
hbox = gtk.HBox(False, 8)
button = gtk.Button('Exit Tooltip')
label = gtk.Label('Tooltip text')
hbox.pack_start(label)
hbox.pack_start(button)
hbox.show_all()
tooltip.set_custom(hbox)
return True
label = gtk.Label('Test label')
label.set_has_tooltip(True)
label.connect('query-tooltip', callback)
Here I've created a custom tooltip with a close button in it. Now I want it to stay until i click that close button. Searching google was not that helpful. besides I would also like to know the signals/events that are emitted when a tooltip is being closed.
Similar problems are handled smoothly for JQuery/JavaScript/Ajax tooltips etc. but
for gtk/pygtk there is no luck :(
Thanks in advance ...
I had this issue as well, and as far as I know, there isn't any way to determine how long a tooltip stays up.
What I did (and recommend to you) is that you make your own "tooltip" and set it's background color to yellow, or whatever color you want, via an eventbox. Make sure you don't show it yet. This is just a simplified code, as you will need to position and size this in your project yourself.
color = gtk.gdk.rgb_get_colormap().alloc_color('black')
ebTooltip = gtk.EventBox()
btnTooltip = gtk.Button("Close")
ebTooltip.add(btnTooltip)
ebTooltip.modify_bg(gtk.STATE_NORMAL, color)
Now, you just need to hide and show this eventbox via your callbacks. To show it, call...
ebTooltip.show()
And, to hide it (probably on the "clicked" event of your close button)...
ebTooltip.hide()
Hope that solves your issue!

scrollbar for statictext in wxpython?

is possible to add a scrollbar to a statictext in wxpython?
the thing is that i'm creating this statictext:
self.staticText1 = wx.StaticText(id=wxID_FRAME1STATICTEXT1,label=u'some text here',name='staticText1', parent=self.panel1, pos=wx.Point(16, 96),
size=wx.Size(408, 216),style=wx.ST_NO_AUTORESIZE | wx.THICK_FRAME | wx.ALIGN_CENTRE | wx.SUNKEN_BORDER)
self.staticText1.SetBackgroundColour(wx.Colour(255, 255, 255))
self.staticText1.SetBackgroundStyle(wx.BG_STYLE_SYSTEM)
self.staticText1.SetFont(wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD, False,u'MS Shell Dlg 2'))
self.staticText1.SetAutoLayout(True)
self.staticText1.SetConstraints(LayoutAnchors(self.staticText1, False,True, True, False))
self.staticText1.SetHelpText(u'')
but later i use StaticText.SetLabel to change the label and the new text is too big to fit the window, so i need to add a scrollbar to the statictext..
i tried adding wx.VSCROLL to the style, and the scrollbar show up but cant scroll down to see the rest of the text..
wx.StaticText is designed to never respond to mouse events and never take user focus. Given that this is its role in life, it seems that a scrollbar would be inconsistent with its purpose.
There are two ways to get what you want: 1) You could use a regular TextCtrl with the style TE_READONLY (see here); or 2) you could make a scrolled window that contains your StaticText control.

Categories