Python Tkinter Font Chooser - python

I'm trying to write simple notepad with Tkinter. And I need some font chooser. So my question is: is there included one? and if there isn't, where can I get one?
Thanks.

Tk (and Tkinter) doesn't have any font chooser in the default distribution. You'll need to create your own. Here is an example I found: Tkinter FontChooser
Note: Tk 8.6 will have a build in font chooser dialog: FontChooser

The basis for a simple font selector can be found below
import tkinter as tk
from tkinter import font
def changeFont(event):
selection = lbFonts.curselection()
laExample.config(font=(available_fonts[selection[0]],"16"))
root = tk.Tk()
available_fonts = font.families()
lbFonts = tk.Listbox(root)
lbFonts.grid()
for font in available_fonts:
lbFonts.insert(tk.END, font)
lbFonts.bind("<Double-Button-1>", changeFont)
laExample = tk.Label(root,text="Click Font")
laExample.grid()
root.mainloop()
Double clicking on a font in the list will change the example text below to that font. You can scroll down through the list of fonts by using a mouse wheel (or you can add a scroll bar to it)

There is a font chooser window in tkinter but it has been discontinued in the newer versions but you can still access it.
l = ttk.Label(root, text="Hello World", font="helvetica 24")
l.grid(padx=10, pady=10)
def font_changed(font):
l['font'] = font
root.tk.call('tk', 'fontchooser', 'configure', '-font', 'helvetica 24', '-command', root.register(font_changed))
root.tk.call('tk', 'fontchooser', 'show')
Check this link for more details: https://tkdocs.com/tutorial/windows.html

Use rvfont module for font chooser It is easy to use
pip install rvfont
code:
from rvfont.rvfontchooser import FontDialog
fonts=FontDialog()
It will give fonts option in dictionary

Related

How do i turn off the button "glowing" on python tkinter (Linux)?

So, i've been learning a bit of tkinter and i've just found out that some things like buttons work differently from windows to linux... On windows everything works fine but on linux, when i over the button (with an image) it glows and, honestly, i can't figure out how to fix this.
from tkinter import *
import tkinter as tk
import PySimpleGUI as sg
root = tk.Tk()
root.title("Test")
root.geometry("500x500")
close_icon = PhotoImage(file='assets/Close Button.png')
close_button = tk.Button(root, image=close_icon, command=root.destroy, borderwidth=0)
close_button.pack()
if __name__ == "__main__":
root.mainloop()
What it should always look like:
What it looks like when i hover it:
Try to set option activebackground to be the same background color of the window.
color = root.cget("background")
close_button.configure(activebackground=color)

How to unbold the font used to show messages in the tkinter.messagebox.showinfo() widget? [duplicate]

