I am creating a book with Tkinter, I have defined each pages of my book to a canvas and I want to be able to shift between pages by closing the current canvas (current page) and open the next canvas (next page).
I have two problems:
The problem is that when I use the command
Canvas.delete(can1)
the canvas does not close (I don't know why...), so I triedCanvas.destroy(can1)
, but then when I want to go to the previous page I can't because I destroyed it (I think that this is the problem). Is there an other function that does the same job of deleting the canvas from my main window, but then if i want to reopen it I can?If you look at the code (at the end), I need to use a Button to open the first page because I can't put the 'page1()' in the mainloop(). Is there a command to call a function once at the start of a program? So that when I run it the first page opens automatically... (I hope you know/understand what I'm searching).
I apologies for my awful english and for the french in the code.. ;)
The code is in python 3.6!
from tkinter import *
ModeleProiePredateur=Tk()
ModeleProiePredateur.title('Le Modele Proie-Predateur')
Largeur=1047
Hauteur=740
x=1
can1 = Canvas(ModeleProiePredateur,width=Largeur,height=Hauteur)
can2 = Canvas(ModeleProiePredateur,width=Largeur,height=Hauteur)
can3 = Canvas(ModeleProiePredateur,width=Largeur,height=Hauteur)
def affichage_page(page):
if page == 1 :
page1()
if page == 2:
page2()
if page == 3 :
page3()
def candel(cannum):
if cannum == 1:
Canvas.destroy(can1) #.destroy ne convient pas pour pouvoir reouvrir apres
if cannum==2:
Canvas.destroy(can2) #.delete de fonctionne pas comme prevu
if cannum==3:
Canvas.destroy(can3)
def pagesuivante():
global x
candel(x)
x = x+1
affichage_page(x)
def pageprecedente():
global x
y=x
candel(y)
y=y-1
affichage_page(y)
def page1():
can1.pack()
planche = PhotoImage(file='/Users/loysforget1/Desktop/Planches/planche1.png')
icon1 = can1.create_image(0,0, anchor='nw',image=planche)
can1.photo = planche
button1 = Button(ModeleProiePredateur, text = "Suivant", command = pagesuivante, anchor = N)
button1.configure(width = 10, activebackground = "#33B5E5", relief = FLAT)
button1_ModeleProiePredateur = can1.create_window(10, 730, anchor = SW, window=button1)
button2 = Button(ModeleProiePredateur, text = "Precedent", command = pageprecedente, anchor = N)
button2.configure(width = 10, activebackground = "#33B5E5", relief = FLAT)
button2_ModeleProiePredateur = can1.create_window(100, 730, anchor = SW, window=button2)
self.start()
def page2():
can2.pack()
planche = PhotoImage(file='/Users/loysforget1/Desktop/Planches/planche7.png')
icon2 = can2.create_image(0,0, anchor='nw',image=planche)
can2.photo = planche
button1 = Button(ModeleProiePredateur, text = "Suivant", command = pagesuivante, anchor = N)
button1.configure(width = 10, activebackground = "#33B5E5", relief = FLAT)
button1_ModeleProiePredateur = can2.create_window(10, 730, anchor = SW, window=button1)
button2 = Button(ModeleProiePredateur, text = "Precedent", command = pageprecedente, anchor = N)
button2.configure(width = 10, activebackground = "#33B5E5", relief = FLAT)
button2_ModeleProiePredateur = can2.create_window(100, 730, anchor = SW, window=button2)
def page3():
can3.pack()
planche = PhotoImage(file='/Users/loysforget1/Desktop/Planches/planche9.png')
icon3 = can3.create_image(0,0, anchor='nw',image=planche)
can3.photo = planche
button1 = Button(ModeleProiePredateur, text = "Suivant", command = pagesuivante, anchor = N)
button1.configure(width = 10, activebackground = "#33B5E5", relief = FLAT)
button1_ModeleProiePredateur = can3.create_window(10, 730, anchor = SW, window=button1)
button2 = Button(ModeleProiePredateur, text = "Precedent", command = pageprecedente, anchor = N)
button2.configure(width = 10, activebackground = "#33B5E5", relief = FLAT)
button2_ModeleProiePredateur = can3.create_window(100, 730, anchor = SW, window=button2)
Button(ModeleProiePredateur, text ='Page 1', command = page1).pack(side=RIGHT,padx = 5,pady = 5)
ModeleProiePredateur.mainloop()