loading multiple images at a time in a scroller

I’m new to Python, and basically, what I’m trying to make is an image scroller that shows 3 images at a time. I thought that putting them into multiple lists would make it work, but it doesn’t. I’ll try to clarify if it doesn’t make sense, but here is my code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import tkinter as tk
from tkinter import *
from PIL import *
win = tk.Tk()
win.iconbitmap(r'pikachu.ico')
win.title('choose your pokemon!')
text = tk.Label(win, text='choose your starter pokemon!', fg='white', bg='red', font=20)
gen = tk.Label(win, text='generation 1', font=14, pady=10, bg='red')
win.geometry('400x200')
win.maxsize(400, 200)
win.config(bg='red')
button = Button(win, text='next gen>')
# loading alllll images (load gifs once i find out how..)
img1 = PhotoImage(file='poke/1_bulbasaur.png')
img2 = PhotoImage(file='poke/1_charmander.png')
img3 = PhotoImage(file='poke/1_squirtle.png')
img4 = PhotoImage(file='poke/2_chikorita.png')
img5 = PhotoImage(file='poke/2_cyndaquil.png')
img6 = PhotoImage(file='poke/2_totodile.png')
img7 = PhotoImage(file='poke/3_treecko.png')
img8 = PhotoImage(file='poke/3_torchic.png')
img9 = PhotoImage(file='poke/3_mudkip.png')
img10 = PhotoImage(file='poke/4_turtwig.png')
img11 = PhotoImage(file='poke/4_chimchar.png')
img12 = PhotoImage(file='poke/4_piplup.png')
img13 = PhotoImage(file='poke/5_snivy.png')
img14 = PhotoImage(file='poke/5_tepig.png')
img15 = PhotoImage(file='poke/5_oshawott.png')
img16 = PhotoImage(file='poke/6_chespin.png')
img17 = PhotoImage(file='poke/6_fennekin.png')
img18 = PhotoImage(file='poke/6_froakie.png')
img19 = PhotoImage(file='poke/7_rowlet.png')
img20 = PhotoImage(file='poke/7_litten.png')
img21 = PhotoImage(file='poke/7_popplio.png')
# pokemon gen lists
gen1 = ['img1', 'img2', 'img3']
gen2 = ['img4', 'img5', 'img6']
gen3 = ['img7', 'img8', 'img9']
gen4 = ['img10', 'img11', 'img12']
gen5 = ['img13', 'img14', 'img15']
gen6 = ['img16', 'img17', 'img18']
gen7 = ['img19', 'img20', 'img21']
genlist = ['gen1', 'gen2', 'gen3', 'gen4', 'gen5', 'gen6', 'gen7']
counter = 0
def changegen():
global counter
if counter < len(genlist) - 1:
counter += 1
else:
counter = 0
text.configure(image=genlist[counter])
gen.configure(text='generation ' + str(counter + 1))
button.pack(side=BOTTOM, command=changegen())
text.pack()
gen.pack()
win.mainloop()
</code>
<code>import tkinter as tk from tkinter import * from PIL import * win = tk.Tk() win.iconbitmap(r'pikachu.ico') win.title('choose your pokemon!') text = tk.Label(win, text='choose your starter pokemon!', fg='white', bg='red', font=20) gen = tk.Label(win, text='generation 1', font=14, pady=10, bg='red') win.geometry('400x200') win.maxsize(400, 200) win.config(bg='red') button = Button(win, text='next gen>') # loading alllll images (load gifs once i find out how..) img1 = PhotoImage(file='poke/1_bulbasaur.png') img2 = PhotoImage(file='poke/1_charmander.png') img3 = PhotoImage(file='poke/1_squirtle.png') img4 = PhotoImage(file='poke/2_chikorita.png') img5 = PhotoImage(file='poke/2_cyndaquil.png') img6 = PhotoImage(file='poke/2_totodile.png') img7 = PhotoImage(file='poke/3_treecko.png') img8 = PhotoImage(file='poke/3_torchic.png') img9 = PhotoImage(file='poke/3_mudkip.png') img10 = PhotoImage(file='poke/4_turtwig.png') img11 = PhotoImage(file='poke/4_chimchar.png') img12 = PhotoImage(file='poke/4_piplup.png') img13 = PhotoImage(file='poke/5_snivy.png') img14 = PhotoImage(file='poke/5_tepig.png') img15 = PhotoImage(file='poke/5_oshawott.png') img16 = PhotoImage(file='poke/6_chespin.png') img17 = PhotoImage(file='poke/6_fennekin.png') img18 = PhotoImage(file='poke/6_froakie.png') img19 = PhotoImage(file='poke/7_rowlet.png') img20 = PhotoImage(file='poke/7_litten.png') img21 = PhotoImage(file='poke/7_popplio.png') # pokemon gen lists gen1 = ['img1', 'img2', 'img3'] gen2 = ['img4', 'img5', 'img6'] gen3 = ['img7', 'img8', 'img9'] gen4 = ['img10', 'img11', 'img12'] gen5 = ['img13', 'img14', 'img15'] gen6 = ['img16', 'img17', 'img18'] gen7 = ['img19', 'img20', 'img21'] genlist = ['gen1', 'gen2', 'gen3', 'gen4', 'gen5', 'gen6', 'gen7'] counter = 0 def changegen(): global counter if counter < len(genlist) - 1: counter += 1 else: counter = 0 text.configure(image=genlist[counter]) gen.configure(text='generation ' + str(counter + 1)) button.pack(side=BOTTOM, command=changegen()) text.pack() gen.pack() win.mainloop() </code>
import tkinter as tk
from tkinter import *
from PIL import *


