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.
Related
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.
This question already has an answer here:
How to add an Image to a widget? Why is the image not displayed?
(1 answer)
Closed 4 years ago.
So I'm writing basic stuff in python with tkinter and PIL and the problem here is that when I run the program, only the last button show the image of the item I asked him to show.
So my program was expected to extract name of items from lines containing the name of a game Champion in a txt document.
Each line in the document looks like "ItemName/stat1/stat2/stat3/ChampionName".
The code was then supposed to create buttons with a picture of the item on it(I made sure to name the pngs and the item name in the .txt the same and to put everything in the same folder) but in the end, only the last button had an image on it.
What I tried :
I tried to reduce the number of elements in the txt, it didn't work
I then thought that the problem was that the variable icon being updated, the image shown would disappear. So I tried to make an array but it didn't work either because what I put in isn't an int value, I know it's about the 'i' but I don't know if I can put something else appropriate.
My first attempt :
from PIL import Image, ImageTk
import tkinter as tk
itemwindo = tk.Tk()
itemwindo.title("Items")
data = open("Ressource.txt","r")
for line in data:
if 'Vi' in line:
(a,b,c,d,e) = line.split("/")
icon = ImageTk.PhotoImage(Image.open(a + '.png'))
bt = tk.Button(itemwindo,image=icon)
bt.pack()
itemwindo.mainloop()
And my second with arrays :
data = open("Ressource.txt","r")
imglist = arr.array('i')
for line in data:
if 'Vi' in line:
(a,b,c,d,e) = line.split("/")
icon = ImageTk.PhotoImage(Image.open(a + '.png'))
imglist.extend([icon])
p = len(imglist)
bt = tk.Button(itemwindo,image=imglist[p])
bt.pack()
itemwindo.mainloop()
I would like each button to display the picture of the affiliated item on it.
In the loop that cretes the buttons you use the same name for
every image. This creates a new object every iteration and the
previous buttons can no longer find a reference to the previous image.
To fix this you can assign the image attribute of the button
at creation and then it does no longer make any difference if you reuse the icon or bt name. Study the example below:
import tkinter as tk
itemwindo = tk.Tk()
file_list = ['beer.png', 'test.gif']
for file in file_list:
icon = tk.PhotoImage(file=file)
bt = tk.Button(itemwindo, image=icon)
bt.pack()
bt.image = icon # Save reference to icon in button
itemwindo.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'm using this python module called code128 that generates images of barcodes in code 128 barcode format.
The module works fine alone and produces the barcode desired. But now I'm trying to integrate it with tkinter and get tkinter to display it in a window but with no luck.
Tkinter just shows a black rectangle that would otherwise be the whole barcode. I'm not at all familiar with tkinter, but managed to borrow some functional code, but for some reason this barcode thing isn't working.
import tkinter as tk
from PIL import ImageTk, Image
import code128
#This creates the main window of an application
window = tk.Tk()
window.title("barcode test")
window.geometry("800x600")
#window.configure(background='grey')
#create a barcode image
barcode = code128.image('EL123456789US')
#barcode.show() #for testing purposes. the barcode is generated
#Creates a Tkinter-compatible photo image, which can be used everywhere Tkinter expects an image object.
img = ImageTk.PhotoImage(barcode)
#The Label widget is a standard Tkinter widget used to display a text or image on the screen.
label = tk.Label(window, image = img)
label.image = img #keep a reference!
#The Pack geometry manager packs widgets in rows or columns.
label.pack(side = "bottom", fill = "both", expand = "yes")
#Start the GUI
window.mainloop()
I could just save the barcode to disk and load it from disk (it does work as I've already tried it) but I'm trying to avoid that as I need to display many barcodes one after another. Saving and reading from disk would be too costly.
Any help would be greatly appreciated. Thank you.
I am working on tkinter and what I wanted to do was to show a cancel sign (a PNG) over text that will show the price in background of PNG.
So far I was able to display the text over the image properly using the "compound" option of tkinter.label().
But I have tried different ways to get the PNG on top of text but i haven't succeeded so far.
Is what I am trying to do at all possible? If yes kindly guide, if no, then suggest me a way around. what is the closest I can make using the available features.
I even tried making a cancel sign using canvas.draw() but that also hides the text.
Kindly help me out
Regards
Your best solution to display a partially transparent PNG on top of a text in tkinter is probably to use a Canvas with the create_text() and create_image() methods.
Example code (Python 2.x):
import Tkinter as tk
from PIL import ImageTk
from io import BytesIO
from urllib import urlopen
root = tk.Tk()
canvas = tk.Canvas(root, width=300, height=300)
canvas.pack()
canvas.create_text(150, 150, text="word word word word word word word word")
url = "https://www.dropbox.com/s/etqan5h62d14mv8/Pikachu-PNG-Photos.png?dl=1"
img = ImageTk.PhotoImage(file=BytesIO(urlopen(url).read()))
canvas.create_image(150, 150, image=img)
root.mainloop()
For Python 3.x, use the following in the import statements:
import tkinter as tk
from urllib.request import urlopen
You should get this: