Font differences in tkinter & matplotlib - python

I am using Tkinter for a GUI in which is also show Matplotlib graphs. I am trying to align the way how text looks between Tkinter (e.g. Label) and Matplotlib (e.g. graph title, axis labels). To try it out I have set both to Arial with the following commands:
import matplotlib
import ttkbootstrap as ttk
matplotlib.rcParams.update({"font.family": "Arial"}) # Matplotlib
defaultFont = ttk.font.nametofont('TkDefaultFont') # Tkinter (more specifically Ttk Bootstrap)
defaultFont.configure(family = 'Arial')
However, it seems like the font is somewhat 'bolder' for Tkinter (although its not set to bold or anything. You can see that in screenshot below, where upper text is a Tkinter Label and below is the title of a Matplotlib graph. Any idea why I am seeing this difference? I would really want to 1:1 use the same font face everywhere.

Related

Full transparency in Tk window icon

Searching for the best way to clear the Tkinter icon, I found Removing the TK icon on a Tkinter window. But all the answers given there seemed unsatisfactory to me: I wished to do it in a platform-independent way, without additional files and without (compressed) inline data.
However, the solution I found – and that's why I didn't post this a an answer – has a small flaw that I would like to understand.
import tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()
photo = ImageTk.PhotoImage(Image.new('RGBA', (1,1), (0,0,0,1)))
root.iconphoto(False, photo)
root.mainloop()
The alpha value I'm using here is 1 instead of 0, so there is no full transparency even if probably no one will be able to see the difference. If I use 0 to get full transparency as documented, a small black square (larger than 1x1) is shown instead.
I wasn't able to find any information about what causes this strange (but maybe acceptable?) behavior. And so I would be happy if any of you could give me an explanation.

Tkinter default font is tiny

I am using some libraries that are using tkinter, and the font is so tiny I can't read it (a few pixels only). Here is a minimum working example:
from tkinter import *
root = Tk()
myLabel = Label(root, text = 'Hello, world!')
myLabel.pack()
root.mainloop()
The working example produces the tiny hello world in the window to the right: mwe tiny font
The rest of the image is showing the gitk gui as a size reference.
Is there a way to set the default font size in tkinter without changing the code? It could also be a problem with dpi settings?
I am on Arch linux, i3wm, using the nvidia proprietary driver
Add this line to the beginning of your code. change helevicta by the font you want to use, change 20 by the font size you want to use and change bold by any formatting options you want (you don't have to include any, and if you do, you can add more than one.) This line will set the font for any labels in the root window.
root.option_add("*Label.Font", "helvetica 20 bold")
I found a workaround to my problem.
Tkinter has an automatic way to determine the monitor size and it will set a tk scale variable accordingly. In my case the scaling value does the opposite of what it is supposed to do - larger scaling value leads to smaller font size. I still don't know why that is the case.
I ran strace on my minimum working example and found out that tkinter tries to read the file ~/.Tk.tcl and I can therefore use this file as a configuration file.
Putting tk scaling 1.0 into ~/.Tk.tcl solved the problem for me. You can experiment with the scaling value to increase/decrease the overall scaling of the gui components.
Looking at strace it also tries to read ~/.Tk.py, ~/.example.tcl and ~/.example.py.
Yes you can set default font for tkinter widgets. Refer to this thread
https://stackoverflow.com/questions/15462647/modify-the-default-font-in-python-tkinter#:~:text=2%20Answers&text=Tkinter%20has%20several%20built%2Din,them%20will%20change%20as%20well.

Showing Matplotlib graphs in a canvas tkinter python 2.7

i want to display a graph in a canvas. The GUI was written in tkinter using python 2.7.
self.plotting(x, pa, y)
savefig("Omegakillerisawesome.png")
im=PhotoImage(file="Omegakillerisawesome.png")
self.canvasForGraph.create_image(500, 350, anchor=CENTER, image=im)
#show()
os.remove('Omegakillerisawesome.png')
The plotting function was implemented by me. accepting x,y values. so i'm hoping to show the graph in a canvas. so i'm saving the graph in the directory and reloading the image to the canvas. The problem is the .png file is not appearing in the canvas.
but if i uncomment the show() function the image comes up in the canvas as well as the usual window that graphs are displayed. i don't want that. i only want the graph to show up in the canvas.
please help. i'm a newbie.
[edit]
depth=[1,2,3,4,5,6]
temp=[9,8,7,6,5,4]
plot(depth,temp)
show()
this shows me a frame with my graph in it. but i want to do is display the graph inside a canvas widget using tkinter. but i dont want the usual frame that comes with the latter code. i just want the graph plotted inside the canvas no more.
that is what i'm trying to achieve from the first piece of code.
thanks.

Module to make a program in python

I've been wondering if there were other types of window-oriented modules than tkinter (too simple in my sense) or pygame (too much game-oriented). For example, if it were possible to display things with coordinates and not Up/Down like tkinter it would be better I guess.
Asking for suggestions is not a good fit for Stack Overflow but your other question about grid layouts is a good question!
Grid Layout with tkinter
Tkinter, like other desktop window managers have things called layout managers. Layout managers provide a format for displaying/ organising components.
Using this reference i have prepared a short summary of the tkinter grid layout
from Tkinter import *
colours = ['red','green','orange','white','yellow','blue']
r = 0
#generate a set of components and place them in specific cells with the .grid() call
for c in colours:
Label(text=c, relief=RIDGE,width=15).grid(row=r,column=0)
Entry(bg=c, relief=SUNKEN,width=10).grid(row=r,column=1)
r = r + 1
#run application
mainloop()
Produces:

Create a gray scale colormap with colorbar on Python

I have a matrix with 580x580 elements and I want to plot it like a gray colormap (with colorbar indicating the values and axes on the picture indicating pixel values) in a Canvas of a GUI I'm making with TKinter. I know I can do that using the function imshow(myMatrix, cmap='gray'), but that requires me to import the pylab library. Is there another way of creating the colormap or, if not, is it possible to show the output of the imshow function on my GUI panel instead of on a new window?
You should be able to display charts from matplotlib within your tkinter application by setting matplotlib to use the appropriate backend.
import matlplotlib
matplotlib.use('TkAgg')
There are two pretty decent examples of how to do so here and here.

Categories

Resources