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")
Related
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."
I am trying to make an image slide show application using Tkinter and Pillow. I would like the image to go full screen, so currently my code looks like this (I think these are all the important bits, ask me if you need to see more):
canvas = Canvas(root, width=screenwidth, height=screenheight, bg=‘black’)#screenwidth and height previously assigned (checked to be correct) variables containing screen dimensions.
image = image.resize((resizew, resizeh) Image.ANTIALIAS)
imagesprite = canvas.create_image(midx, midy, image=photo) #had changed our resized image to a tkinter photo image previously, midx and midy are just half the screen dimensions.
The problem:
No matter what settings I change there is always some form of grey bar around the edge of the window. I have tried changing the window size, changing the canvas size, setting the window geometry manually using root.geometry to no avail. However, some of the combinations of settings lead to there being fewer bars; I have seen between 1 and 3. Pictures of the output in its current state are attached. There are no errors in the shell, not (currently) is there a border on the left of the image
[1]: https://i.stack.imgur.com/1DLfg.jpg
You need to set highlightthickness=0 when creating the canvas:
canvas = Canvas(root, width=screenwidth, height=screenheight, bg='black', highlightthickness=0)
Tkinter canvas.postscript is not saving the canvas background.
Sizes of the canvas that are used throughout the Python code:
w = 800
h = 600
Function to be assigned to "SAVE CANVAS' button:
def save_canvas():
canvas.update()
canvas.postscript(file= r'Z:\\...\FILE.ps', height=h, width=w, colormode='color')
When I click on the button that has the save_canvas command assign, the file that is saved has no background as assigned in the widget. I changed the color to orange, green, etc. draw on it. Everything looks ok, but saving is without background. Same with .jpeg/.png
What do I need to call for saving the background? I'll be needing this as the application I build require images as background as well.
I don't believe the postscript command is designed to preserve the background color of the canvas widget. It only saves the items that appear on the canvas.
A simple solution is to draw a rectangle that is the exact size of the canvas, and change the color of that rectangle.
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'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