I'm using the simple message-boxes provided in tkinter and I'm wondering if there's any way to change the font.
This is the general idea of what I want, but the font= option doesn't work.
from tkinter import Tk
import tkinter.messagebox as tkmsg
_root = Tk()
_root.withdraw()
tkmsg.showinfo(
"Info",
"Some monospaced text",
font=("Monospace", 15)
)
_root.destroy()
Is there any way to change the font or do I have to use a custom dialog?
See here for how to change the dialog box text: Control Font in tkMessageBox
In short (copied verbatim from the link above):
You can configure the font for just dialog boxes by doing the following:
from Tkinter import *
import tkMessageBox
r = Tk()
r.option_add('*Dialog.msg.font', 'Helvetica 12')
tkMessageBox.showinfo(message='Hello')
Be sure to call r.option_clear() to set the font back to normal afterwards.
You can change the default font for captions:
import tkinter as tk
from tkinter import messagebox as mb
from tkinter import font
root = tk.Tk()
font1 = font.Font(name='TkCaptionFont', exists=True)
font1.config(family='courier new', size=20)
mb.showinfo(message='Hello')
You should write your own messagbox. Tkinter invoke system dialog for Windows or Mac and genetate dialogs for Linux. In all cases is imposible change Tkinter dialogs.
you can't.
write your own messagebox using the toplevel widgted (tkinter.Toplevel()) and label!
Something like this (from http://effbot.org/tkinterbook/label.htm)
from Tkinter import *
master = Tk()
w = Label(master, text="Hello, world!")
w.pack()
mainloop()
i hope it helps!
Edit: This is a very old answer, 3 years later someone said it's possible:
Control Font in tkMessageBox

Why is my button not getting grey nor with a border?

I want my button to get gray when I pass the cursor through it, but it does nothing, it doesnt have a border too
I tried 'gray', 'grey', 'Grey', 'Gray'
from tkinter import *
from tkinter import Tk, StringVar, ttk
def oi():
print('oi')
def menu(cany_list):
#some code...
#lots of code...
#created some frames including left
for i in candy_list:
i = Button(left, text=i, width = 250, activebackground = 'Gray', command = oi, bd = 5, font =('courier', 12))
i.pack(side=TOP)
i am getting a button that executes the command and has a text and the font i choosed but not a border nor a active background
The proper way to do it is to set activebackground of the Button widget, however on windows this doesn't seem to work as expected. A workaround will be binding two events to the widget, which is <Enter> and <Leave>. In this case, it will be something like this:
import tkinter as tk
root = tk.Tk()
a = tk.Button(root,text="Hover me")
a.pack()
a.bind("<Enter>",lambda e: a.config(bg="red"))
a.bind("<Leave>",lambda e: a.config(bg=root.cget('bg')))
root.mainloop()
Windows and Mac do not allow to change the button background color (I think because they are created from images).
If you want to change the button background color, you can use a ttk.Button and change the theme for one that allows such customization, like 'clam' or 'alt'. ttk.Button has no activebackground option, the appearance is set through the style with style.configure for the normal appearance ('normal' state, inactive button) and style.map for the dynamic appearance (disabled, active, ...).
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
style = ttk.Style(root)
style.theme_use('clam') # change theme
# set active color to gray for the button with style 'gray.TButton',
# if you want it to apply to all buttons, change it to 'TButton'
style.map('gray.TButton', background=[('active', 'gray')])
ttk.Button(root, text='Ok', style='gray.TButton').pack()
root.mainloop()

Drag and drop files onto a Tkinter Gui

Assumption: I'm using Python 3.6 and I'm working on Windows 10
Is possible to create a GUI with tkinter in which dragging a file in the window it returns the path of the file?
If with tkinter it's not possible, is there another solution that can solve the problem without installing additional libraries?
You need to install tkinterdnd2
pip install tkinterdnd2
code:
from tkinter import TOP, Entry, Label, StringVar
from tkinterdnd2 import *
def get_path(event):
pathLabel.configure(text = event.data)
root = TkinterDnD.Tk()
root.geometry("350x100")
root.title("Get file path")
nameVar = StringVar()
entryWidget = Entry(root)
entryWidget.pack(side=TOP, padx=5, pady=5)
pathLabel = Label(root, text="Drag and drop file in the entry box")
pathLabel.pack(side=TOP)
entryWidget.drop_target_register(DND_ALL)
entryWidget.dnd_bind("<<Drop>>", get_path)
root.mainloop()

How to set font of a messagebox with Python tkinter?

I'm using the simple message-boxes provided in tkinter and I'm wondering if there's any way to change the font.
This is the general idea of what I want, but the font= option doesn't work.
from tkinter import Tk
import tkinter.messagebox as tkmsg
_root = Tk()
_root.withdraw()
tkmsg.showinfo(
"Info",
"Some monospaced text",
font=("Monospace", 15)
)
_root.destroy()
Is there any way to change the font or do I have to use a custom dialog?
See here for how to change the dialog box text: Control Font in tkMessageBox
In short (copied verbatim from the link above):
You can configure the font for just dialog boxes by doing the following:
from Tkinter import *
import tkMessageBox
r = Tk()
r.option_add('*Dialog.msg.font', 'Helvetica 12')
tkMessageBox.showinfo(message='Hello')
Be sure to call r.option_clear() to set the font back to normal afterwards.
You can change the default font for captions:
import tkinter as tk
from tkinter import messagebox as mb
from tkinter import font
root = tk.Tk()
font1 = font.Font(name='TkCaptionFont', exists=True)
font1.config(family='courier new', size=20)
mb.showinfo(message='Hello')
You should write your own messagbox. Tkinter invoke system dialog for Windows or Mac and genetate dialogs for Linux. In all cases is imposible change Tkinter dialogs.
you can't.
write your own messagebox using the toplevel widgted (tkinter.Toplevel()) and label!
Something like this (from http://effbot.org/tkinterbook/label.htm)
from Tkinter import *
master = Tk()
w = Label(master, text="Hello, world!")
w.pack()
mainloop()
i hope it helps!
Edit: This is a very old answer, 3 years later someone said it's possible:
Control Font in tkMessageBox

Categories

Resources