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))
Related
How to set the active background color for the treeview heading. used the following code, background and foreground is working fine but active background is not working. please help me with this
ttk.Style().theme_use('default')
ttk.Style().configure("Treeview.Heading",font=("Calibri",12,'italic'),activebackground="#81C44C",background = "#0D94D2",foreground="White")
You need to use style.map to change the behavior:
pressed_color = 'black'
highlight_color='red'
ttk.Style().map("Treeview.Heading",
background = [('pressed', '!focus', pressed_color),
('active', highlight_color),
('disabled', '#ffffff')])
As a good source
I am making a chess program and I want to be able to drag the pieces. In order to do this, I put the image of the piece on a Canvas so it can be dragged (I can also use a Label if I want). However, when I drag the piece there is a white square that surrounds the image of the piece.
When I researched the problem, many people gave this solution:
drag_canvas = Canvas(self, height=80, width=80, bg="yellow")
root.wm_attributes("-transparentcolor", "yellow")
This caused the background to be transparent but it was not the chessboard that was visible, it was the program behind the GUI
.
Is there any way I can have the background be transparent and show the chessboard behind rather than the program behind the tkinter window?
Note: I do not mind using any other widget (e.g. a Label) but they must use modules that come default with Python (so no PIL) as this program needs to be used in an environment where I cannot download other modules.
Question: How to make a tkinter canvas background transparent?
The only possible config(... option, to set the background to nothing
c.config(bg='')
results with: _tkinter.TclError: unknown color name ""
To get this result:
you have to hold the chess board and figures within the same .Canvas(....
self.canvas = Canvas(self, width=500, height=200, bd=0, highlightthickness=0)
self.canvas.create_rectangle(245,50,345,150, fill='white')
self.image = tk.PhotoImage(file='chess.png')
self.image_id = self.canvas.create_image(50,50, image=self.image)
self.canvas.move(self.image_id, 245, 100)
Tested with Python: 3.5 - TkVersion: 8.6
A windows only solution is to use the pywin32 module that can be installed with:
pip install pywin32
With pywin32 you can alter the window exstyle and set the canvas to a layered window. A layered window can have a transparent colorkey and is done in the example below:
import tkinter as tk
import win32gui
import win32con
import win32api
root = tk.Tk()
root.configure(bg='yellow')
canvas = tk.Canvas(root,bg='#000000')#full black
hwnd = canvas.winfo_id()
colorkey = win32api.RGB(0,0,0) #full black in COLORREF structure
wnd_exstyle = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
new_exstyle = wnd_exstyle | win32con.WS_EX_LAYERED
win32gui.SetWindowLong(hwnd,win32con.GWL_EXSTYLE,new_exstyle)
win32gui.SetLayeredWindowAttributes(hwnd,colorkey,255,win32con.LWA_COLORKEY)
canvas.create_rectangle(50,50,100,100,fill='blue')
canvas.pack()
Explaination:
First we need the handle of the window which is called hwnd and we can get it in tkinter by .winfo_id().
Next we get the actual extended window style by GetWindowLong and ask specific for extended style information with win32con.GWL_EXSTYLE.
After that we do a bitwise operation in hexadezimal to alter the style with wnd_exstyle | win32con.WS_EX_LAYERED the result is our new_style.
Now we can set the extended style to the window with SetWindowLong. Finally we have our LayeredWindow which has additional Attributes we can work with. A transparent ColorKey can be set with SetLayeredWindowAttributes while we just use LWA_COLORKEY the alpha parameter has no use to us.
Important note: After defining a transparent colorkey, everything in that canvas with that color will be transparent.
def generate_thumb(self):
# -- clear preview image
pixmap = QPixmap(self.nwidth, self.nheight)
pixmap.fill( QColor('#C0C0C0'))
ico = QIcon(pixmap)
self.btn3.setIcon(ico)
self.btn3.show() # still not showing
self.generate_thumbnail()
I want btn3 filled with a grey background visible to the user - and then to start the thumbnail generation (takes 5-10 seconds then the btn3 icon is filled again with the generated thumbnail). When I run the code I don't see the grey background at all, only the new rendered image after the generation is done.
How can I force the gui to refresh or show the grey btn3 icon and then start to generate the the new image?
I just finally found the answer myself.
calling QtGui.qApp.processEvents() worked for me
def generate_thumb(self):
# -- clear preview image
pixmap = QPixmap(self.nwidth, self.nheight)
pixmap.fill( QColor('#C0C0C0'))
ico = QIcon(pixmap)
self.btn3.setIcon(ico)
# show it now!
QtGui.qApp.processEvents()
self.generate_thumbnail()
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.
I am using this example
http://www.pygtk.org/pygtk2tutorial/examples/testtext.py
but it doesn't work
i see only white textarea, terminal doesn't dispaly any notices
Ubuntu 11.04
You apply colors to textview, not textbuffer
tview = gtk.TextView()
# background
tview.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse('#000000'))
# foreground
tview.modify_text(gtk.STATE_NORMAL, gtk.gdk.color_parse('#FFFFFF'))
# bonus font
tview.modify_font(pango.FontDescription('Monospace 11'))