How do I insert image in my python-tkinter project?
I wanted to insert image in my tkinter Button, I tried it with this code:
photo = PhotoImage(r"Link to folder containing image\image.png")
photoimage = photo.subsample(3,3) # because I want image+text in button
st = Button(root, text='Sample', bg='grey', width=15, height=7, command=callback , image = photoimage, compound= LEFT, font=('Comic Sans Ms',20,'italic'))
st.grid(row=2, column=0, pady=20, side = TOP)
but this code dosen't work, I am using SubLime Text 3 IDE to build my projects
This is the code you need:
from PIL import ImageTk, Image
photo = ImageTk.PhotoImage(Image.open("1.Files/UI/edit.png")
Hope that helps!
The problem might be in the file path. Try escaping it. that means that you have to put two backslashes in a row instead of one when you're writing the path because it thinks that you're trying to escape a special character. you also need to make sure that you pass the path to the file parameter by typing file= before the path.
photo = PhotoImage(file="Link to folder containing image\\image.png")
If the button isn't showing up altogether, then you might have forgotten to pack the Button. Try adding .pack(side=TOP) at the end of the line where you define the button (st = Button(...).pack(side=TOP))
This worked for me.
Related
This is a follow-up question to this:
Get file path from askopenfilename function in Tkinter
and this:
How to get an absolute file path in Python
How do you insert the filepath into a text box in a frame, for example, below a label, and next to a relevant button for browsing to a file via a file dialogue?
e.g.: in procedural code:
with open(somepath.get()) as sp, open(otherpath.get()) as op:
somelines = sp.somelines()
otherlines = op.otherlines()
&
ttk.Label(someframe, anchor='w', width=20, text='Some filepath:').grid(column=0, row=0, padx=5, pady=5)
sp_entry = ttk.Entry(someframe, width=90, textvariable=somepath).sp_entry.pack()
The use case is that you may have a list of similar name text files in a folder, and want to open them up, and see the entire path within a text field, and be able to edit it, in the form:
C:/somepath/../myfile1.txt
C:/somepath/../myfile2.txt
etc...
If anyone can show the difference between implementing this as a class, and as procedural code, that would be interesting.
Thanks.
So, i want to change text color while writing, i tried this code which did nothing apparently:
def text_color():
txt.tag_configure('color',foreground='red')
txt.tag_add('color','1.0',END)
for example, when i am writing a text in Text() widget field by default it is black:
i want to write in text field with red color:
If you want to set the text color to red in the whole text widget, don't use tags, use the foreground option:
import tkinter as tk
root = tk.Tk()
txt = tk.Text(root, foreground='red')
txt.pack()
root.mainloop()
I have a running project, tag_config work for me. The other content is the same as yours.
I am making an animated presentation and I want to add an image to it, but I can't use PIL because it will be presented at school, where PIL isn't installed, so I used the method given by the website http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm, but it doesn't work - my slide is still blank. What should I do?
Here is my code (copied almost completely from the website - did I miss something out?):
photo = PhotoImage('Alveoli.png')
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()
You can not open .png files without using an external image library. Since PIL isn't available I'd think other similar libraries aren't either.
EDIT: Note: PNG files are supported for tkinter v8.6+.
The only possibility that comes to mind is converting the image to another, compatible format like GIF or PGM
If you have the image in one of those formats you can simply add them. It worked just fine for me with this code:
from tkinter import *
root = Tk()
photo = PhotoImage(file="img.ppm")
img = Label(root, image=photo)
img.image = photo
img.place(x=0, y=0)
root.mainloop()
I have attached a screenshot of this person's gui and was wondering if anyone knows how he got the white box on the right.
In the video it does not show but is it a label or something? I am quite new and have never seen this before. I need to add my own data into it using variables but I don't know how to format the entire box.
It is a Text widget:
from tkinter import *
root = Tk()
t = Text(root)
t.pack()
root.mainloop()
You can also change the size by doing t = Text(root, width=50, height=25). Height is actually the number of lines and width the number of characters on one line, not pixels. For more formatting options, go here.
I have developed some application on my laptop with python and tkinter. Then, I was stuck at some point. Question is : how can I change text position on image.
import tkinter as tk
from PIL import Image, ImageTk
path_to_pic = "....."
root = tk.Tk()
pic = Image.open(path_to_pic)
tkpic = ImageTk.PhotoImage(pic)
tk.Label(root, image = tkpic, text = ".....", compound = tk.CENTER).pack()
root.mainloop()
This shows that my text appears on the picture, only on the center. I would like to move my text little by little and find best position. Do you know any solution or similar way to achieve this ?
You can move text horizontally and vertically by adding spaces and '\n's respectively, to any side(s) of the text you wish:
text = "caption \n\n\n\n\n\n\n"
This will put "caption" at the top left of the text.