I am using the asksaveasfile function from tkFileDialog to save files using a GUI I'm creating. I'd like to change the color of the font within the dialog saveas window. Scoured the internet and I haven't found anyone asking the same thing.
Asked
Active
Viewed 1,586 times
1
-
additional information: I have an issue with tkinter.filedialog on my debian KDE system. When I use the filedialog, the text inside (the files and folders texts) are all very light, and on a white background. The text is so light that I can just barely see that something is there. When I hover over the faint text with a mouse, holding the left click button down, the text becomes high contrast and visible, as if being highlighted. I have to highlight all the text this way to make it visible. It seems like a bug. (KDE system setting Theme and Style are Breeze Dark & Oxygen, Python 3.9.2) – user12711 Jan 13 '22 at 15:46
1 Answers
2
Disclaimer: I am using Linux and I am not sure whether my answer fully applies to other platforms given that the filedialogs look quite different in Windows.
It is not possible to fully change the color of the font because part of it is hard coded in the tcl code.
The elements surrounding the file list are mostly ttk widgets which can be themed via a ttk.Style
so that they look like the rest of the ttk widgets of the app. The menus can be changed with option_add
.
However, the file list is not as customizable. Indeed, the filenames are set back to black when unselected so there is no way to change that behavior from python.
import tkinter as tk
from tkinter import filedialog
from tkinter import ttk
root = tk.Tk()
root.option_add('*foreground', 'red') # set all tk widgets' foreground to red
root.option_add('*activeForeground', 'red') # set all tk widgets' foreground to red
style = ttk.Style(root)
style.configure('TLabel', foreground='red')
style.configure('TEntry', foreground='red')
style.configure('TMenubutton', foreground='red')
style.configure('TButton', foreground='red')
filedialog.askopenfilename(master=root, filetypes=[('*', '*'), ('PNG', '*.png')])
root.mainloop()

j_4321
- 15,431
- 3
- 34
- 61
-
This is perfect, ill give it a try. The environment text color is default set to white for reasons. In the dialog window when a file is unselected it goes to blackjust like u mentioned, but black is the color i want. Im running the gui on a system that has both 2.4 and 2.7. The gui is coded in tix (on purpose), i didnt think that the filedialog was actually pulling the environment settings for ttk. Systems with only 2.4 installed dont have this problem since i suppose its pulling the environment from only tix. – Joshua Hayhurst Dec 07 '18 at 18:21
-
Also, if you want to target the file/folder list specifically, you can use the patterns `*TkFDialog*foreground` and `*TkChooseDir*foreground` – myQwil May 16 '20 at 08:05
-
A little off topic but I see you are the maintainer of [tkfilebrowser](https://pypi.org/project/tkfilebrowser/) and I wonder if it lets you change the font size of text? In tkinter I can change font size of everything in TCL except text using: `style.configure('.', font=(None, 12))`, This includes `TLabel`, `TButton`, etc. in your list. – WinEunuuchs2Unix Aug 10 '20 at 17:14
-
Ah no sooner did I ask when I found the solution: `default_font = font.nametofont("TkTextFont")` then: `default_font2 = font.nametofont("TkIconFont")` then: `default_font.configure(size=12)` and finally: `default_font2.configure(size=12)` – WinEunuuchs2Unix Aug 10 '20 at 17:37
-
Is it possible to change the size of the icons used for folders and files in the list within the filedialog widget ?. If I change the size of the "TkIconFont" the icon size remains fixed. – user1259970 Jul 13 '21 at 17:00