Python Forum
[Tkinter] Why is it so difficult to just ouput an image where I want?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Why is it so difficult to just ouput an image where I want?
#11
(Apr-06-2019, 04:33 AM)wuf Wrote: Hi jpezz

Make following alteration:
image2 = Image.open("pngs/b.png")
root.test_image2 = ImageTk.PhotoImage(image2)
image_label.place(x=600, y=50)
image_label.config(image=root.test_image2)
wuf :-)
This command moves the A over from x=500 to x=600
image_label.place(x=600, y=50)
Then this command overwrites the A with the B
image_label.config(image=root.test_image2)
The result is the B on top of the A with the B in the correct position.

Full program:
# -*- coding: utf-8 -*

try:
    # Import Tkinter for Python 2.xx
    import Tkinter as tk
except ImportError:
    # Or import Tkinter for Python 3.xx
    import tkinter as tk

from PIL import Image, ImageTk
import time

MAIN_WIN_TITLE = "Image Placements"
MAIN_WIN_XPOS = 200
MAIN_WIN_YPOS = 100
MAIN_WIN_WIDTH = 800
MAIN_WIN_HEIGHT = 600


root = tk.Tk()
#root.title(MAIN_WIN_TITLE)

# Remove titlebar
#root.overrideredirect(1)

# Placement of the main window
root.geometry("{}x{}+{}+{}".format(
    MAIN_WIN_WIDTH, MAIN_WIN_HEIGHT, MAIN_WIN_XPOS, MAIN_WIN_YPOS))

# Get the .jpg image and convert it to tkinter format
image = Image.open("pngs/a.png")
root.test_image = ImageTk.PhotoImage(image)

#
# Create a label widget containig the image and place it directly on
# the main window
image_label = tk.Label(root, text="Image Label placed\non main window",
    compound='bottom', image=root.test_image)
image_label.place(x=500, y=50)
 
image2 = Image.open("pngs/b.png")
root.test_image2 = ImageTk.PhotoImage(image2)
image_label.place(x=600, y=50)
image_label.config(image=root.test_image2)

root.mainloop()
Reply
#12
Hi jpezz

Could you please exactly what you want to achieve???

wuf :-)
Reply
#13
(Apr-07-2019, 10:05 AM)wuf Wrote: Hi jpezz

Could you please exactly what you want to achieve???

wuf :-)

I apologize for not being clear.
I want to create a Label big enough to hold x images but initially containing only one. Then at sometime later, I want to add another image to that Label to the right of it. I know the element sizes so I know where to position the second image. I want to repeat adding images to the Label, each to the right of the last.
At some future time in the program, I intend to destroy the label and start all over.

Te old program calls "feh" for each image to output it in the correct position but this causes a number of issues and I have to kill all the "feh" processes from the python program to clear the screen. I want to do it within python and to only have to worry about a single label for each image group as I want to put multiple image groups on the screen in different places. Each group (4 groups total) and possibly up to 20 or more images in a group. I don't want up to 80 labels to manage.
Reply
#14
Hi jpezz

Thanks for your reply. In a label you can insert one image only! But you can place many label each containing a image in a frame container.
Here a script with totally three image label in one row: (For the trial all with the same image!)
# -*- coding: utf-8 -*

try:
    # Import Tkinter for Python 2.xx
    import Tkinter as tk
except ImportError:
    # Or import Tkinter for Python 3.xx
    import tkinter as tk
    
from PIL import Image, ImageTk

MAIN_WIN_TITLE = "Image Placements"
MAIN_WIN_XPOS = 100
MAIN_WIN_YPOS = 100
MAIN_WIN_WIDTH = 800
MAIN_WIN_HEIGHT = 600
 
main_win = tk.Tk()
main_win.title(MAIN_WIN_TITLE)

# Placement of the main window
main_win.geometry("{}x{}+{}+{}".format(
    MAIN_WIN_WIDTH, MAIN_WIN_HEIGHT, MAIN_WIN_XPOS, MAIN_WIN_YPOS))

# Get the .jpg image and convert it from pil-format to tk-format
pil_image_a = Image.open("pngs/a.jpg")
main_win.tk_image_a = ImageTk.PhotoImage(pil_image_a)

image_container = tk.Frame(main_win)
image_container.pack()

# Place first image-label in the image-container
image_label = tk.Label(image_container, text="Image-1",
    compound='bottom', image=main_win.tk_image_a)
image_label.grid(row=0, column=0)

# Place second image-label to the left side of first label
# in the image-container
image_label = tk.Label(image_container, text="Image-2",
    compound='bottom', image=main_win.tk_image_a)
image_label.grid(row=0, column=1)

# Place third image-label to the left side of second label
# in the image-container
image_label = tk.Label(image_container, text="Image-3",
    compound='bottom', image=main_win.tk_image_a)
image_label.grid(row=0, column=2)


main_win.mainloop()
What is the size of your images?

wuf :-)
Reply
#15
wuf,

Yeah, that does more like what I want. Thanks.
The image sizes are as follows"
characters: 100x153
"dot": 100x100
"dash": 100x300
All are 100 wide. Letters will not be on same line as dot/dash.
I may change the size of characters if it takes too much room on line. We haven't decided the max length of number of characters. dot/dash will be no wider than 5 dashes (1500 pixels). Frame will have most of screen. Four lines will be needed - two text & two dot/dash. Will need to overwrite longer line with shorter one (even if it means filling in with blanks at end or deleting the label.
No image labels.

Note, I modified your program to output 3 different characters (and changed the code to use .png files instead of .jpg files and that worked great.
I also put characters on one line and dots/dashes on the next and that worked great.
BTW, your documentation says you are putting them on the left of the previous but should say the right of the previous.

Thanks. I'll try playing with using a frame and putting things in it. Give me a few days to try out putting multiple labels in a frame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Switching from tkinter to gtk is difficult! snakes 1 1,421 Aug-08-2022, 10:35 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020