I am started to learn python from scratch and not everything is clear to me.
I am trying to make a simple gallery application. When application started - you can browse images (like Next and Previous). All images are named like 1.png
, 2.png
, etc. and placed in the same folder.
So I am stuck with button. Can't make it change picture. First image is loading successful (1.png
), but when I am printing variable Imagecount
(just to check it) - it is already equal 2 instead of 1. And nothing happens when button is pressed.
Program is starting without errors. My code is below:
import tkinter
from tkinter import PhotoImage, Tk, Canvas
imagecount = 1 #variable to change file name
class MainUI:
def change_pic(self, imagecount): # getting new path to image
self.filename = r"D:/pic/%d.png" % imagecount
return (self.filename)
def clicker(self): # that function should add 1 to variable Imagecount on button click
global imagecount
imagecount += 1
return(imagecount)
def __init__(self):
self.windows = Tk()
self.windows.title("My test gallery")
self.windows.geometry("1080x720")
self.windows.minsize(300, 300)
self.windows.maxsize(1000, 1000)
self.can = Canvas(self.windows, width=700, height=1000)
self.icon = PhotoImage(file=self.change_pic(imagecount)) # here I create the
# image in this class,
self.can.create_image(0, 0, anchor='nw', image=self.icon)
# and there is a button that is not working
self.button1 = tkinter.Button(self.windows, text="Next", command=self.clicker())
self.button1.pack()
self.can.pack()
self.windows.mainloop()
app = MainUI()