Bold text on tkinter canvas - python-3.x

How? I've tried several variations of
self.textindex = self.canvastext.create_text(0,696,font=('freemono bold',9), anchor='nw', fill='black', text='Longitude: ' + str(px))
or
self.textindex = self.canvastext.create_text(0,696,font=('bold', 'freemono bold',9), anchor='nw', fill='black', text='Longitude: ' + str(px))
etc, to either have the font type revert back to the default font or to give me errors.
I actually want some type of font that is block style font, every character has the same width, so I can setup up nice column style formatting on parts of the screen. I don't see on any programs I run, Times Roman(I think that is the right name) pop up so I'm guessing Linux Mint doesn't come standard with it:)..hence using freemono. I would stick with the default font, which is already bold, trying to format it on the screen is a lot more difficult though and I'm in kinda for the looks given how nicely this program is turning out right now.

The best way is to create a font object. For example:
# python2
import Tkinter as tk
import tkFont as tkfont
# python3
# import tkinter as tk
# from tkinter import font as tkfont
root = tk.Tk()
canvas = tk.Canvas(root, width=400, height=400, background="bisque")
canvas.pack(fill="both", expand=True)
normal_font = tkfont.Font(family="Helvetica", size=12)
bold_font = tkfont.Font(family="Helvetica", size=12, weight="bold")
canvas.create_text(50,50, text="This is normal", font=normal_font)
canvas.create_text(50,100, text="This is bold", font=bold_font)
root.mainloop()

this worked for me (Python 3):
canvas.create_text(x, y, font=('freemono', 11, 'bold'), anchor='sw', text=s)
And in addition to Bryan's answer: if you're using tkinter (as opposed to Tkinter) your code is:
from tkinter import font
...
myfont = font.Font(family='freemono', size=11, weight="bold")
canvas.create_text(x, y, font=myfont, anchor='sw', text=s)

instead of changing the weight of the letters, you could change the font of the letters by saying (font="Ariel") but with the font that is thick enough for you. Hope this helped :)

Related

Background Button Color is not working properly MACOS

I am trying to make a simple GUI for a school project. I am new to using tkinter. I used to create my GUIs using pygame, which is great for custumization, but not really for efficiency haha.
I am creating a button for each file in my /graphs folder. In this code, line 42, it seems to me I can't change the background color of the button. This is a reccurent problemI have with tkinter. I have no idea if it is something I am doing wrong or if there is a problem with the framework I am using.
I am trying to make the bg color red.
I am using MACOS, and i know there are complications with tkinter and mac, but i can't use tkmacosx because i need this project to be runnable on windows and linux as well.
Thank you for your help, don't hesitate if you have any suggestions on common practices with tkinter that i am not applying or if you have the solution to my problem !
Here is the output and the code
Output
import tkinter as tk
import os
# colors
SILVER = "#BFACAA"
BLACK = "#02020A"
OXFORD_BLUE = "#05204A"
WISTERIA = "#B497D6"
LAVENDER = "#E1E2EF"
RED = "#FF0000"
# Sizes
WIDTH = 800
HEIGHT = 600
# Path
PRJ_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
class Window:
def __init__(self):
self.window = tk.Tk()
self.window.title("Graph Scheduler")
self.window.geometry(f"{WIDTH}x{HEIGHT}")
self.window.configure(background=LAVENDER)
# Title and title box
title_box = tk.Frame(self.window, bg=SILVER, width=WIDTH)
title_box.pack(fill="x")
title = tk.Label(title_box, text="Graph Scheduler", font=("Arial", 40), bg=SILVER, fg=BLACK)
title.pack(pady=5)
# File bar
file_bar = tk.Frame(self.window, bg=OXFORD_BLUE, width=200, height=HEIGHT)
file_bar.pack(fill="y", side="left")
# File bar buttons
file_bar_buttons = tk.Frame(file_bar, bg=OXFORD_BLUE, width=200, height=HEIGHT)
file_bar_buttons.pack(fill="y", side="left")
for file in os.listdir(PRJ_DIR + "/graphs"):
if file.endswith(".txt"):
file_bar_button = tk.Button(file_bar_buttons, text=file,background=RED, fg=SILVER, font=("Arial", 20), width=10, height=2)
file_bar_button.pack(pady=5)
self.window.mainloop()
Window()
use from tkmacosx import Button

Python Tkinter Notebook positioning widgets using ".place"

I am not able to figure out why widgets are not visible when ".place" is used. It works when ".pack" is used. My GUI needs precision positioning of widgets and hence I need to use ".place". Am I doing anything wrong?
Environment: Python: 3.9.6 Windows 10 (21H1)
import tkinter as objTK
from tkinter import ttk as objTTK
root = objTK.Tk()
root.title("Tab Widget")
tabControl = objTTK.Notebook(root)
objSummaryTab = objTTK.Frame(tabControl)
objSettingsTab = objTTK.Frame(tabControl)
tabControl.add(objSummaryTab, text ='Summary')
tabControl.add(objSettingsTab, text ='Settings')
lb1 = objTTK.Label(objSummaryTab, text ="Summary")
lb1.place(x=5, y=5)
lb2 = objTTK.Label(objSettingsTab, text ="Settings")
lb2.place(x=5, y=5)
tabControl.place(x=5, y=5)
root.bind("<Escape>", lambda _: root.destroy())
root.geometry("500x500")
root.mainloop()
This error is because place() does not automatically resize the widgets, like pack() and grid() do. You need to specify width and height arguments for tabControl: tabControl = objTTK.Notebook(root, width=100, height=100), for example. In this case, width and height are in pixels.