win = tk.Tk()

win.iconbitmap(r'pikachu.ico')
win.title('choose your pokemon!')
text = tk.Label(win, text='choose your starter pokemon!', fg='white', bg='red', font=20)
gen = tk.Label(win, text='generation 1', font=14, pady=10, bg='red')
win.geometry('400x200')
win.maxsize(400, 200)
win.config(bg='red')
button = Button(win, text='next gen>')

# loading alllll images (load gifs once i find out how..)
img1 = PhotoImage(file='poke/1_bulbasaur.png')
img2 = PhotoImage(file='poke/1_charmander.png')
img3 = PhotoImage(file='poke/1_squirtle.png')
img4 = PhotoImage(file='poke/2_chikorita.png')
img5 = PhotoImage(file='poke/2_cyndaquil.png')
img6 = PhotoImage(file='poke/2_totodile.png')
img7 = PhotoImage(file='poke/3_treecko.png')
img8 = PhotoImage(file='poke/3_torchic.png')
img9 = PhotoImage(file='poke/3_mudkip.png')
img10 = PhotoImage(file='poke/4_turtwig.png')
img11 = PhotoImage(file='poke/4_chimchar.png')
img12 = PhotoImage(file='poke/4_piplup.png')
img13 = PhotoImage(file='poke/5_snivy.png')
img14 = PhotoImage(file='poke/5_tepig.png')
img15 = PhotoImage(file='poke/5_oshawott.png')
img16 = PhotoImage(file='poke/6_chespin.png')
img17 = PhotoImage(file='poke/6_fennekin.png')
img18 = PhotoImage(file='poke/6_froakie.png')
img19 = PhotoImage(file='poke/7_rowlet.png')
img20 = PhotoImage(file='poke/7_litten.png')
img21 = PhotoImage(file='poke/7_popplio.png')


# pokemon gen lists
gen1 = ['img1', 'img2', 'img3']
gen2 = ['img4', 'img5', 'img6']
gen3 = ['img7', 'img8', 'img9']
gen4 = ['img10', 'img11', 'img12']
gen5 = ['img13', 'img14', 'img15']
gen6 = ['img16', 'img17', 'img18']
gen7 = ['img19', 'img20', 'img21']

genlist = ['gen1', 'gen2', 'gen3', 'gen4', 'gen5', 'gen6', 'gen7']

