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'))
Related
I want to have all my labels and buttons to have a white background.
I know I can use bg="white" in each one but I was thinking if there was a way I could change the default colour to white or make it so that all widgets have a white background with a single line of code. similar to how you can change the font by doing:
from tkinter import *
window=Tk()
window.option_add("*font",("Arial",15))
window.mainloop()
Which will set all the font to Arial 15
thanks to #acw1668 's comment, can do
window.option_add("*Label*Background", "white")
and
window.option_add("*Button*Background", "white")
to solve it. Just thought id put it as an answer in case people don't see the comment and what not.
Hi I'm trying to make a canvas to create images onto it, but when I create the canvas, it has this ugly white background.
test_canvas = Canvas(main_window,100,100, bd=0, highlightthickness=0)
And after placing it, it has a white background.
Is there anyway to remove that background and make it blend in (be transparent)?
Here's an image to see the problem (I want that white background to disappear and blend in with the rest of the background)
https://imgur.com/a/FySWyyn
A windows only solution is to use the pywin32 modul and 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 like in the exampel 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()
NOTE: AFTER DEFINING A TRANSPARENT COLORKEY EVERYTHING IN THAT CANVAS WITH THAT COLOR WILL BE TRANSPARENT!
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.
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))