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:
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.
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