counter = 0
def changegen():
    global counter
    if counter < len(genlist) - 1:
        counter += 1
    else:
        counter = 0
    text.configure(image=genlist[counter])
    gen.configure(text='generation ' + str(counter + 1))


button.pack(side=BOTTOM, command=changegen())
text.pack()
gen.pack()

win.mainloop()

I think it’s an issue with sublists, but I’m not sure about it.

The idea of ​​using a list with sublists works. But there are several issues with your code.

Nested lists must refer to image objects, not strings.

To start generation, you can call the function outside the button command when all widgets have already been created in the root window.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import tkinter as tk
# from tkinter import *
# from PIL import *
win = tk.Tk()
win.iconbitmap(r'pikachu.ico')
win.title('choose your pokemon!')
win.geometry('400x200')
win.maxsize(400, 200)
win.config(bg='red')
# loading alllll images (load gifs once i find out how..)
img1 = tk.PhotoImage(file='poke/1_bulbasaur.png')
img2 = tk.PhotoImage(file='poke/1_charmander.png')
img3 = tk.PhotoImage(file='poke/1_squirtle.png')
img4 = tk.PhotoImage(file='poke/2_chikorita.png')
img5 = tk.PhotoImage(file='poke/2_cyndaquil.png')
img6 = tk.PhotoImage(file='poke/2_totodile.png')
img7 = tk.PhotoImage(file='poke/3_treecko.png')
img8 = tk.PhotoImage(file='poke/3_torchic.png')
img9 = tk.PhotoImage(file='poke/3_mudkip.png')
img10 = tk.PhotoImage(file='poke/4_turtwig.png')
img11 = tk.PhotoImage(file='poke/4_chimchar.png')
img12 = tk.PhotoImage(file='poke/4_piplup.png')
img13 = tk.PhotoImage(file='poke/5_snivy.png')
img14 = tk.PhotoImage(file='poke/5_tepig.png')
img15 = tk.PhotoImage(file='poke/5_oshawott.png')
img16 = tk.PhotoImage(file='poke/6_chespin.png')
img17 = tk.PhotoImage(file='poke/6_fennekin.png')
img18 = tk.PhotoImage(file='poke/6_froakie.png')
img19 = tk.PhotoImage(file='poke/7_rowlet.png')
img20 = tk.PhotoImage(file='poke/7_litten.png')
img21 = tk.PhotoImage(file='poke/7_popplio.png')
# pokemon gen lists
gen1 = [img1, img2, img3]
gen2 = [img4, img5, img6]
gen3 = [img7, img8, img9]
gen4 = [img10, img11, img12]
gen5 = [img13, img14, img15]
gen6 = [img16, img17, img18]
gen7 = [img19, img20, img21]
genlist = [gen1, gen2, gen3, gen4, gen5, gen6, gen7]
counter = 0
def changegen():
global counter
if counter == len(genlist):
counter = 0
print(counter)
pokemon1.configure(image=genlist[counter][0])
pokemon2.configure(image=genlist[counter][1])
pokemon3.configure(image=genlist[counter][2])
counter += 1
gen.configure(text='generation ' + str(counter))
text = tk.Label(win, text='choose your starter pokemon!', fg='white', bg='red', font=20)
text.pack(fill=tk.BOTH, expand=True)
# frame that contains labels
frame = tk.Frame(win, bg='red')
frame.pack(fill=tk.BOTH, expand=True)
# It looks better if your images are the same size.
frame.columnconfigure((0, 1, 2), weight=1, uniform='a') # equal-width grid columns
# The frame is packed in the root window, but the frame's children are ordered by the grid geometry manager.
# | col 0 | col 1 | col 2
# --------------------------------------
# row 0 | pokemon1 | pokemon2 | pokemon3
# --------------------------------------
pokemon1 = tk.Label(frame, bg='red')
pokemon1.grid(column=0, row=0)
pokemon2 = tk.Label(frame, bg='red')
pokemon2.grid(column=1, row=0)
pokemon3 = tk.Label(frame, bg='red')
pokemon3.grid(column=2, row=0)
gen = tk.Label(win, text='generation 1', font=14, pady=10, bg='red')
gen.pack(fill=tk.BOTH, expand=True)
button = tk.Button(win, text='next gen>', command=changegen)
button.pack(side=tk.BOTTOM)
# At the time we call the function, the variable names are already defined (pokemon1, pokemon2, pokemon3, gen, ...).
print(globals().keys())
# call function to start generation 1
changegen()
win.mainloop()
</code>
<code>import tkinter as tk # from tkinter import * # from PIL import * win = tk.Tk() win.iconbitmap(r'pikachu.ico') win.title('choose your pokemon!') win.geometry('400x200') win.maxsize(400, 200) win.config(bg='red') # loading alllll images (load gifs once i find out how..) img1 = tk.PhotoImage(file='poke/1_bulbasaur.png') img2 = tk.PhotoImage(file='poke/1_charmander.png') img3 = tk.PhotoImage(file='poke/1_squirtle.png') img4 = tk.PhotoImage(file='poke/2_chikorita.png') img5 = tk.PhotoImage(file='poke/2_cyndaquil.png') img6 = tk.PhotoImage(file='poke/2_totodile.png') img7 = tk.PhotoImage(file='poke/3_treecko.png') img8 = tk.PhotoImage(file='poke/3_torchic.png') img9 = tk.PhotoImage(file='poke/3_mudkip.png') img10 = tk.PhotoImage(file='poke/4_turtwig.png') img11 = tk.PhotoImage(file='poke/4_chimchar.png') img12 = tk.PhotoImage(file='poke/4_piplup.png') img13 = tk.PhotoImage(file='poke/5_snivy.png') img14 = tk.PhotoImage(file='poke/5_tepig.png') img15 = tk.PhotoImage(file='poke/5_oshawott.png') img16 = tk.PhotoImage(file='poke/6_chespin.png') img17 = tk.PhotoImage(file='poke/6_fennekin.png') img18 = tk.PhotoImage(file='poke/6_froakie.png') img19 = tk.PhotoImage(file='poke/7_rowlet.png') img20 = tk.PhotoImage(file='poke/7_litten.png') img21 = tk.PhotoImage(file='poke/7_popplio.png') # pokemon gen lists gen1 = [img1, img2, img3] gen2 = [img4, img5, img6] gen3 = [img7, img8, img9] gen4 = [img10, img11, img12] gen5 = [img13, img14, img15] gen6 = [img16, img17, img18] gen7 = [img19, img20, img21] genlist = [gen1, gen2, gen3, gen4, gen5, gen6, gen7] counter = 0 def changegen(): global counter if counter == len(genlist): counter = 0 print(counter) pokemon1.configure(image=genlist[counter][0]) pokemon2.configure(image=genlist[counter][1]) pokemon3.configure(image=genlist[counter][2]) counter += 1 gen.configure(text='generation ' + str(counter)) text = tk.Label(win, text='choose your starter pokemon!', fg='white', bg='red', font=20) text.pack(fill=tk.BOTH, expand=True) # frame that contains labels frame = tk.Frame(win, bg='red') frame.pack(fill=tk.BOTH, expand=True) # It looks better if your images are the same size. frame.columnconfigure((0, 1, 2), weight=1, uniform='a') # equal-width grid columns # The frame is packed in the root window, but the frame's children are ordered by the grid geometry manager. # | col 0 | col 1 | col 2 # -------------------------------------- # row 0 | pokemon1 | pokemon2 | pokemon3 # -------------------------------------- pokemon1 = tk.Label(frame, bg='red') pokemon1.grid(column=0, row=0) pokemon2 = tk.Label(frame, bg='red') pokemon2.grid(column=1, row=0) pokemon3 = tk.Label(frame, bg='red') pokemon3.grid(column=2, row=0) gen = tk.Label(win, text='generation 1', font=14, pady=10, bg='red') gen.pack(fill=tk.BOTH, expand=True) button = tk.Button(win, text='next gen>', command=changegen) button.pack(side=tk.BOTTOM) # At the time we call the function, the variable names are already defined (pokemon1, pokemon2, pokemon3, gen, ...). print(globals().keys()) # call function to start generation 1 changegen() win.mainloop() </code>
import tkinter as tk
# from tkinter import *
# from PIL import *


