pythontkinterphotoimage

How do I place sentence on the top?


I'm trying to place sentence (welcome currency converter) on the top, but can't success.

import tkinter as tk

my_window = tk.Tk()
photo = tk.PhotoImage(file='currency conventer.png')
background_window = tk.Label(my_window,
                          text='Welcome\nCurrency Converter',
                          image=photo,
                          compound=tk.CENTER,
                          font=('Calibri',20,'bold italic'),
                          fg='black')
background_window.pack()
my_window.mainloop()

Solution

  • Two things,

    1. You need to use compound=tk.BOTTOM so that the image will be below your text.

    2. If you image is too big, you will need to resize it so that it doesn't not "push" your text off the top of the screen.

    Try this:

    import tkinter as tk
    from PIL import Image, ImageTk
    
    my_window=tk.Tk()
    image = Image.open('currency conventer.png')
    image = image.resize((250, 250), Image.ANTIALIAS) # resize image to that it fits within the window. If the image is too big, it will push your new label off the top of the screen
    photo=ImageTk.PhotoImage(image)
    background_window=tk.Label(my_window,
                              text='Welcome\nCurrency Converter',
                              image=photo,
                              compound=tk.BOTTOM, # put the image below where the label will be
                              font=('Calibri',20,'bold italic'),
                              fg='black')
    background_window.place(x=0,y=1000)
    background_window.pack()
    my_window.mainloop()
    

    Here I am using Pillow to import the image, resize it, and then pass it off to ImageTk. To install pillow, follow these instructions. How to install Pillow on Python 3.5?

    Feel free to ask me for more help concerning this. It worked for me, and I'd love to know if it worked for you!