Tkinter Widget Size Issues

EDIT: I found the solution! My xorg settings were all wacky because I have an ultrawide monitor, and it had no idea what size (and therefore dpi) my monitor was. Explicitly setting the display and font dpi in X's settings fixed the rendering in all GUI's.
ORIGINAL:
When I run any tkinter program, many of the widgets render at around 1/2 size, especially text. Why is this happening? My window manager is Window Maker, and I'm running the latest version of Tcl/Tk.
Any help will be greatly appreciated!
My code:
import tkinter as tk
window = tk.Tk()
label = tk.Label(text="Name")
entry = tk.Entry()
button = tk.Button(text="Submit")
label.pack()
entry.pack()
button.pack()
window.mainloop()
Window manager: Window Maker 0.95.0
Tk: tk 8.6.10-2
Screenshot:
In tkinter you can only change the size of a label with the font atribute, and same goes for the text inside the button. The button size can be changed with the width and height atribute.
from tkinter import *
window = Tk()
label = Label(text="Name", font='Helvetica 15')
entry = Entry()
button = Button(text="Submit",font ='Helvetica 15', height="3", width="10")
label.pack(pady = 5) # add pady inside the pack
entry.pack()
button.pack(pady = 5)
window.mainloop()

tkinter only displays top of label text

I am making an alarm clock radio using python3, tkinter on a raspberry pi 3 using a 2.8in PiTFT. And I am using a true type font called digital-7. When I create a tkinter Label to hold the time, then only the top 3/4s of the text is displayed. If I add a "\n", then the full text displays. I've done a lot of searching but haven't been able to find a solution. Any help would be appreciated.
Here is a snippet of the code:
import datetime
import tkinter as tk
root = tk.Tk()
root.configure(background='black')
# make root use full screen
root.overrideredirect(True)
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
# using 2.8 PiTFT with 320x240 display
timeText = tk.StringVar()
# digital-7 is a true type font, which shows clock in 7 segment display
timeLabel = tk.Label(root, font=('digital-7', 120), fg='red', bg='black', textvariable=timeText, anchor='n')
timeLabel.grid(row=0, columnspan=6)
def updateDate():
global timeText
dt = datetime.datetime.now()
# without the '\n' the bottom part of the time gets cropped
tts = dt.strftime('%H:%M')
timeText.set(tts+'\n')
root.after(2000, updateDate)
updateDate()
root.mainloop()
The full code is here (it is still a work in progress):
https://raw.githubusercontent.com/dumbo25/tkinter-alarm-clock-radio-gui/master/acr.py
digital-7 goes in /home/pi/.fonts and here is where I got digital 7 from:
https://github.com/dec/angular-canvas-gauge/blob/master/Sample/fonts/digital-7-mono.ttf

Can't change button font size in tkinter

I can't seem to change the size of my font in tkinter! No matter which size I choose, the button text displays the same. If I deleted the whole stlye line, it's displayed smaller.
Similarly, the font always looks the same, no matter what I choose.
I want to finetune the size and the font, can you please help me=?
import tkinter
import tkinter.ttk as ttk
from tkinter import font
root = tkinter.Tk()
frame = ttk.Frame(root)
frame.grid(column=0, row=0)
style = ttk.Style(root)
ttk.Button(frame, text="Open file", command=None).grid(column=0, row=1)
ttk.Style().configure("TButton", font=font.Font(family='wasy10', size=80)) #I can choose any value here instead of "80" and any font like "Helvetica" - nothing will change
root.mainloop()
You do not need to import font. ttk style has its own font argument.
Just put the style in the first option and the font size in the 2nd option.
I would also use the variable name to edit the style. Instead of calling:
ttk.Style().configure()
Do this:
style.configure()
Take a look at the below.
import tkinter
import tkinter.ttk as ttk
root = tkinter.Tk()
frame = ttk.Frame(root)
frame.grid(column=0, row=0)
style = ttk.Style(root)
style.configure("TButton", font=('wasy10', 80))
ttk.Button(frame, text="Open file", command=None, style="TButton").grid(column=0, row=1)
root.mainloop()
On the advice of Bryan Oakley in the comments here is a 2nd option that is close to what you are trying to do with fort.
This option saves a referent to the font object and then uses it to update the style.
import tkinter
import tkinter.ttk as ttk
from tkinter import font
root = tkinter.Tk()
frame = ttk.Frame(root)
frame.grid(column=0, row=0)
style = ttk.Style(root)
font = font.Font(family="wasy10", size=80)
style.configure("TButton", font=font)
ttk.Button(frame, text="Open file", command=None, style="TButton").grid(column=0, row=1)
root.mainloop()

Resources