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
I am working on a Python 3 tkinter app that uses windows with vertical and horizontal (mouse-scrollable) scrollbars along the window's dimensions. But I am unable to display a .png image as the background on the same windows.
I've gone through the methods to add a .png image as the background using a label and canvas but instead of the picture, I can only see white on regions of the window not covered by the frames. Also I find the canvas method really hard as it becomes very difficult to place frames on it which in turn have labels, buttons, scrollbars etc. I've also tried using PIL module but I'm finding trouble importing it from its path (I also don't think it would help much as I'm only trying to use a .png image).
Any suggestions on how to have both the scrollbars and the background image working in the same tkinter window?
#Function to create tkinter window with horizontal and vertical scrollbars
from tkinter import *
from tkinter import ttk
def win():
r0=Tk()
r0.title('title')
f=Frame(r0,bg='BLACK')
f.pack(fill=BOTH, expand=1)
#Creating canvas for attaching scrollbars
c=Canvas(f,bg='BLACK')
c.pack(side=LEFT, fill=BOTH, expand=1)
s1=ttk.Scrollbar(f, orient=VERTICAL, command=c.yview)
s2=ttk.Scrollbar(f, orient=HORIZONTAL, command=c.xview)
s1.pack(side=RIGHT, fill=Y)
c.configure(yscrollcommand=s1.set)
s2.pack(side=BOTTOM, fill=X)
c.configure(xscrollcommand=s1.set)
c.bind('<Configure>', lambda e:c.configure(scrollregion=c.bbox('all')))
#Function to enable mouse scroll
def mw(event):
c.yview_scroll(-1*int((event.delta/120)), 'units')
c.bind_all('<MouseWheel>', mw)
r=Frame(c, bg='BLACK')
c.create_window((0,0),window=r, anchor='s')
return r, r0
r,r0=win()
#Adding images as background in tkinter window using labels
r=Tk()
# Adjust size
r.geometry("800x800")
bg=PhotoImage(file="Your_img.png")
# Show image using label
l1 = Label(r, image=bg)
l1.place(x=0, y=0)
#Adding images as background in tkinter window using canvas
r=Tk()
# Adjust size
r.geometry("800x800")
bg=PhotoImage(file="Your_img.png")
c1=Canvas(r, width=800, height=800)
c1.pack(fill="both", expand=True)
# Display image
c1.create_image(0, 0, image=bg, anchor="nw")
I tried results from other answers but that didn't solve my Problem.
Here is a screenshot of my App on which I am working:
There above, (the one with >myIDLE text) is my Text box in disabled state.
When I decrease the font and use sticky="nsew" method, it works fine. But, when I increase the font and use the same method, something like this happens:
How to permanently FIX the size?
Currently I am declaring size like this:
from tkinter import *
root = Tk()
root.state('zoomed')
root.title("myIDLE Window")
root.iconbitmap(".\\resources\\images\\myIDLE.ico")
root.resizable(0, 0)
swidth = root.winfo_screenwidth()
sheight = root.winfo_screenheight()
mainf = Frame(root)
mainf.grid(row=0, column=0, padx=5, pady=7)
display = Text(mainf, height=int(33*(sheight/864)), width=int(112*(swidth/1536)))
display.grid(row=0, column=0)
root.mainloop()
I read in one answer about a similar question to use .grid_propogate(False)
I used it with following result:
On root window: Nothing changed
On Frame (mainf): Screen became white
On Text Widget: Nothing Changed
Please tell a way so I fix this problem
Also I am sorry I cant share my full code, but pls feel free to ask any part of the code
If you want the text widget to be a specific size in pixels, the following technique works:
Create a frame with the size that you want
turn geometry propagation off for this frame
create a text widget with a width of 1 and a height of 1, using the frame as its master
add the text widget to the frame so that it fills the frame.
add the frame to your app however you want, using grid, pack, or place.
The following example will appear to create a text widget that is 400 pixels wide and tall. Changing the font will not change the size since it is the frame controlling the size of the text widget rather than the text widget controlling the size of the frame.
import tkinter as tk
...
text_frame = tk.Frame(root, width=400, height=400)
text_frame.pack_propagate(0)
text = tk.Text(text_frame, width=1, height=1)
text.pack(fill="both", expand=True, padx=20, pady=20)
...
Found this question through Google. Ended up finding an easy solution.
By using the root.geometry() method, increasing and decreasing font will no longer change the size of the window.
For example, you may do root.geometry("250x250").
I imagine some background Boolean gets ticked off upon its usage that says "the program specified a size for the window, so it probably doesn't want to be changed through external means."
My application was recently working fine, but when I upgraded Python to v3.7.2, which solved another problem I was having, the images are either no longer shown or of poor quality.
After doing some testing, I've concluded that I can display any PNG without transparency, but PNGs with transparency exhibit this problem. I can recreate it with this code:
import tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()
path = 'png-transparent.png'
img = Image.open(path)
imgtk = ImageTk.PhotoImage(img)
panel = tk.Label(root, image=imgtk).pack()
root.mainloop()
If I use a label to display the image, it doesn't show at all. If I use a button to display the image, which is what I really need to do, it is poor quality (which means not displaying properly, no definition, coarse edges).
They are very small (16x16), but you can see two different icons here, the ones in the boxes are the distorted ones and the ones not in boxes are how they are supposed to look.
Current versions: Python 3.7.2, Pillow 5.4.1, OS X 10.13.6
I am using colorama to change the colour of my text and it works fines, but now I want to change the colour of the whole background instead of just my text.
I am using Windows:
import colorama
from colorama import Fore, Back, Style
colorama.init()
print(Back.GREEN)
print(Fore.RED)
But, that code only makes the text coloured. Is there a way to do that in python? I want it like CMD where you can have the background of it a colour. I cannot use the OS module as I do not have admin rights, but I'm open to using any other module.
How do I solve this problem?
After a while in playing with it i figured it out. Just forgot about this post. Here is what I did.
import colorama
from colorama import Back as bg
colorama.init()
print(bg.RED)
print(colorama.ansi.clear_screen())
I think clearing the screen fixed the Issue
After playing with colorama on my Windows 10 box it seems it's only used to change the text but not the console/terminal background. I was however able to change the background using this standard library solution:
import ctypes
try:
ctypes.windll.msvcrt.system(b"color 4f")
print("Hello World!")
except ValueError:
pass
The terminal background will change to red with white text.
I am making a widget with Tkinter in python 3.4. For some reason, I cannot change a label's background color from the default grey. The code for the label is something like this:
self.label = ttk.Label(master, text="Label Text",
foreground="blue", background="yellow")
Everything else works fine. I can change the foreground (text) color, however the background will not change, whether I am using label.config(), label['background'], or whatever.
I can change the background if I write it for Python 2.7, but I am using tutorials for Tkinter in 3.4, so this is undesirable.
This bug is caused by the 'aqua' ttk style on Mac OSX. It also breaks 'ttk.Progressbar' when set to 'indeterminate' mode. To fix both issues insert the following code after 'root = Tk()' to change the style ...
style = ttk.Style()
style.theme_use('classic') # Any style other than aqua.
This solution was posted by dietrich41
here : http://www.python-forum.org/viewtopic.php?f=12&t=16212
I tested it on a Mac running Python 3.4.1.