I want to download an open source font and use it in my Python Tkinter Program.
How can I tell Tkinter to import a font from a directory or by putting the font in the same folder as the program ?
Note:
I have searched for an answer for a while even reading API reference of Tkinter about every font related thing I could find. If there was an obvious answer to this question and I didn't know because maybe I didn't search hard enough or asked the wrong questions I am sorry.
This worked for me on Windows (and I would guess on any platform which supports the pyglet module)
import tkinter as tk
import pyglet, os
pyglet.font.add_file('myFont.ttf') # Your TTF file name here
root = tk.Tk()
MyLabel = tk.Label(root,text="test",font=('myFont',25))
MyLabel.pack()
root.mainloop()
Related
I have a functioning code that will not show text on the buttons unless I click and hold the button. When releasing the button's text becomes blank again. I have tried solving this on other forums and
please note: THERE IS NOTHING WRONG WITH THE CODE ITSELF.
There is some sort of issue with how my laptop is running the code. I've tried reinstalling python3.7 as well as tcl-tk. I really don't know what else to do.
Here is solution to add this line from tkinter import ttk.
and than use ttk. everywhere you plain to use Button, Label, Entry, etc.:
ttk.Button(text="Login", width="30", command = login).pack()
Here is code so you can try it.
def main_screen():
from tkinter import ttk
global screen
screen = Tk()
screen.geometry("500x500")
screen.title("4rManager")
Label(text="Login/Register", font=("Calibri", 13)).pack()
ttk.Label(text="").pack()
ttk.Button(text="Login").pack()
ttk.Label(text="").pack()
ttk.Button(text="Register").pack()
screen.mainloop()
main_screen()
Hope I helped.
The reason for that might be how you installed the python. For some, if you installed from hombrew, it doesn't work.
So, try removing python in hombrew. Download and install it from the actual website.
Updating doesn't work for everyone, I've read several posts of those for whom it didn't, and it didn't for me.
But since resizing the window works, there must be something that the tk instance does when resizing that makes everything appear again.
So you might check what happens there, and use that if your program detects if the OS is Mojave. Obviously this is not production worthy and only a fix for your own local projects or maybe some course project, but at least it's something.
I would like to know where does tkinter load his fonts from.
Is it from /usr/share/fonts or does it have a specific folder ?
thanks
So as Bryan said, tkinter gets the fonts from the standard font locations of the OS. Putting fonts there will permit to tkinter to be able to load them.
Thanks Bryan
I'm trying to have the user select a file through a pop-up directory browser. Looking things up suggested that I use filedialog.askopenfile(), which works but leaves a small unresponsive tk window that I'd rather not have because any attempt to interact with it causes the kernel to crash and need to be restarted.
Does anyone have any solutions or alternatives? Thanks.
This works fine:
from tkinter import *
root = Tk()
root.withdraw()
filedialog.askopenfile()
I can do the following in my program to get a simple open file dialog and print the selected file path. Unfortunately it doesn't go away right away when the user selects the file, and stays around for over 5 minutes. How do I make the window disappear immediately once a selection has been made before executing more python code? After the Tkinter code I do try to import some video using OpenCV which I think may be causing the slowing. My OpenCV code does execute properly and I don't think there is a problem with that alone (i.e. some interaction is causing the error & maybe some intensive process is started before Tkinter wraps up its GUI dialog).
import Tkinter as Tk
import cv2
from tkFileDialog import askopenfilename
root = Tk.Tk()
root.withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)
cap = cv2.VideoCapture('video.mp4') # this works just fine
I am using Python 2.7 and Mac OS X 10.9 if that is useful.
[EDIT: This does not seem to be a problem for all, but it is for me, so I am changing the question to also include debugging the problem. I don't want anything to execute until the Tkinter open file dialog window is done closing in the GUI. It seems that a subsequent step in my program (an open cv video import) could somehow be causing Tkinter to slow things down, so I want to ensure it does close before any new process is started. Again, the Tkinter window does actually close after 5 minutes...]
I was having some trouble with Tkinter dialog boxes. Like you, the dialog just sat there after I had selected a file. I didn't try leaving it for very long, it may have disappeared after 5 minutes as you said your's did. After lots of random experimentation I found that calling root.update() before the askopenfilename() line seemed to fix it.
For reference, this is the code I was testing with:
import sys
from tkinter import *
from tkinter import filedialog
#instantiate a Tk window
root = Tk()
#set the title of the window
root.title('Tk test')
# I don't know, what this does, but it fixes askopenfilename if I use it.
root.update()
print(filedialog.askopenfilename(title='dialogue? surely.'))
Exactly the problem I had - sometimes the file dialog would go away after a while, sometimes not. But it always seemed to block a later status windows. Adding the root.update() fixed both problems immediately.
Python 2.7 (32-bit) Windows: We're experimenting with Python 2.7's support for themed Tkinter (ttk) for simple GUI's and have come away very impressed!! The one area where the new theme support seems to have come up short is how OS specific common dialogs are wrapped.
Corrected: In other words, the MessageBox and ColorChooser common dialogs have "ugly" looking Win 95 style blocky looking buttons vs. the themed (rounded/gradient) buttons that normally show up on these common dialogs under XP, Vista, and Windows 7. (I'm testing on all 3 platforms with identical, un-themed results).
Note: The filedialog common dialogs (askopenfilename, askopenfilenames, asksaveasfilename, askdirectory) are all properly themed.
import tkMessageBox as messagebox
messagebox.showinfo()
import tkColorChooser as colorchooser
color = colorchooser.askcolor( parent=root, title='Customize colors' )
Any ideas on what's required to get Tkinter's MessageBox and ColorChooser common dialogs to be OS theme compatible (at least under Windows XP or higher)?
Your observation is mainly correct. I do see what you are referring to in the messagebox and the colorchooser. However, my filedialogs all seem to have properly rounded buttons, etc.
My recommendation for you on making the messagebox is to create your own messagebox using the TopLevel widget, and then define what you need on it and the appropriate behavior for the different buttons (it's definitely a bit harder than just using a messagebox, but if you really need the new style buttons, it'll work).
I don't think you can hack together a solution for the colorchooser problem, however.
I though for a minute that perhaps Python 3.1 had fixed this problem, but sadly, I tried and that isn't the case. I suppose if you need the user to pick a color, the buttons will have to be ugly.
An option to get better looking dialog boxes is to compile your script to an executable using pyinstaller. I explain this more thouroughly here.
tl;dr, it appears that compiling with pyinstaller allows you to have dialog boxes with the style of the currently running OS, but not custom styles.