win = tk.Tk()

win.iconbitmap(r'pikachu.ico')
win.title('choose your pokemon!')
win.geometry('400x200')
win.maxsize(400, 200)
win.config(bg='red')

# loading alllll images (load gifs once i find out how..)
img1 = tk.PhotoImage(file='poke/1_bulbasaur.png')
img2 = tk.PhotoImage(file='poke/1_charmander.png')
img3 = tk.PhotoImage(file='poke/1_squirtle.png')
img4 = tk.PhotoImage(file='poke/2_chikorita.png')
img5 = tk.PhotoImage(file='poke/2_cyndaquil.png')
img6 = tk.PhotoImage(file='poke/2_totodile.png')
img7 = tk.PhotoImage(file='poke/3_treecko.png')
img8 = tk.PhotoImage(file='poke/3_torchic.png')
img9 = tk.PhotoImage(file='poke/3_mudkip.png')
img10 = tk.PhotoImage(file='poke/4_turtwig.png')
img11 = tk.PhotoImage(file='poke/4_chimchar.png')
img12 = tk.PhotoImage(file='poke/4_piplup.png')
img13 = tk.PhotoImage(file='poke/5_snivy.png')
img14 = tk.PhotoImage(file='poke/5_tepig.png')
img15 = tk.PhotoImage(file='poke/5_oshawott.png')
img16 = tk.PhotoImage(file='poke/6_chespin.png')
img17 = tk.PhotoImage(file='poke/6_fennekin.png')
img18 = tk.PhotoImage(file='poke/6_froakie.png')
img19 = tk.PhotoImage(file='poke/7_rowlet.png')
img20 = tk.PhotoImage(file='poke/7_litten.png')
img21 = tk.PhotoImage(file='poke/7_popplio.png')

