Home > Mobile >  How to change the place of the GIF in tkinter?
How to change the place of the GIF in tkinter?

Time:05-15

I have a this code where GIF file shows in tkinter canvas. The problem is that I need it to be in the top right corner, not in the middle. I tried and searched a lot but there's nothing in the internet about GIF placement in tkinter.

import tkinter as tk
from tkinter import *

wd = tk.Tk()
wd.geometry("1000x600")

frameCnt = 54
frames = [PhotoImage(file='teamfortress2.gif', format='gif -index %i' % (i))
          for i in range(frameCnt)]

def update(ind):
    frame = frames[ind]
    ind  = 1
    if ind == frameCnt:
        ind = 0
    label.configure(image=frame)
    wd.after(100, update, ind)


label = Label(wd)
label.pack()
wd.after(0, update, 0)

wd.mainloop()

CodePudding user response:

Actually what you're really asking about is how to put the Label with the GIF on it in the top right corner. Fortunately the tkinter pack() geometry manager has an option for that named anchor= which determines where the widget is placed inside the packing box. It used compass terminology, so the top right would be the North East corner (or NE).

See The Tkinter Pack Geometry Manager

...
label = Label(wd)
label.pack(anchor=tk.NE)  # <- Puts label in top-right corner.
wd.after(0, update, 0)

wd.mainloop()

CodePudding user response:

anchor option in Label() set the place where your text is inside the label widget.

fill option in pack() set the size of your widget, an nice example is in fhdrsdg'post

Then try this :

label = Label(wd, anchor="ne")  # "ne" for North-East
label.pack(fill="x")
  • Related