Transparent background with matplotlib and tkinter - python-3.x

I'm programming a data-analysis programm for whatsapp chats and I have a matplotlib graph in my tkinter window. I know how to customize colors, but how do I set the background to transparent?
f = Figure(figsize=(4.1, 4.1), dpi=100)
f.set_facecolor('xkcd:gray') # this should be transparent
a = f.add_subplot(111)
a.plot(dates, mes_count)
a.xaxis.set_major_locator(MaxNLocator(prune='both',nbins=6))
a.set_facecolor('xkcd:gray')
canvas = FigureCanvasTkAgg(f, mainloop.root)
canvas.get_tk_widget().place(x=190, y=80)

I got a bit frustrated by trying to match the background color of the axes/figure with the background color of the tkinter app on each platform so I did a little digging:
In general, two things need to be done:
Tell matplotlib to use a transparent background for the figure and axes object by setting the facecolor to "none":
f.set_facecolor("none")
a.set_facecolor("none")
Tell the backend renderer to also make the background transparent, e.g. for saving it as png:
f.savefig('mygraph.png', transparent=True)
The problem is: How do I tell this to FigureCanvasTkAgg? Looking at the sourcecode of FigureCanvasTk, that does the drawing, one can see that the tk.Canvas always gets created with a white background. AFAIK a Canvas can't be transparent (please correct me), but when created without a specific background color, it will have the same background color as the Frame surrounding it, given by the ttk.Style of it. So there are two workarounds I can think of:
After creating the FigureCanvasTkAgg, set the background color of the canvas to the same as the current style:
s = ttk.Style()
bg = s.lookup("TFrame", "background")
bg_16bit = self.winfo_rgb(bg)
bg_string = "#" + "".join([hex(bg_color >> 8)[2:] for bg_color in bg_16bit])
canvas.get_tk_widget().config(bg=bg_string)
Lines 3 & 4 are necessary on e.g. Windows, since s.lookup("TFrame", "background") can return a system color name here, that then needs to be converted to a standard #aabbcc hex color
Making your own MyFigureCanvasTk that's just a copy of the code in matplotlib, that skips setting the background color of the Canvas:
class MyFigureCanvasTk(FigureCanvasTk):
# Redefine __init__ to get rid of the background="white" in the tk.Canvas
def __init__(self, figure=None, master=None):
super().__init__(figure)
self._idle_draw_id = None
self._event_loop_id = None
w, h = self.get_width_height(physical=True)
self._tkcanvas = tk.Canvas(
master=master,
#background="white"
width=w,
height=h,
borderwidth=0,
highlightthickness=0,
)
self._tkphoto = tk.PhotoImage(master=self._tkcanvas, width=w, height=h)
self._tkcanvas.create_image(w // 2, h // 2, image=self._tkphoto)
self._tkcanvas.bind("<Configure>", self.resize)
if sys.platform == "win32":
self._tkcanvas.bind("<Map>", self._update_device_pixel_ratio)
self._tkcanvas.bind("<Key>", self.key_press)
self._tkcanvas.bind("<Motion>", self.motion_notify_event)
self._tkcanvas.bind("<Enter>", self.enter_notify_event)
self._tkcanvas.bind("<Leave>", self.leave_notify_event)
self._tkcanvas.bind("<KeyRelease>", self.key_release)
for name in ["<Button-1>", "<Button-2>", "<Button-3>"]:
self._tkcanvas.bind(name, self.button_press_event)
for name in ["<Double-Button-1>", "<Double-Button-2>", "<Double-Button-3>"]:
self._tkcanvas.bind(name, self.button_dblclick_event)
for name in ["<ButtonRelease-1>", "<ButtonRelease-2>", "<ButtonRelease-3>"]:
self._tkcanvas.bind(name, self.button_release_event)
# Mouse wheel on Linux generates button 4/5 events
for name in "<Button-4>", "<Button-5>":
self._tkcanvas.bind(name, self.scroll_event)
# Mouse wheel for windows goes to the window with the focus.
# Since the canvas won't usually have the focus, bind the
# event to the window containing the canvas instead.
# See https://wiki.tcl-lang.org/3893 (mousewheel) for details
root = self._tkcanvas.winfo_toplevel()
root.bind("<MouseWheel>", self.scroll_event_windows, "+")
class MyFigureCanvasTkAgg(FigureCanvasAgg, MyFigureCanvasTk):
def draw(self):
super().draw()
self.blit()
def blit(self, bbox=None):
_backend_tk.blit(
self._tkphoto, self.renderer.buffer_rgba(), (0, 1, 2, 3), bbox=bbox
)
This let's the graph blend in with whatever background color might be surrounding it. It should work on all platforms (tested only on Windows and Linux, with python 3.11, tkinter 8.6.12). Maybe this helps someone stumbling over this question.

Related

Embedding Images and matplotlib Plots together on on tk canvas

A few years ago I wrote a script in python2 that would pull images and generate plots and show them together in one gui, following this old documentation. However, I'm trying to update this to python3, and I've run into an issue with the way that plots are put on tk windows in the latest version of matplotlib. There are updated guides for how to embed matplotlib graphs in tk windows, of course, but these don't seem to be exactly what I want, since I want to plot various images and graphs together.
Currently:
# Start by creating the GUI as root
root=Tk()
root.wm_title("JADESView")
# Create the canvas
canvas=Canvas(root, height=canvasheight, width=canvaswidth)
# Plot the EAZY SED
#image = Image.open(EAZY_files+str(current_id)+"_EAZY_SED.png")
image = getEAZYimage(current_id)
# Crop out the thumbnails
image = cropEAZY(image)
photo = resizeimage(image)
item4 = canvas.create_image(eazy_positionx, eazy_positiony, image=photo)
Label(root, text="EAZY FIT", font=('helvetica', int(textsizevalue*1.5))).place(x=eazytext_positionx, y = eazytext_positiony)
# Plot the BEAGLE SED
#new_image = Image.open(BEAGLE_files+str(current_id)+"_BEAGLE_SED.png")
new_image = getBEAGLEimage(current_id)
new_photo = resizeimage(new_image)
item5 = canvas.create_image(beagle_positionx, beagle_positiony, image=new_photo)
Label(root, text="BEAGLE FIT", font=('helvetica', int(textsizevalue*1.5))).place(x=beagletext_positionx, y = beagletext_positiony)
canvas.pack(side = TOP, expand=True, fill=BOTH)
# Plot the thumbnails
fig_photo_objects = np.empty(0, dtype = 'object')
fig_photo_objects = create_thumbnails(canvas, fig_photo_objects, current_id, current_index, defaultstretch)
I create a tk.canvas object, and then use canvas.create_image to place two PIL photo objects, with labels (the "EAZY SED" and BEAGLE SED"), and this still works with python3. However, the create_thumbnails function I've written creates individual figure objects and then calls a function "draw_figure" to embed them, which I'll post below, but it's from the tk example I linked above:
def draw_figure(canvas, figure, loc=(0, 0)):
""" Draw a matplotlib figure onto a Tk canvas
loc: location of top-left corner of figure on canvas in pixels.
Inspired by matplotlib source: lib/matplotlib/backends/backend_tkagg.py
"""
figure_canvas_agg = FigureCanvasAgg(figure)
figure_canvas_agg.draw()
figure_x, figure_y, figure_w, figure_h = figure.bbox.bounds
figure_w, figure_h = int(figure_w), int(figure_h)
photo = PhotoImage(master=canvas, width=figure_w, height=figure_h)
# Position: convert from top-left anchor to center anchor
canvas.create_image(loc[0] + figure_w/2, loc[1] + figure_h/2, image=photo)
# Unfortunately, there's no accessor for the pointer to the native renderer
tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2)
# Return a handle which contains a reference to the photo object
# which must be kept live or else the picture disappears
return photo
And this breaks in all sorts of ways.
I'm open to overhauling how everything is done, but I don't see examples that show how this sort of thing might work. The examples for the latest version of matplotlib all explain that you should use FigureCanvasTkAgg, but I don't exactly know how to use this if I already have an existing canvas with photos embedded.
Any help would be appreciated, and I can also explain more if necessary!

Making parts of canvas transparent while still detecting and blocking mouse clicks in transparent areas in tkinter?

I'm trying to make a program where the user can paint on the screen. So I want to make an invisible canvas window in fullscreen where only the user's pen marks on the canvas will be visible. The closest thing I found is this function: root.attributes("-transparentcolor","color code here"), which will make all the parts of the window that's in the color you give transparent. So if I give the second parameter the background color of the canvas, then only the pen strokes on the canvas will be visible. This is so close to what I want, except for one thing, the transparent areas can't detect or block mouse clicks! Any mouse clicks will just go through to whatever is behind the tkinter window. Is there a way to make it so the transparent areas will still block mouse clicks? I really need help on this!
Here is a much better way to do this using only tkinter. Explanation is in code comments. Basically uses two windows, one for "blocking" the mouse and being transparent using the "-alpha" attribute and the other window for "hosting" canvas and having one completely transparent color while keeping others opaque using "-transparentcolor" attribute. That also means that this is cross-platform solution too (except I think the -transparentcolor attribute differs a little bit on other OS like Linux where I think it is -splash or sth and maybe something different on MacOS):
from tkinter import Tk, Toplevel, Canvas
# setting the starting coordinate of the line so that
# on motion it is possible to immediately draw it
def set_first(event):
points.extend([event.x, event.y])
# on motion append new coordinates to the list and if there are
# 4 (the minimum), create a new line and save the id
# otherwise update the existing line
def append_and_draw(event):
global line
points.extend([event.x, event.y])
if len(points) == 4:
line = canvas.create_line(points, **line_options)
else:
canvas.coords(line, points)
# when released clear the list to not waste space
# and not necessarily but also set "id" to None
def clear_list(event=None):
global line
points.clear()
line = None
line = None # this is a reference to the current line (id)
points = [] # list to keep track of current line coordinates
line_options = {} # dictionary to allow easier change of line options
# just a variable to more easily store the transparent color
transparent_color = 'grey15'
# creating the root window which will help with drawing the line
# because it will "block" mouse because `-alpha` (0.01 seems to be the lowest value)
# attribute is used, however it makes everything transparent on the window
# so need another window to "host" the canvas
root = Tk()
root.attributes('-alpha', 0.01)
root.attributes('-topmost', True)
root.attributes('-fullscreen', True)
# just press Esc key to close the whole thing, otherwise
# it is only doable by pressing Alt + F4 or turning off
# the computer
root.bind('<Escape>', lambda e: root.quit())
# create the host window, because it allows to have only
# one transparent color while keeping the other opaque and
# visible
top = Toplevel(root)
top.attributes('-transparentcolor', transparent_color)
top.attributes('-topmost', True)
top.attributes('-fullscreen', True)
# set the focus to root because that is where events are bound
root.focus_set()
# create the canvas to draw on
canvas = Canvas(top, bg=transparent_color, highlightthickness=0)
canvas.pack(fill='both', expand=True)
# bind all the events to `root` which "blocks" mouse
# but is also almost (because it has a very small alpha value
# it is not entirely invisible but human eye won't notice much)
# invisible
root.bind('<Button-1>', set_first)
root.bind('<B1-Motion>', append_and_draw)
root.bind('<ButtonRelease-1>', clear_list)
root.mainloop()
Here is an improvable example (you may need to pip install pyautogui, ctypes is a built-in library), it is also Windows only as far as I know:
Note: The other answer using two windows, however, is a lot better but I will keep this too just for the information.
from tkinter import Tk, Canvas
import pyautogui as pag
import ctypes
data = {
'draw': True,
'cur_line_points': [],
'cur_line_id': None
}
# function taken mainly from here: https://stackoverflow.com/a/46596592/14531062
def is_pressed(btn: str = 'left') -> bool:
if btn == 'left':
btn = 0x01
elif btn == 'right':
btn = 0x02
else:
raise Warning("incorrect argument, should be 'left' or 'right'")
return ctypes.windll.user32.GetKeyState(btn) not in (0, 1)
def draw_line(canvas_):
if not data['draw']:
root.after(10, draw_line, canvas_)
return
pressed = is_pressed('left')
cur_line_points = data['cur_line_points']
cur_line_id = data['cur_line_id']
if not pressed:
if cur_line_id is not None:
canvas_.coords(cur_line_id, cur_line_points)
data['cur_line_id'] = None
cur_line_points.clear()
else:
mouse_x, mouse_y = pag.position()
cur_line_points.extend((mouse_x, mouse_y))
len_points = len(cur_line_points)
if len_points == 4:
data['cur_line_id'] = canvas_.create_line(cur_line_points)
elif len_points > 4:
canvas_.coords(cur_line_id, cur_line_points)
root.after(10, draw_line, canvas_)
transparent_color = 'grey15'
root = Tk()
root.config(bg=transparent_color)
root.attributes('-transparentcolor', transparent_color)
root.attributes('-topmost', True)
root.attributes('-fullscreen', True)
canvas = Canvas(root, bg=transparent_color, highlightthickness=0)
canvas.pack(fill='both', expand=True)
draw_line(canvas)
root.mainloop()
Basically detects if mouse button is pressed using the built-in library ctypes and if it is adds the current mouse coordinates (does that using pyautogui library which may need be installed) to a list and then draws a line based on that list (it also keeps the reference of the currently drawn line and simply changes its coordinates instead of drawing a new line each time it loops), the only slight issue is that while drawing the mouse is also interacting with the window below, highlighting text and stuff, couldn't really figure out how to remove that yet but at least you get to draw a line.

Is there a way to set a wrap length on a Tkinter Text Widget?

Question
I am trying to make a text editor in Tkinter. If a large single-line file is opened, it lags massively and stops responding. Is there a way to set a wrap length for a text widget? I have scroll bars and I don't want to have the characters wrap at the end of the text box. I want it to wrap after a certain number of characters.
Is this possible?
If so, how can I do it?
I am using Python 3.9.6 on 64-bit Windows 10.
What I have tried
I have tried using wraplength= in the function, but that doesn't work. I have also searched for this question and found nothing.
Code
from tkinter import *
root = Tk()
root.title('Notpad [new file]')
root.geometry('1225x720')
txt = Text(root,width=150,height=40,wrap=NONE)
txt.place(x=0,y=0)
#Buttons here
scr = Scrollbar(root)
scr.pack(side='right',fill='y',expand=False)
txt.config(yscrollcommand=scr.set)
scr.config(command=txt.yview)
scr1 = Scrollbar(root,orient='horizontal')
scr1.pack(side='bottom',fill='x',expand=False)
txt.config(xscrollcommand=scr1.set)
scr1.config(command=txt.xview)
root.mainloop()
There is no wraplength option in tkinter Text widget. However you can simulate the effect using rmargin option of tag_configure().
Below is an example with a custom text widget using rmargin option:
import tkinter as tk
from tkinter import font
class MyText(tk.Text):
def __init__(self, master=None, **kw):
self.wraplength = kw.pop('wraplength', 80)
# create an instance variable of type font.Font
# it is required because Font.measure() is used later
self.font = font.Font(master, font=kw.pop('font', ('Consolas',12)))
super().__init__(master, font=self.font, **kw)
self.update_rmargin() # keep monitor and update "rmargin"
def update_rmargin(self):
# determine width of a character of current font
char_w = self.font.measure('W')
# calculate the "rmargin" in pixel
rmargin = self.winfo_width() - char_w * self.wraplength
# set up a tag with the "rmargin" option set to above value
self.tag_config('rmargin', rmargin=rmargin, rmargincolor='#eeeeee')
# apply the tag to all the content
self.tag_add('rmargin', '1.0', 'end')
# keep updating the "rmargin"
self.after(10, self.update_rmargin)
root = tk.Tk()
textbox = MyText(root, width=100, font=('Consolas',12), wrap='word', wraplength=90)
textbox.pack(fill='both', expand=1)
# load current file
with open(__file__) as f:
textbox.insert('end', f.read())
root.mainloop()
Note that I have used after() so that even there are changes on the content, the rmargin option is applied to updated content.
Note also that it may not be an efficient way, but it shows a possible way.

How to add background image to an tkinter application?

I recently made a Python watch application. But it looks very boring because of the one-color background and just nothing except of the time and the date. So I wanted to add background to the application. And I ran into a problem: If I just pack the image into a label, the other labels won't be transparent and will have their own background. I tried to use tkinter.Canvas, but it doesn't work with labels. Any suggestions?
That's the code for the first version of the app:
from tkinter import *
from time import sleep
from random import choice
import datetime
root=Tk()
BGCOLOR='#000000'
FGCOLOR='#FFFFFF'
root.overrideredirect(True)
w = root.winfo_screenwidth()
h = root.winfo_screenheight()
root.geometry(str(w)+'x'+str(h)+'+0+0')
root.configure(bg=BGCOLOR)
date=str(datetime.datetime.now()).split()[0].split('-')
tl=Label(root,text=str(datetime.datetime.now()).split()[1].split('.')[0],font=['URW Gothic L',300], fg=FGCOLOR, bg=BGCOLOR)
dl=Label(root,text=date[2]+'.'+date[1]+'.'+date[0],font=['URW Gothic L',100],bg=BGCOLOR,fg=FGCOLOR)
tl.pack(expand=True, fill=BOTH)
dl.pack(expand=True,fill=BOTH)
Button(master=root,text='X',bg=BGCOLOR,fg=FGCOLOR,command=root.destroy).pack(side=RIGHT)
while True:
tl.config(text=str(datetime.datetime.now()).split()[1].split('.')[0])
if date!=str(datetime.datetime.now()).split()[0].split('-'):
date=str(datetime.datetime.now()).split()[0].split('-')
dl.config(text=date[2]+'.'+date[1]+'.'+date[0])
dl.update()
tl.update()
sleep(1)
Ok, so the solution itself is, to create a Canvas object and use create_text() function with "fill" and "font" arguments. The best thing about this is that you can change the position of the text

Button color not working , tkinter Python3 [duplicate]

I've been working through the Tkinter chapters in Programming Python and encountered a problem where the foreground and background colours of a button will not change. I am working on a Mac OS X 10.6 system with Python 2.6.1. The colours of a label will change, but not the colours of a button. For example:
from Tkinter import *
Label(None, text='label', fg='green', bg='black').pack()
Button(None, text='button', fg='green', bg='black').pack()
mainloop()
On my Mac system the colours of the label change, but the colours of the button do not. On a Windows system with Python 2.6.1 the colours of both the label and button change.
Anyone know what is going wrong?
I've checked Interface Builder and it appears that there is no option to change the foreground or background colour of a button in that tool. There is the ability to edit the foreground and background colours of a label.
The Mac OS X rendering system (Quartz?) may just not support (easily) changing the fg and bg of a button.
There is a solution for changing the background of buttons on Mac.
Use:
highlightbackground=color
For example:
submit = Button(root, text="Generate", highlightbackground='#3E4149')
This results in the following, a nice button that fits in with the background:
I think the answer is that the buttons on the mac simply don't support changing the background and foreground colors. As you've seen, this isn't unique to Tk.
You can do it with tkmacosx library from PyPI.
Installation:
For Python 2, use pip install tkmacosx.
For Python 3, use pip3 install tkmacosx.
This is how you use tkmacosx:
from tkinter import *
from tkmacosx import Button
root = Tk()
B1 = Button(root, text='Mac OSX', bg='black',fg='green', borderless=1)
B1.pack()
root.mainloop()
It works fine on Mac OS X.
For anyone else who happens upon this question as I did, the solution is to use the ttk module, which is available by default on OS X 10.7. Unfortunately, setting the background color still doesn't work out of the box, but text color does.
It requires a small change to the code:
Original:
from Tkinter import *
Label(None, text='label', fg='green', bg='black').pack()
Button(None, text='button', fg='green', bg='black').pack()
mainloop()
With ttk:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
# background="..." doesn't work...
ttk.Style().configure('green/black.TLabel', foreground='green', background='black')
ttk.Style().configure('green/black.TButton', foreground='green', background='black')
label = ttk.Label(root, text='I am a ttk.Label with text!', style='green/black.TLabel')
label.pack()
button = ttk.Button(root, text='Click Me!', style='green/black.TButton')
button.pack()
root.mainloop()
Its quite annoying that after years this is still a problem.
Anyways, as others have mentioned, highlightbackground (the border color) can be used in place of background on a Mac. If you increase the size of the border to be huge (the size of the button or greater), you will get a nice, solid background color. This will give your button the appearance of a label.
This works if you are using place, but not if you are using something like grid. With grid, increasing the border size increases the button size automatically, unfortunately.
However, if you must use grid, you can always hack it....create your colorless grid button. Next use place to parent a background color button on top of it. This will be the button with the 'command' on it or the button you bind events to.
If you want your code to be OS independent, you can either add an 'if OS == "Mac"' statement or even add a custom function that modifies the button if its on a Mac but leaves it alone on Windows or Linux. Here's the former:
from tkinter import *
import platform
if platform.system() == "Darwin": ### if its a Mac
B = Button(text="Refersh All Windows", highlightbackground="Yellow", fg="Black", highlightthickness=30)
else: ### if its Windows or Linux
B = Button(text="Refresh All Windows", bg="Yellow", fg="Black")
B.place(x=5, y=10, width=140, height=30)
mainloop()
This worked for me:
self.gnuplot_bt = Button(
self.run_but_container, text="Plot with Gnuplot", font="Helvetica", command=self.gnuplot,
highlightbackground ="#8EF0F7", pady=2, relief=FLAT
)
I was looking as to why this doesn't work as well. I found a quick way to try and fix it is to have a label and then bind a click with the label. Then have the label change colors for a short time to mimic clicking. Here is an example.
def buttonPress(*args):
searchB.config(state = "active")
searchB.update()
time.sleep(0.2)
searchB.config(state = "normal")
## Whatever command you want
searchB = Label(main, text = "Search", bg = "#fecc14", fg = "Black", activebackground = "Red", highlightbackground="Black")
searchB.bind("<Button-1>", startSearch)
searchB.pack()
Confirm following code can change the background of tkinter Button on Mac OS X.
self.btn_open = tk.Button(self.toolbar,
text = "Open",
command=self.open,
highlightbackground = "gray")
But it cannot change bg of ttk.Button.
Not sure if anyone is still viewing this thread, but I have created a simple solution to this problem by creating my own Button class. It is available on GitHub.
import tkinter as tk
class Button():
button_frame = None
root = None
width=100
height=20
text=""
bg="white"
fg="black"
font="f 12"
bordercolor = "black"
bordersize = 3
label = None
command = None
def __init__(self,root,width=100,height=20,text="",bg="white",fg="black",font="f 12",command=None,bordercolor="black",bordersize=0):
self.root = root
self.width=width
self.height=height
self.text=text
self.bg=bg
self.fg=fg
self.font=font
self.command = command
self.bordercolor = bordercolor
self.bordersize = bordersize
self.button_frame = tk.Frame(root,width=width,height=height,bg=bg)
self.label = tk.Label(self.button_frame,text=self.text,bg=self.bg,width=self.width,height=self.height,fg=self.fg,font=self.font,highlightbackground=self.bordercolor,highlightthickness=self.bordersize)
self.label.place(anchor="center",relx=0.5,rely=0.5,relheight=1,relwidth=1)
self.label.bind("<Button-1>",self.call_command)
def call_command(self,event):
if (self.command != None):
self.command()
def place(self,anchor="nw",relx=0,rely=0):
self.button_frame.place(anchor=anchor,relx=relx,rely=rely)
def configure(self,width=width,height=height,text=text,bg=bg,fg=fg,font=font,command=command,bordercolor=bordercolor,bordersize=bordersize):
self.button_frame.configure(width=width,height=height,bg=bg)
self.label.configure(text=text,bg=bg,width=width,height=height,fg=fg,font=font,highlightbackground=bordercolor,highlightthickness=bordersize)
self.command =
Button and Label seem pretty similar to me, so I find it odd that the Label and Button work differently... even after all these years.
You can always make your own Button class which is wrapped around a Label with a border (default width is 2) and a bind call for the Button Release. You'd miss out on some of the "animation" of button press and release, but you'd get your background and foreground colors as desired.
I wrote a project called Tagged Text Widgets ('ttwidgets' on PyPI.org) which essentially does just that. I wrote the project to allow multi-font, multi-color Buttons and Labels. Essentially the project creates a compound Button or Label consisting of multiple underlying Label widgets (each with its own color/font) but acting like a single object. Those different colors and fonts are created by passing in HTML-like tagged text in lieu of regular text. And because of the underlying Labels rather than Buttons, it works around the issue on macOS.
I just tested it on macOS Sierra, and it works around the Button bg/fg color problem.
You can use it as follows:
from ttwidgets import TTButton
A TTButton supports the full interface of a Tkinter Button but with many enhancements. But for someone trying to work around the macOS color issue, just using a TTButton in lieu of a Tkinter Button suffices.

Resources