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
Related
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)
I'm a Python beginner that trying to learn my way through Tkinter, and I need your help.
Let say I create a simple button like this:
import tkinter as tk
window = tk.Tk()
button = tk.Button(text="Hello World!")
button.pack()
window.mainloop()
Is there a way that I can hide and then display the text again? Given that I can create two more buttons that will do the job of hiding and displaying. I have tried to use button.pack_forget(), but it will hide the entire button instead.
Any help would be appreciated.
To make it look like the buttons text vanishes,you can make the text color as same as the background color via cget method.
import tkinter as tk
def hide_text():
color = button['bg']
button.config(foreground=color, activeforeground=color)
window = tk.Tk()
button = tk.Button(text="Hello World!",command=hide_text)
button.pack()
window.mainloop()
Approach 2 ttk.Button:
import tkinter as tk
from tkinter import ttk
def hide_text():
button.config(text='')
window = tk.Tk()
button = ttk.Button(text="Hello World!",width=100,command=hide_text)
button.pack()
window.mainloop()
Approach3 using style:
import tkinter as tk
from tkinter import ttk
def change_button_style(event):
widget = event.widget
if widget['style'] == 'TButton':
widget.configure(style='VanishedText.TButton')
else:
event.widget.config(style='TButton')
BACKGROUND = '#f0f0f0'
FOREGROUND = '#000000'
window = tk.Tk()
window.configure(bg=BACKGROUND)
style = ttk.Style()
style.theme_use('default')
button = ttk.Button(text="Hello World!",style='VanishedText.TButton')
button.bind('<ButtonPress-1>',change_button_style)#,command=change_button_style
button.pack()
style.map('VanishedText.TButton',
foreground =[('disabled',BACKGROUND),
('!disabled',BACKGROUND),
('pressed',BACKGROUND),
('!pressed',BACKGROUND),
('active',BACKGROUND),
('!active',BACKGROUND)],
background =[('disabled',BACKGROUND),
('!disabled',BACKGROUND),
('pressed',BACKGROUND),
('!pressed',BACKGROUND),
('active',BACKGROUND),
('!active',BACKGROUND)],
focuscolor=[('disabled',BACKGROUND),
('!disabled',BACKGROUND),
('pressed',BACKGROUND),
('!pressed',BACKGROUND),
('active',BACKGROUND),
('!active',BACKGROUND)])
window.mainloop()
You Can Simply Config And Set Text :
import tkinter as tk
from tkinter import ttk
win=tk.Tk()
def hidetext():
button.config(text="")
button=ttk.Button(win,text="FooBar",command=hidetext)
button.pack()
win.mainloop()
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 does tkinter.Button() appear as some ancient OS style button, while a messagebox like tkinter.messagebox.showinfo() comes with an OK button using the current version of the OS?
My OS is Windows. Not sure if this problem exists on Mac OS, but either way the people using my tools are on Windows.
An example snippet of code I found here shows how the buttons are different.
Image:
Question:
Is there a way to make tkinter.Button() look like the button inside a messagebox, which seems to be using the current OS style?
Code:
from tkinter import *
from tkinter import messagebox
window = Tk()
window.title("Welcome to LikeGeeks app")
window.geometry('350x200')
def clicked():
messagebox.showinfo('Message title', 'Message content')
btn = Button(window,text='Click here', command=clicked)
btn.grid(column=0,row=0)
window.mainloop()
Use tkinter.ttk to get themed version
from tkinter.ttk import *
from tkinter import messagebox
window = Tk()
window.title("Welcome to LikeGeeks app")
window.geometry('350x200')
def clicked():
messagebox.showinfo('Message title', 'Message content')
btn = Button(window,text='Click here', command=clicked)
btn.grid(column=0,row=0)
window.mainloop()
doc
You can use tkinter.ttk which provides a modern OS style theme to your TK widgets. Example from the Python tkinter docs:
from tkinter import ttk
import tkinter
root = tkinter.Tk()
ttk.Style().configure("TButton", padding=6, relief="flat",
background="#ccc")
btn = ttk.Button(text="Sample")
btn.pack()
root.mainloop()
#Output:
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