# pokemon gen lists
gen1 = [img1, img2, img3]
gen2 = [img4, img5, img6]
gen3 = [img7, img8, img9]
gen4 = [img10, img11, img12]
gen5 = [img13, img14, img15]
gen6 = [img16, img17, img18]
gen7 = [img19, img20, img21]

genlist = [gen1, gen2, gen3, gen4, gen5, gen6, gen7]

counter = 0
def changegen():
    global counter
    if counter == len(genlist):
        counter = 0
    print(counter)
    pokemon1.configure(image=genlist[counter][0])
    pokemon2.configure(image=genlist[counter][1])
    pokemon3.configure(image=genlist[counter][2])
    counter += 1
    gen.configure(text='generation ' + str(counter))

text = tk.Label(win, text='choose your starter pokemon!', fg='white', bg='red', font=20)
text.pack(fill=tk.BOTH, expand=True)

# frame that contains labels
frame = tk.Frame(win, bg='red')
frame.pack(fill=tk.BOTH, expand=True)
# It looks better if your images are the same size.
frame.columnconfigure((0, 1, 2), weight=1, uniform='a') # equal-width grid columns
# The frame is packed in the root window, but the frame's children are ordered by the grid geometry manager.
#       | col 0    | col 1    | col 2
# --------------------------------------
# row 0 | pokemon1 | pokemon2 | pokemon3
# --------------------------------------
pokemon1 = tk.Label(frame, bg='red')
pokemon1.grid(column=0, row=0)
pokemon2 = tk.Label(frame, bg='red')
pokemon2.grid(column=1, row=0)
pokemon3 = tk.Label(frame, bg='red')
pokemon3.grid(column=2, row=0)

gen = tk.Label(win, text='generation 1', font=14, pady=10, bg='red')
gen.pack(fill=tk.BOTH, expand=True)

button = tk.Button(win, text='next gen>', command=changegen)
button.pack(side=tk.BOTTOM)

# At the time we call the function, the variable names are already defined (pokemon1, pokemon2, pokemon3, gen, ...).
print(globals().keys())
# call function to start generation 1
changegen()

win.mainloop()

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật